当前位置:网站首页>Making of simple addition calculator based on pyqt5 and QT Designer
Making of simple addition calculator based on pyqt5 and QT Designer
2022-06-29 09:36:00 【Summer insects can't talk with ice】
be based on PyQt5 and Qt Designer The production of a simple addition calculator
Preface
Write in front of the article , I wrote this article to record my learning process , Give the latecomers a reference and train of thought , This is not a particularly complicated thing, mainly because I saw it in two days b Two articles of the station PyQt5 course
PyQt5 course , From Netease cloud classroom ( I saw it 24 course ) and Python Of Gui Programming -PyQt5 From entry to project practice ( See the second 23 course )
For me, the first article tends to ui Design , The second is better than the first , It's very detailed , I'm glad I saw the second one because the first half of the first one started with a lot of ink Qt Designer There is no connection between program and control , But after reading the second article, I also lamented that I had read the first article before and knew it very early Qt Designer. It saves me a lot of time without my hands UI Interface . See the second 23 In class, I began to give myself a task , Is to design a small calculator , Only adding, but I practice my hand instead of pursuing practicality , I still have a sense of achievement when I have used all my knowledge points .
Of an addition calculator Qt Designer part
Calculator UI Design
Get into Qt Designer after 
choice Main Window Click Create 
It may be very big at first, but you can click on the lower right corner to adjust it 
find Display widgets Tab and select LCD Number Drag to MainWindow
find Buttons Choose Push Button Drag to Main Window

Ctrl+a Choose all the spaces in Right click in the blank Be sure to put it in the blank , Do not on the control , Otherwise you won't adapt to the screen , It's a tragedy when you zoom in and out later 
Click these two points to make a full screen , At this point, you can try to pull the lower right corner to see if it changes with you , however lcd number The display is too large , We click lcd number
In the Attribute Editor on the right QWidget The maximum width of its maximum size is found in the tab , Its value is very large. Let's set a 100 Then make corresponding changes through the scroll wheel corresponding to the actual figure on the right , Make your calculator look more comfortable 
Adjusted lcd number Then we double-click the button to change the content of the button
Here we are ui The interface design is basically completed , The next step is to insert the signal and slot
Insert signal and slot

Click on Edit Then click Edit signal / The slot can be used for signal and slot editing

We click on a button control , Then pull down. ( Be careful not to touch other controls )
Then a box will appear, and we will select on the left clicked(), Select edit on the right 
After entering the edit, click... In the slot +, Add a custom slot function 
Click OK after adding three slot functions we need in the future 
After confirmation, there are no more slot functions in our slot than before , We'll take the one on the left clicked() Pair with the one on the right ( Number button pairing clickNum(),+ Button No. corresponds to clickAdd(),= The button corresponds to clickNum())
This is what it looks like to bury a slot , It's a mess, isn't it ?
Actually, there's a signal in the lower right corner / Slot editor , Here you can clearly see if you have made a mistake 
Click the save icon above to save as .ui file
coordination vscode The subsequent background program design

First of all, we have to vscode Install the plug-in 
Open the folder and right-click to select PYQT:Complie from
It will generate a Ui_AddCalculator.py The file of , We don't have to worry about it , Let's create a new file called StartAddCalculator.py Write program , I have comments on every line
from Ui_AddCalculator import Ui_MainWindow # Import Qt Designer The generated class
import sys # Import standard library sys library
from PyQt5.QtWidgets import QApplication,QMainWindow # Import pyQt5 What the package needs
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__() # Inherit from all parent classes __init__()
self.setupUi(self) # Default run Ui_AddCalculator.py Inside setupUi function
self.Num = 0 # Set the current number to 0
self.Add = [] # Set a list to store the numbers of previous periods
def clickNum(self):
# self.Num = self.lcdNumber.value() # Place the displayed number in Num in
btn = int(self.sender().text()) # Get the name of the sender
self.Num = self.Num*10+btn # Insert the newly entered number into Num At the back of
self.lcdNumber.display(self.Num) # Display the number on the top
def clickAdd(self):
# self.Num = self.lcdNumber.value() # Place the displayed number in Num in
self.Add.append(self.Num) # Cache the numbers in the list
self.Num = 0 # Empty Num Ready to accept the next number
self.lcdNumber.display(0) # Show 0
def clickEqu(self):
# self.Num = self.lcdNumber.value() # Place the displayed number in Num in
self.Add.append(self.Num) # Cache the numbers in the list
self.lcdNumber.display(sum(self.Add))# The list is summed and displayed
self.Num = 0 # Empty Num cache
self.Add = [] # Clear digital cache
if __name__ == "__main__": # When running this file, run the following
app = QApplication(sys.argv) # establish QApplication Object and put it into the system to pass in parameters
Window = MainWindow() # establish MainWindow object
Window.show() # Exhibition MainWindo Object content
sys.exit(app.exec_()) # Get into app.exec_ Event cycle when the system exits the cycle, the whole system exits

A small calculator will complete , Of course, it is easy to complete other functions
边栏推荐
- MySQL uses union all to count the total number of combinations of multiple tables and the number of tables respectively
- Mh/t 6040 smoke density test of aviation materials
- 1.4 机器学习方法之回归问题
- UE4 动画重定向
- Reading notes on how to connect the network - Web server request and response (IV)
- What is hyperfusion? What is the difference with traditional architecture
- Training view (issue temporary storage)
- Mysql配置主从数据库
- Simplicity Studio无法识别新买的JLink v9解决方法
- 长安链数据存储介绍及Mysql存储环境搭建
猜你喜欢

ThinkPHP 6 uses mongodb

Highlight in the middle of the navigation bar at the bottom of wechat applet

YOLO Nano:一种高度紧凑的只看一次的卷积神经网络用于目标检测

GD32F4xx 以太網芯片(enc28j60)驅動移植

Factory mode

3DMax 卡死、白屏、渲染死机问题总结

商业智能BI的未来,如何看待AI+BI这种模式?

KiCad学习笔记--快捷键

The former security director of Uber faced fraud allegations and concealed the data leakage event

Lffd: a lightweight fast face detector for edge detection
随机推荐
Easyexcl export 1million lines of EXECL report font error solution
UE4 plug-in reports an error cannot open include file: 'modulemanager H 'resolve
Written test question "arrange version numbers from large to small"
Wechat applet search keyword highlighting and ctrl+f search positioning
Find the most repeated element in the string
UE4 蓝图修改Array 中Get a copy 为 reference
Wechat applet latest canvas2d handwritten signature
【NOI模拟赛】为NOI加点料(重链剖分,线段树)
UE4 编译单个文件(VS与编辑器分别启动)
easyexecl导出100万行execl报字体错误的解决办法
Mh/t 6040 smoke density test of aviation materials
数据治理:数据标准管理(第三篇)
Record the field name dynamically modified by SetData of wechat applet
Wechat applet jump to official account image and text content
Pytorch summary learning series - data manipulation
Hb5470 combustion test of non-metallic materials in civil aircraft cabin
watch监听和computed计算属性的使用和区别
Detecting and counting tiny faces
Gd32f4xx Ethernet Chip (ENC28J60) Drive transplantation
【数据集】|标注的bbox影响 Can we trust bounding box annotations for object detection