Put an Image on a QPushButton

余生颓废 提交于 2020-07-28 13:14:11

问题


I'm a beginner in PyQt and I have an image known as add.gif. I need to put this image in a QPushButton but I don't know how.


回答1:


Example:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.button = QtGui.QPushButton('', self)
        self.button.clicked.connect(self.handleButton)
        self.button.setIcon(QtGui.QIcon('myImage.jpg'))
        self.button.setIconSize(QtCore.QSize(24,24))
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.button)

    def handleButton(self):
        pass


if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())



回答2:


Assuming pyqt supports gif pictures, this should work

icon  = QtGui.QPixmap('add.gif')
button = QtGui.QPushButton()
button.setIcon(icon)

QPushButton

Push buttons display a textual label, and optionally a small icon. These can be set using the constructors and changed later using setText() and setIcon(). If the button is disabled, the appearance of the text and icon will be manipulated with respect to the GUI style to make the button look "disabled".



来源:https://stackoverflow.com/questions/23634241/put-an-image-on-a-qpushbutton

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!