Skip to content
Advertisement

PyQt5 window comes up empty

I’m trying to build a simple PyQt5 application . I have so far created a couple of widgets and have added them to my layout . Unfortunately my Window is not showing the lables or the pushbutton which I have created .

from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
import sys
from datetime import datetime


class MyWindow(QtWidgets.QDialog):
    def __init__(self,parent=None):
        super(MyWindow, self).__init__(parent)
        self.setGeometry(200,200,300,300)
        self.setWindowTitle("Timer")
        self.create_widget()
        self.create_layout()
        

    def create_widget(self):
        self.user_name_lbl = QtWidgets.QLabel("username")
        self.start_btn = QtWidgets.QPushButton("Start")
       
        

    def create_layout(self):
        main_layout = QtWidgets.QVBoxLayout(self)
        group_layout = QtWidgets.QHBoxLayout()
        group_layout.addWidget(self.user_name_lbl)
        group_layout.addWidget(self.start_btn)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    form=MyWindow()
    form.show()
    sys.exit(app.exec_())

enter image description here

Advertisement

Answer

The problem is caused because the layout associated with the widgets is not associated with the window. A possible solution is to add layout group_layout to layout main_layout:

main_layout.addLayout(group_layout)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement