from PyQt5 import QtWidgets from PyQt5.QtWidgets import QApplication, QMainWindow import sys class MyWin: def __init__(self): super(MyWin, self).__init__() self.setGeometry(200,200,300,300) self.setWindowTitle("Test") self.initUI() def initUI(self): self.label = QtWidgets.QLabel(self) self.label.setText("First Label") self.label.move(100, 100) self.button = QtWidgets.QPushButton(self) self.button.setText("Button") self.button.clicked.connect(self.click) def click(self): self.label.setText("Pressed ha") def window(): app = QApplication(sys.argv) win = QMainWindow() win.show() sys.exit(app.exec_()) window()
I am a beginner in PyQt5, and I’m having an issue with my program. When this code is executed, a window appears; however, the label and the button does not appear. Any help would be much appreciated.
Advertisement
Answer
You are not creating an instance of your class, which should also inherit from QMainWindow, as right now it’s just a simple python object
subclass.
class MyWin(QtWidgets.QMainWindow): # ... def window(): app = QtWidgets.QApplication(sys.argv) win = MyWin() win.show() sys.exit(app.exec_())