-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_Basic_MainWindow.py
50 lines (41 loc) · 2.16 KB
/
01_Basic_MainWindow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Introduction to Python GUIs with PyQt5
# Lesson 01: Basic main window
#Import the necessary libraries
from PyQt5.QtWidgets import (QApplication,
QMainWindow,
QWidget,
QLabel,
QGridLayout)
from PyQt5.QtCore import QCoreApplication
#Define the main window class that inherits the predifined QMainWindow object.
class MainWindow(QMainWindow):
# All classes (objects) have an __init__() function that is executed when the
# class is instanciated. This runs when an instance of the object is created.
def __init__(self):
#The super command makes sure the inhereted class is also initiated.
super().__init__()
#We call a function to initialize the user interface.
self.init_ui()
# Now define the function that initializes the user interface.
# all functions within a class need to reference itself.
def init_ui(self):
# A simple example of some built in functionality is the status bar.
self.statusBar().showMessage("Status Bar Line.")
# Let's make a simple label widget. We have to be sure to have this imported.
greeting_label = QLabel("Hello PyQt5")
#Define a main widget that will contain all the other widgets and set
#it as the central widget.
main_widget = QWidget(self)
self.setCentralWidget(main_widget)
#A layout manager is needed to put things where we want. Let's use a grid.
grid_layout = QGridLayout(main_widget)
#assign the label to the grid.
grid_layout.addWidget(greeting_label,0,0,1,1)
#Setup the window title and make it appear
self.setWindowTitle("Lesson 01: Basic Main Window")
self.show() #This is needed for the window to appear.
# This line is run when the to get everything started.
if __name__ == '__main__':
app = QApplication([]) #The empty list ([]) is passed inplace of system arguments.
execute = MainWindow() #Calls the main window class we defined earlier.
app.exec_() #this starts the event handling loop to accept interaction.