Welcome to Engineering Python.
This is a Python programming course for engineers.
In this video, I'll continue to discuss GUI programming using PyQt and Qt Designer.
We will create a simple calculator.
This simple calculator can perform simple math operations such as addition, subtraction,
multiplication, and division.
We will use the following widgets in this program.
QMainWindow, QPushButton, QLineEdit, QComboBox, QLabel, QStatusBar, and QMessageBox.
We will use the GUI development flow I introduced in a previous video to develop this program.
Step 1 is to design the interface in Qt Designer.
To start Qt Designer, you can type in designer in the Windows Start menu.
For Mac OS, you can try open -a designer in your terminal to launch Qt Designer.
In this New Form dialog, we always choose Main Window and click Create.
Because I have already created the UI, I'll just open it from my folder.
In the view menu, check all these items to show these tool boxes.
This is the Widget box.
This is the object inspector.
This is the property editor.
This is the resource browser.
This is the action editor.
This is the signal slot editor.
Next, I'll briefly describe what I did when I created this graphic user interface.
I clicked the empty space of the main window.
In the property editor, set the MainWindow's geometry width to 400, height to 200.
Set the window title to Calculator.
In the object inspector, there was originally a menubar.
I just right clicked and removed the menubar so you don't see it here.
We don't need it in this program.
In the widget box, I searched for line edit.
Dragged and dropped this widget to the main window.
I repeated this process two more times.
So, you see three line edits here.
I changed their objectNames to lineEdit1, lineEdit2 and lineEdit3.
Changed their toolTips to First Number, Second Number, and Result, respectively.
So, when we run this program later and hover the mouse on these line edits, we will see
these tooltips.
I searched for combo box in the widget box.
Dragged and dropped a combo box to the main window.
Double clicked the combo box, added four operators, plus, minus, star, which means times, and
slash, which means divided by.
Set the toolTip to Operator.
Then, I searched for label in the widget box.
Dragged and dropped a label to the main window.
Double clicked the label and changed the text to equal.
I searched for pushbutton in the widget box.
Dragged and dropped a pushbutton to the main window.
Double clicked it and changed the text to Clear.
Changed the objectName to buttonClear in the property editor.
Changed the statusTip to Clear All the Boxes.
It will show a tip in the status bar if we hover the cursor on top of this button.
I repeated this process and added another pushButton, changed the text to Calculate,
and changed the objectName to buttonCalc.
Changed the statusTip to Calculate the Result.
Let me break out this layout first.
Then, what I did was selecting the top five widgets and click this lay out horizontally
button.
Select the bottom two buttons and click this lay out horizontally button.
Use Control-A to select all the widgets and click this lay out vertically button.
Click an empty area in the main window and click this lay out in a grid button.
After that, I selected the push button Clear.
In the signal slot editor, I added a signal.
Double clicked the sender and selected buttonClear.
Double clicked the signal and selected clicked.
Double clicked the receiver and selected lineEdit1.
Double clicked the slot and selected clear.
I added two more signals, changed the receivers to lineEdit2 and lineEdit3, respectively.
What this does is that, when we click the clear button, the contents of these three
line edits will be cleared.
You can preview this interface by pressing Control R or clicking the menu Form Preview.
Type in some numbers and click clear.
It works.
However, the Calculate button does nothing.
We will write code to process the click of this button later.
After we are done, we save the interface as calculator_gui.ui.
If you are interested, you can open this ui file and take a look in the Jupyter editor.
It's actually an XML document.
Step 2 is to convert the ui file to a Python file using the following command in a command
prompt or a terminal.
pyuic5 -x calculator_gui.ui -o calculator_gui.py After that, we will get this Python file calculator_gui.py.
You can open this Python file and see the content.
This file translated the ui file to a Python file using PyQt5.
The entire window is an instance of this Ui_MainWindow class.
You can find all the widgets we added to the interface in this setupUI function.
Like the grid layout, the line edits, the combo box, the pushButtons, and the status
bar.
Some of the properties are defined in this retranslateUi function.
Don't change anything in this Python file.
All changes made in this file will be lost if you use the pyuic5 command again to convert
the ui file to this py file.
Step 3 is to code in Python.
We need to create a new file named calculator.py.
I already did that so I'll just open my file.
But you need to write this entire file by yourself if you are writing a new program.
Note this is different from the calculator_gui.py.
In the first line of this file, we need to import everything from calculator_gui.
Then we will define the signals function to deal with the signals passed by the widgets.
This line will connect the Calculate button's clicked signal to a user-defined calc function.
In this calc function, we will do the actual math calculation.
After the user typed in some numbers.
We give the text in lineEdit1 to a and the text in lineEdit2 to b.
They will be the first number and the second number.
We give the currentText in the combo box to operator.
It will be one of the plus, minus, times, and divided by operators.
Next, we try to calculate the result by assembling these three strings in one expression and
executing that expression.
For example, if a is 2, b is 4, and the operator is a star, the eval function will do the multiplication.
Then we show the calculation result 8 in lineEdit3.
Some errors may occur when the Calculate button is clicked.
For example, lineEdit1 or lineEdit2 is empty, or a number is divided by 0.
If an error occurs, we use a message box to show a critical error message like invalid
inputs.
The user needs to confirm this error by clicking the Ok button before he or she tries again.
Next, we need to add these two self-defined functions as new attributes in the Ui_MainWindow
class.
This calculator.py file can be run as a stand-alone program, it can also be imported to other
programs as a library.
If it is run as a stand-alone program like what we will do later, we will need to import
the sys library.
Every PyQt5 program must create a QApplication object.
If this program is run in the command line, sys dot argv allows the user to pass additional
parameters.
This statement will create a main window instance.
This one will create a Ui_MainWindow instance.
Next, we add all the widgets to the main window.
Connect the signals or events with the appropriate functions or actions.
Show the main window on the computer screen.
Wait for the termination signal.
It will receive a termination signal when the user clicked the close window button on
the top right or top left corner, depending on whether you are running windows or mac
os.
If a termination signal is captured, exit the program.
Step 4 is to execute the program.
We do it in Jupyter Notebook using the run magic.
Type in some numbers like 8 and 7.
Change the combo box to minus.
Then click Calculate.
The result is 1.
Click the clear button.
Type in 4 and 0.
Choose divided by in the combo box.
Click Calculate.
We will see an error message, invalid inputs.
Change the second number to 8 and click Calculate.
The result is 0.5.
Click the close window button.
The program will end.
Okay, that was how to use PyQt and Qt Designer to create a simple calculator.
The course materials are available on YouTube and GitHub.
You can watch the course videos in sequence.
If you like this video, please subscribe and share.
I'm Yong Wang.
Thanks for watching.
Không có nhận xét nào:
Đăng nhận xét