?print-pdf
' Created for
def sub_task_1():
pass
def sub_task_2():
pass
#....
def sub_task_N():
pass
if __name__ == "__main__":
sub_task_1()
sub_task_2()
# ...
sub_task_N()
def start_event_loop():
while True:
if event_queue_not_empty:
if terminate_signal:
exit()
else:
process event()
else:
wait for event
if __name__ == "__main__":
start_event_loop( )
# 1. import needed QtWidgets classes
from PyQt6.QtWidgets import QApplication, QWidget
# 2. the main app instance for our application.
app = QApplication([])
# 3. Create Qt widget, which will be our main window.
window = QWidget()
# 4. show the window
window.show()
# 5. Start the event loop
app.exec()
app = QApplication(sys.argv)
argv
command-line arguments.
app.exec()
# best practice:
sys.exit(app.exec())
app.exec()
begins the QApplication event loop which process our user interactions with the GUI.app.exec_()
and app.exec()
methods are implemented, because exec
is a reserved word in Python2app.exec_()
methodsys.exit(app.exec())
window = QWidget()
window.show()
pyuic6
to convert the ui
file to Python codepyuic6
(Python User Interface Compiler) tool can compile the ui
file to Python code
pyuic6 [options] file.ui
pyuic6 --help
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-p, --preview show a preview of the UI instead of generating code
-o FILE, --output=FILE
write generated code to FILE instead of stdout
-x, --execute generate extra code to test and display the class
-d, --debug show debug output
-i N, --indent=N set indent width to N spaces, tab if N is 0 [default:
4]
-o
) option, the generated python code will be sent to console's output. That's why we must use the -o
option
pyuic6 simpleLoginForm.ui -o simpleLoginForm.py
.ui
file, we can even use the -x
option to generate extra code to run and display the class. But we should never write our functionality in this file
ls -l
# simpleLoginForm.ui
# note, the use of '-x' option
pyuic6 -x -o simpleLoginForm.py simpleLoginForm.ui
ls -l
# simpleLoginForm.py
# simpleLoginForm.ui
python simpleLoginForm.py
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again
object
object, not from the QWidget, and that make the code more obscure
class Ui_Form(object):
...
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
import sys
from PyQt6 import QtWidgets as qtw
from simpleLoginForm import Ui_Form
if __name__ == "__main__":
app = qtw.QApplication(sys.argv)
w = qtw.QWidget()
gw = Ui_Form()
gw.setupUi(w)
w.show()
sys.exit(app.exec())
import sys
from PyQt6 import QtWidgets as qtw
from simpleLoginForm import Ui_Form
class MainWindow(qtw.QWidget, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
# add as any properties here...
self.setWindowTitle('The title of main window')
if __name__ == "__main__":
app = qtw.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())
Make sure you really understand the concept of class inheritance in Python
# the base class
class Parent:
def __init__(self,*args, **kwargs):
print(f'{self} constructor execute')
print(args)
print(kwargs)
# the derived class, which inherits from base class:
class Child(Parent):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
p1 = Parent(1,2,a=3,b=4)
c = Child(5,6,c=7,d=8)
calculator.ui
calculator.ui
file to python module, using the pyuic6
tooluic.loadUiType
which generates and loads a .ui file at runtime. It returns a tuple containing the reference to the Python class, and the base class
import sys
from PyQt6 import QtWidgets as qtw
from PyQt6 import uic
generated_class, base_class = uic.loadUiType('./ui/btnWidget.ui')
print(generated_class, base_class)
if __name__ == "__main__":
app = qtw.QApplication(sys.argv)
w = base_class()
form = generated_class()
form.setupUi(w)
w.show()
sys.exit(app.exec())
For now, in our "dummy" Qt App, we've used only the QtWidgets module. But any Qt App will use (and must include) next three modules:
from PyQt6 import QtWidgets as qtw
from PyQt6 import QtCore as qtc
from PyQt6 import QtGui as qtg
Never use *
imports with Qt modules, as you risk to overwhelm your namespace with hundreds of names
import sys
from PyQt6 import QtWidgets as qtw
from PyQt6 import QtCore as qtc
from PyQt6 import QtGui as qtg
class MainWindow(qtw.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# --------------------------- your code starts here -------------------------- #
# ---------------------------- your code ends here --------------------------- #
self.show();
# ------------------------ create your methods here -------------------------- #
if __name__ == '__main__':
app = qtw.QApplication(sys.argv);
window = MainWindow()
sys.exit(app.exec())