Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put an Image on a QPushButton

Tags:

python

pyqt

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.

like image 687
Margarita Gonzalez Avatar asked May 13 '14 14:05

Margarita Gonzalez


People also ask

How do I make an image clickable in Qt?

Create a instance CClickableLabel, use setPixmap() to set the image and listen to the clicked() signal. That should do the trick.

How do I add an icon to QT?

First, create an ICO format bitmap file that contains the icon image. This can be done using Microsoft Visual Studio: Select File >> New, and choose the Icon File. Note: You need not load the application into the Visual Studio IDE as you are using the icon editor only.


1 Answers

Here is the same example from @NorthCat but for PyQt5:

from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout


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

    def handleButton(self):
        pass


if __name__ == '__main__':

    import sys
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
like image 153
Momen Avatar answered Sep 24 '22 13:09

Momen