支持重复运算
1.Ui_untitled.py文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '/home/10318916@zte.intra/lxy/Python/study_one_day/untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(308, 341)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(40, 150, 31, 26))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(90, 150, 31, 26))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(140, 150, 31, 26))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(Form)
self.pushButton_4.setGeometry(QtCore.QRect(40, 190, 31, 26))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(Form)
self.pushButton_5.setGeometry(QtCore.QRect(90, 190, 31, 26))
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_6 = QtWidgets.QPushButton(Form)
self.pushButton_6.setGeometry(QtCore.QRect(140, 190, 31, 26))
self.pushButton_6.setObjectName("pushButton_6")
self.pushButton_7 = QtWidgets.QPushButton(Form)
self.pushButton_7.setGeometry(QtCore.QRect(40, 230, 31, 26))
self.pushButton_7.setObjectName("pushButton_7")
self.pushButton_8 = QtWidgets.QPushButton(Form)
self.pushButton_8.setGeometry(QtCore.QRect(90, 230, 31, 26))
self.pushButton_8.setObjectName("pushButton_8")
self.pushButton_9 = QtWidgets.QPushButton(Form)
self.pushButton_9.setGeometry(QtCore.QRect(140, 230, 31, 26))
self.pushButton_9.setObjectName("pushButton_9")
self.pushButton_10 = QtWidgets.QPushButton(Form)
self.pushButton_10.setGeometry(QtCore.QRect(40, 270, 51, 26))
self.pushButton_10.setObjectName("pushButton_10")
self.pushButton_11 = QtWidgets.QPushButton(Form)
self.pushButton_11.setGeometry(QtCore.QRect(100, 270, 71, 26))
self.pushButton_11.setObjectName("pushButton_11")
self.pushButton_12 = QtWidgets.QPushButton(Form)
self.pushButton_12.setGeometry(QtCore.QRect(190, 180, 71, 21))
self.pushButton_12.setObjectName("pushButton_12")
self.pushButton_13 = QtWidgets.QPushButton(Form)
self.pushButton_13.setGeometry(QtCore.QRect(190, 210, 71, 21))
self.pushButton_13.setObjectName("pushButton_13")
self.pushButton_14 = QtWidgets.QPushButton(Form)
self.pushButton_14.setGeometry(QtCore.QRect(190, 240, 71, 21))
self.pushButton_14.setObjectName("pushButton_14")
self.pushButton_15 = QtWidgets.QPushButton(Form)
self.pushButton_15.setGeometry(QtCore.QRect(190, 270, 71, 21))
self.pushButton_15.setObjectName("pushButton_15")
self.textEdit = QtWidgets.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(40, 20, 221, 121))
self.textEdit.setObjectName("textEdit")
self.pushButton_16 = QtWidgets.QPushButton(Form)
self.pushButton_16.setGeometry(QtCore.QRect(190, 150, 71, 21))
self.pushButton_16.setObjectName("pushButton_16")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "计算器"))
self.pushButton.setText(_translate("Form", "1"))
self.pushButton_2.setText(_translate("Form", "2"))
self.pushButton_3.setText(_translate("Form", "3"))
self.pushButton_4.setText(_translate("Form", "4"))
self.pushButton_5.setText(_translate("Form", "5"))
self.pushButton_6.setText(_translate("Form", "6"))
self.pushButton_7.setText(_translate("Form", "7"))
self.pushButton_8.setText(_translate("Form", "8"))
self.pushButton_9.setText(_translate("Form", "9"))
self.pushButton_10.setText(_translate("Form", "0"))
self.pushButton_11.setText(_translate("Form", "="))
self.pushButton_12.setText(_translate("Form", "+"))
self.pushButton_13.setText(_translate("Form", "-"))
self.pushButton_14.setText(_translate("Form", "*"))
self.pushButton_15.setText(_translate("Form", "/"))
self.pushButton_16.setText(_translate("Form", "AC"))
2.1.py文件
from Ui_untitled import *
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from functools import partial
g_now_data=0
g_last_data=0
symbol = '+'
display_string = ''
#####################################################
# 计算数据库组件
#####################################################
def sql_num_event(display_event_ui,num):
global g_now_data
global g_last_data
global display_string
display_string = display_string+str(num)
g_now_data = g_now_data*10+int(num)
def sql_symbol_event(display_event_ui,sym):
global symbol
global g_now_data
global g_last_data
global display_string
if str(sym)=='=':
result = calculation_event(display_event_ui)
display_string=display_string+'\n'+str(sym)
display_string=display_string+str(result)
else:
g_last_data = calculation_event(display_event_ui)
g_now_data = 0
display_string=display_string+str(sym)
symbol=str(sym)
#####################################################
# 核心计算组件
#####################################################
def calculation_event(display_event_ui):
result = 0
if symbol == '+':
result = g_last_data + g_now_data
elif symbol == '-':
result = g_last_data - g_now_data
elif symbol == '*':
result = g_last_data * g_now_data
elif symbol == '/':
result = g_last_data / g_now_data
return result
#####################################################
# 计算界面显示事件请求
#####################################################
def display_event(display_event_ui):
display_event_ui.textEdit.setText(str(display_string))
def display_symbol_event(display_event_ui,sym):
sql_symbol_event(display_event_ui,sym)
display_event_ui.textEdit.setText(str(display_string))
def all_sql_display_event(display_event_ui,num):
sql_num_event(display_event_ui,num)
display_event(display_event_ui)
def all_AC_clear(display_event_ui,clear):
global symbol
global g_now_data
global g_last_data
global display_string
symbol = '+'
g_now_data = 0
g_last_data = 0
display_string = ''
display_event_ui.textEdit.setText(str(display_string))
#####################################################
# 按钮点击事件请求
#####################################################
def click_event_1(click_event_ui):
all_sql_display_event(click_event_ui,1)
return 1
def click_event_2(click_event_ui):
all_sql_display_event(click_event_ui,2)
return 2
def click_event_3(click_event_ui):
all_sql_display_event(click_event_ui,3)
return 3
def click_event_4(click_event_ui):
all_sql_display_event(click_event_ui,4)
return 4
def click_event_5(click_event_ui):
all_sql_display_event(click_event_ui,5)
return 5
def click_event_6(click_event_ui):
all_sql_display_event(click_event_ui,6)
return 6
def click_event_7(click_event_ui):
all_sql_display_event(click_event_ui,7)
return 7
def click_event_8(click_event_ui):
all_sql_display_event(click_event_ui,8)
return 8
def click_event_9(click_event_ui):
all_sql_display_event(click_event_ui,9)
return 9
def click_event_0(click_event_ui):
all_sql_display_event(click_event_ui,0)
return 0
def click_event_return(click_event_ui):
display_symbol_event(click_event_ui,'=')
return '='
def click_event_add(click_event_ui):
display_symbol_event(click_event_ui,'+')
return '+'
def click_event_reduce(click_event_ui):
display_symbol_event(click_event_ui,'-')
return '-'
def click_event_ride(click_event_ui):
display_symbol_event(click_event_ui,'*')
return '*'
def click_event_division(click_event_ui):
display_symbol_event(click_event_ui,'/')
return '/'
def click_event_clear(click_event_ui):
all_AC_clear(click_event_ui,'ac')
return 'ac'
#####################################################
# 按钮点击事件监控
#####################################################
def click_event_monitoring(click_ui):
click_ui.pushButton.clicked.connect(partial(click_event_1,click_ui))
click_ui.pushButton_2.clicked.connect(partial(click_event_2,click_ui))
click_ui.pushButton_3.clicked.connect(partial(click_event_3,click_ui))
click_ui.pushButton_4.clicked.connect(partial(click_event_4,click_ui))
click_ui.pushButton_5.clicked.connect(partial(click_event_5,click_ui))
click_ui.pushButton_6.clicked.connect(partial(click_event_6,click_ui))
click_ui.pushButton_7.clicked.connect(partial(click_event_7,click_ui))
click_ui.pushButton_8.clicked.connect(partial(click_event_8,click_ui))
click_ui.pushButton_9.clicked.connect(partial(click_event_9,click_ui))
click_ui.pushButton_10.clicked.connect(partial(click_event_0,click_ui))
click_ui.pushButton_11.clicked.connect(partial(click_event_return,click_ui))
click_ui.pushButton_12.clicked.connect(partial(click_event_add,click_ui))
click_ui.pushButton_13.clicked.connect(partial(click_event_reduce,click_ui))
click_ui.pushButton_14.clicked.connect(partial(click_event_ride,click_ui))
click_ui.pushButton_15.clicked.connect(partial(click_event_division,click_ui))
click_ui.pushButton_16.clicked.connect(partial(click_event_clear,click_ui))
#####################################################
# 主函数
#####################################################
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
w=Ui_Form()
w.setupUi(MainWindow)
click_event_monitoring(w) #点击事件监控
MainWindow.show()
sys.exit(app.exec_())
3.效果展示
详细教程之后再做更新