2. 4
Design a GUI using QT Designer of PyQT4
QT Designer
Create widget
Using Filter: Label
Drag and drop to the new widget
Adjust size
Press Ctrl+R to view what you created
Filter: Line Edit
Drag and drop to widget
Filter: Button
Drag and drop a push button
3. 5
Signals and slots: Components communication
Move from Edit Widget mode to Edit Signal/Slots
mode
Or press F4
Use F3 to go back to Edit Widget mode
Next, we want the Push button to affect the Hello
world label
Click and drug a line from the
button to the label
4. 6
Signals and slots: Components communication
Select the result of the “IF-THEN” statement
Select clicked() and Next clear()
F3 to go back to Edit Widget mode
Change the name
of the button to show Clear
Ctrl+R
Try it!
6. 8
Signals and slots: Components communication
We want the textbox to edit the label
F4 to go to Edit Signal/Slots mode
IF textChanged(QString) THEN setText (QString)
7. 9
Signals and slots: Components communication
Ctrl+R
Type to the text box
IF push button THEN clicked()->clear()
8. 10
Edit widget
F3
Select Push button and Text box
Lay out Horizontally
Select 3 items
Lay Out Vertically
9. 11
Edit widget
Make the widget responsive
Select Form: QWidget from Object Inspector
Lay Out in a Grid
Ctrl+R
Resize widget size
10. 12
Save project and convert it to python script
Save it as a ui file
qt_hello.ui (like XML schema)
Convert ui file to python code
Open a command line
C:Python27Libsite-packagesPyQt4
pyuic4 -o OutFile_ui.py InFile.ui
Pyuic4.bat file is in
C:Python27Libsite-packagesPyQt4
11. 13
Execute your script using python
Open qt_hello.py using an editor
Add import sys after from PyQt4 import QtCore, QtGui
At the end of your code add:
def main():
app = QtGui.QApplication(sys.argv)
window = QtGui.QDialog()
ui = Ui_Form()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
System-specific parameters
and functions
Define a new function called main()
without any parameters
The QDialog class is the base class of
dialog windows.
A dialog window is a top-level window
mostly used for short-term tasks and
brief communications with the user.
Initializes the window system and
constructs an application object
with argc command line arguments
in argv. sys. argv is a list in Python,
which contains the command-line
arguments passed to the script.Form created
from QT Designer
When the Python interpreter reads a source
file, it executes all of the code found in it.
12. 14
Execute your script using python
Open cmd (command shell)
python py_hello.py
In a lab pc you have to write the full path of python from
the folder that you save the py_hello.py file, e.g.
C:Python27python py_hello.py