单击按钮图标更改 [英] Button Icon Change on Click

查看:56
本文介绍了单击按钮图标更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make a GUI Application that change the icon on the right clicked button! This is my simple code:

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来代替按钮,标签或其他方式(如果可能).

I've been trying for hours but i'm not still able to do that, any ideas? I would like also to use an Image Widget instead of a button, label, or whatelse if it is possible..

谢谢!

推荐答案

您可以使用 QObject :: sender 来获得单击的按钮.

You can use QObject::sender to get the clicked button.

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

这篇关于单击按钮图标更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆