Answer the question
In order to leave comments, you need to log in
Why doesn't the given image appear on the PyQt5 button?
UPD: Reinstalled PyQt and everything works now
I'm learning how to make applications in Python 3 using the PyQt5 module. I have a list that stores the paths to images that I want to put on buttons. When I try to attach these images to buttons, the buttons remain empty. Please tell me what am I doing wrong?
from PyQt5 import QtCore, QtWidgets, QtGui
list1 = ['picture1.png', 'picture2.png']
class MyWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.button = QtWidgets.QPushButton("Button")
self.box1 = QtWidgets.QVBoxLayout()
self.box1.addWidget(self.button)
self.setLayout(self.box1)
self.pictures()
def pictures(self):
self.box2 = QtWidgets.QHBoxLayout()
for picture in list1:
self.button_picture = QtWidgets.QPushButton(self)
self.button_picture.setIcon(QtGui.QIcon(picture))
self.button_picture.setIconSize(QtCore.QSize(100, 100))
self.box2.addWidget(self.button_picture)
self.box1.addLayout(self.box2)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question