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 .
JavaScript
x
33
33
1
from PyQt5 import QtWidgets, QtGui, QtCore
2
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
3
import sys
4
from datetime import datetime
5
6
7
class MyWindow(QtWidgets.QDialog):
8
def __init__(self,parent=None):
9
super(MyWindow, self).__init__(parent)
10
self.setGeometry(200,200,300,300)
11
self.setWindowTitle("Timer")
12
self.create_widget()
13
self.create_layout()
14
15
16
def create_widget(self):
17
self.user_name_lbl = QtWidgets.QLabel("username")
18
self.start_btn = QtWidgets.QPushButton("Start")
19
20
21
22
def create_layout(self):
23
main_layout = QtWidgets.QVBoxLayout(self)
24
group_layout = QtWidgets.QHBoxLayout()
25
group_layout.addWidget(self.user_name_lbl)
26
group_layout.addWidget(self.start_btn)
27
28
if __name__ == '__main__':
29
app=QApplication(sys.argv)
30
form=MyWindow()
31
form.show()
32
sys.exit(app.exec_())
33
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
:
JavaScript
1
2
1
main_layout.addLayout(group_layout)
2