单击按钮图标更改

卡洛·贝卡里尼(Carlo Beccarini)

我正在尝试制作一个GUI应用程序,以更改右键单击按钮上的图标!这是我的简单代码:

import sys
from PySide.QtCore import *
from PySide.QtGui import *

class Ui_MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi()

    def setupUi(self):
        widget = QWidget()
        layout = QGridLayout()
        self.buttons = list()

        for x in range(3):
            row = list()
            for y in range(3):
                button = QPushButton(QIcon('Empty-Cell.png'), '{},{}'.format(x, y))
                button.clicked.connect(self.button_click)
                row.append(button)
                layout.addWidget(button, x, y)
            self.buttons.append(row)

        widget.setLayout(layout)
        self.setCentralWidget(widget)

    def button_click(self):
        # Change icon HERE!

def main():
    app = QApplication(sys.argv)
    ui = Ui_MainWindow()
    ui.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

这是图片:

GUI应用程序的链接

我已经尝试了几个小时,但仍然无法做到这一点,有什么想法吗?我还想使用Image Widget来代替按钮,标签或其他方式(如果可能)。

谢谢!

帕维尔·斯特拉霍夫(Pavel Strakhov)

您可以QObject::sender用来获取点击的按钮。

def button_click(self):
    test_pixmap = QPixmap(16, 16)
    test_pixmap.fill(Qt.red)
    self.sender().setIcon(QIcon(test_pixmap))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章