Put an Image on a QPushButton

后端 未结 2 641
天命终不由人
天命终不由人 2020-12-31 22:28

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.

相关标签:
2条回答
  • 2020-12-31 23:05

    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".

    0 讨论(0)
  • 2020-12-31 23:30

    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_())
    
    0 讨论(0)
提交回复
热议问题