您好,欢迎访问一九零五行业门户网

python3.5 + PyQt5 +Eric6实现的一个计算器方法(附代码)

这篇文章主要介绍了python3.5 + pyqt5 +eric6实现的一个计算器方法(附代码),在windows7 32位系统可以完美运行 计算器,有兴趣的可以了解一下。
目前可以实现简单的计算。计算前请重置,设计的时候默认数字是0,学了半天就做出来个这么个结果,bug不少。 python3.5 + pyqt5 +eric6 在windows7 32位系统可以完美运行 计算器,简单学了半天就画个图实现的存在bug,部分按钮还未实现,后续优化。
代码结构如图:
jisuan.py
import re #匹配整数或小数的乘除法,包括了开头存在减号的情况 mul_p=re.compile("(-?\d+)(\.\d+)?(\*|/)(-?\d+)(\.\d+)?") #匹配整数或小数的加减法,包括了开头存在减号的情况 plus_minus = re.compile("(-?\d+)(\.\d+)?(-|\+)(-?\d+)(\.\d+)?") #匹配括号 bracket=re.compile("\([^()]*\)") #匹配乘法的时候出现乘以负数的情况,包括了开头存在减号的情况 mul_minus_minus = re.compile("(-?\d+)(\.\d+)?(\*-)(\d+)(\.\d+)?") #匹配除法的时候出现乘以负数的情况,包括了开头存在减号的情况 p_minus_minus = re.compile("(-?\d+)(\.\d+)?(/-)(\d+)(\.\d+)?") #定义一个两位数的加减乘除法的运算,匹配左边的右边的数字和左边的数字,然后进行计算 def touble_cale(str_expire): if str_expire.count("+") == 1: right_num = float(str_expire[(str_expire.find("+")+1):]) left_num = float(str_expire[:str_expire.find("+")]) return str(right_num+left_num) elif str_expire[1:].count("-") == 1: right_num = float(str_expire[:str_expire.find("-",1)]) left_num = float(str_expire[(str_expire.find("-", 1) + 1):]) return str(right_num - left_num) elif str_expire.count("*") == 1: right_num = float(str_expire[:str_expire.find("*")]) left_num = float(str_expire[(str_expire.find("*")+1):]) return str(right_num * left_num) elif str_expire.count("/") == 1: right_num = float(str_expire[:str_expire.find("/")]) left_num = float(str_expire[(str_expire.find("/") + 1):]) return str(right_num / left_num) #定义一个方法用于判断是否存在乘以负数和除以负数的情况 def judge_mul_minus(str_expire): #判断公式中乘以负数的部分 if len(re.findall("(\*-)", str_expire)) != 0: #调用上面的正则取得*-的公式 temp_mul_minus = mul_minus_minus.search(str_expire).group() #将匹配的部分的*-换成*并将-放到前面 temp_mul_minus_2 = temp_mul_minus.replace(temp_mul_minus,"-" + temp_mul_minus.replace("*-","*")) #经更改的的部分与原来的部分进行替换 str_expire=str_expire.replace(temp_mul_minus,temp_mul_minus_2) return judge_mul_minus(str_expire) #return str_expire # 判断公式中除以负数的部分 elif len(re.findall(r"(/-)", str_expire)) != 0: # 调用上面的正则取得/-的公式 temp_dev_minus = p_minus_minus.search(str_expire).group() # 将匹配的部分的/-换成/并将-放到前面 temp_dev_minus_2 = temp_dev_minus.replace(temp_dev_minus,"-" + temp_dev_minus.replace("/-","/")) # 经更改的的部分与原来的部分进行替换 str_expire = str_expire.replace(temp_dev_minus,temp_dev_minus_2) return judge_mul_minus(str_expire) #调用change_sign将公式中的++换成= +-换成- return change_sign(str_expire) #定义一个方法取将--更改为+ +-改为- def change_sign(str_expire): if len(re.findall(r"(\+-)", str_expire)) != 0: str_expire = str_expire.replace("+-", "-") return change_sign(str_expire) elif len(re.findall(r"(--)", str_expire)) != 0: str_expire = str_expire.replace("--", "+") return change_sign(str_expire) return str_expire #定义一个方法用于计算只有加减乘除的公式,优先处理乘法 def cale_mix(str_expire): #如果公式中出现符号数字的情况即+5 -6 *8 /8的这种情况直接放回数字否则则先计算乘除在处理加减 while len(re.findall("[-+*/]",str_expire[1:])) != 0: if len(re.findall("(\*|/)",str_expire)) != 0: str_expire = str_expire.replace(mul_p.search(str_expire).group(),touble_cale(mul_p.search(str_expire).group())) elif len(re.findall("(\+|-)",str_expire)) !=0: str_expire = str_expire.replace(plus_minus.search(str_expire).group(),touble_cale(plus_minus.search(str_expire).group())) return str_expire #定义一个方法用于去括号,并调用上述的方法进行计算 def remove_bracket(str_expire): #判断公式中是否有括号 if len(bracket.findall(str_expire)) == 0: return cale_mix(judge_mul_minus(str_expire)) elif len(bracket.findall(str_expire))!=0: while len(bracket.findall(str_expire)) !=0: #print(bracket.search(str_expire).group()) #只有存在括号优先处理括号中的内容并对内容进行替换,直到没有括号位置 str_expire = str_expire.replace(bracket.search(str_expire).group(),cale_mix(judge_mul_minus(bracket.search(str_expire).group()[1:-1]))) str_expire = cale_mix(judge_mul_minus(str_expire)) return str_expire if name == "main": while true: user_input_expire = input("请输入你的公式:(不要带空格,q表示退出):") print("%s=%s" %(user_input_expire,remove_bracket(user_input_expire))) continue
untitled.py
# -*- coding: utf-8 -*- from pyqt5.qtcore import * from pyqt5.qtwidgets import * from pyqt5 import qtcore, qtgui, qtwidgets from ui_untitled import ui_dialog from jisuan import remove_bracket class dialog(qdialog, ui_dialog): def init(self, parent=none): super(dialog, self).init(parent) self.setupui(self) @pyqtslot() def on_button_6_clicked(self): self.edit_xianshi.insertplaintext('6') @pyqtslot() def on_button_2_clicked(self): self.edit_xianshi.insertplaintext('2') @pyqtslot() def on_button_3_clicked(self): self.edit_xianshi.insertplaintext('3') @pyqtslot() def on_button_pingfang_clicked(self): me=self.edit_xianshi.toplaintext() m=int(me) *int(me) self.edit_xianshi.clear() self.edit_xianshi.append(str(m)) @pyqtslot() def on_button_add_clicked(self): h=self.edit_xianshi.toplaintext() self.edit_xianshi.clear() self.edit_xianshi.append(h+'+') @pyqtslot() def on_button_jian_clicked(self): h = self.edit_xianshi.toplaintext() self.edit_xianshi.clear() self.edit_xianshi.append(h + '-') @pyqtslot() def on_button_9_clicked(self): self.edit_xianshi.insertplaintext('9') @pyqtslot() def on_button_chu_clicked(self): h = self.edit_xianshi.toplaintext() self.edit_xianshi.clear() self.edit_xianshi.append(h + '/') @pyqtslot() def on_button_cheng_clicked(self): h = self.edit_xianshi.toplaintext() self.edit_xianshi.clear() self.edit_xianshi.append(h + '*') @pyqtslot() def on_button_8_clicked(self): self.edit_xianshi.insertplaintext('8') @pyqtslot() def on_button_4_clicked(self): self.edit_xianshi.insertplaintext('4') @pyqtslot() def on_button_esc_clicked(self): self.edit_xianshi.clear() @pyqtslot() def on_button_7_clicked(self): self.edit_xianshi.insertplaintext('7') @pyqtslot() def on_button_1_clicked(self): self.edit_xianshi.insertplaintext('1') @pyqtslot() def on_button_5_clicked(self): self.edit_xianshi.insertplaintext('5') @pyqtslot() def on_button_xiaoshu_clicked(self): self.edit_xianshi.insertplaintext('.') @pyqtslot() def on_button_0_clicked(self): self.edit_xianshi.insertplaintext('0') @pyqtslot() def on_button_dengyu_clicked(self): pe=self.edit_xianshi.toplaintext() m=remove_bracket(pe) self.edit_xianshi.clear() self.edit_xianshi.append(str(m)) def on_button_fenzhi_clicked(self): pe = self.edit_xianshi.toplaintext() if int(pe) ==0: qmessagebox.information(self,u'提示',u'零不能作为分母') dialog() else: m=1/(int(pe)) self.edit_xianshi.clear() self.edit_xianshi.append(str(m)) dialog() if name =="main": import sys app = qtwidgets.qapplication(sys.argv) app.processevents() ui = dialog() ui.show() sys.exit(app.exec_())
ui_untitled.py
# -*- coding: utf-8 -*- # form implementation generated from reading ui file 'c:\users\administrator\desktop\pyqt5\untitled.ui' # # created by: pyqt5 ui code generator 5.5 # # warning! all changes made in this file will be lost! from pyqt5 import qtcore, qtgui, qtwidgets class ui_dialog(object): def setupui(self, dialog): dialog.setobjectname("dialog") dialog.resize(357, 320) dialog.setstylesheet("font: 75 16pt \"aharoni\";\n" "background-color: rgb(206, 255, 251);") self.label = qtwidgets.qlabel(dialog) self.label.setgeometry(qtcore.qrect(201, 210, 301, 21)) self.label.settext("") self.label.setobjectname("label") self.edit_xianshi = qtwidgets.qtextedit(dialog) self.edit_xianshi.setgeometry(qtcore.qrect(0, 0, 351, 41)) self.edit_xianshi.setstylesheet("font: 75 16pt \"aharoni\";") self.edit_xianshi.setobjectname("edit_xianshi") self.gridlayoutwidget = qtwidgets.qwidget(dialog) self.gridlayoutwidget.setgeometry(qtcore.qrect(0, 30, 351, 281)) self.gridlayoutwidget.setobjectname("gridlayoutwidget") self.gridlayout = qtwidgets.qgridlayout(self.gridlayoutwidget) self.gridlayout.setobjectname("gridlayout") self.button_6 = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_6.setobjectname("button_6") self.gridlayout.addwidget(self.button_6, 2, 2, 1, 1) self.button_2 = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_2.setobjectname("button_2") self.gridlayout.addwidget(self.button_2, 3, 1, 1, 1) self.button_3 = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_3.setobjectname("button_3") self.gridlayout.addwidget(self.button_3, 3, 2, 1, 1) self.button_fenzhi = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_fenzhi.setobjectname("button_fenzhi") self.gridlayout.addwidget(self.button_fenzhi, 1, 3, 1, 1) self.button_pingfang = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_pingfang.setobjectname("button_pingfang") self.gridlayout.addwidget(self.button_pingfang, 0, 3, 1, 1) self.button_add = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_add.setobjectname("button_add") self.gridlayout.addwidget(self.button_add, 2, 3, 1, 1) self.button_jian = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_jian.setobjectname("button_jian") self.gridlayout.addwidget(self.button_jian, 3, 3, 1, 1) self.button_9 = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_9.setobjectname("button_9") self.gridlayout.addwidget(self.button_9, 1, 2, 1, 1) self.button_chu = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_chu.setobjectname("button_chu") self.gridlayout.addwidget(self.button_chu, 0, 2, 1, 1) self.button_cheng = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_cheng.setobjectname("button_cheng") self.gridlayout.addwidget(self.button_cheng, 0, 1, 1, 1) self.button_8 = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_8.setobjectname("button_8") self.gridlayout.addwidget(self.button_8, 1, 1, 1, 1) self.button_4 = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_4.setobjectname("button_4") self.gridlayout.addwidget(self.button_4, 2, 0, 1, 1) self.button_esc = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_esc.setobjectname("button_esc") self.gridlayout.addwidget(self.button_esc, 0, 0, 1, 1) self.button_7 = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_7.setobjectname("button_7") self.gridlayout.addwidget(self.button_7, 1, 0, 1, 1) self.button_1 = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_1.setobjectname("button_1") self.gridlayout.addwidget(self.button_1, 3, 0, 1, 1) self.button_5 = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_5.setobjectname("button_5") self.gridlayout.addwidget(self.button_5, 2, 1, 1, 1) self.pushbutton_17 = qtwidgets.qpushbutton(self.gridlayoutwidget) self.pushbutton_17.settext("") self.pushbutton_17.setobjectname("pushbutton_17") self.gridlayout.addwidget(self.pushbutton_17, 4, 0, 1, 1) self.button_xiaoshu = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_xiaoshu.setobjectname("button_xiaoshu") self.gridlayout.addwidget(self.button_xiaoshu, 4, 1, 1, 1) self.button_0 = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_0.setstylesheet("") self.button_0.setobjectname("button_0") self.gridlayout.addwidget(self.button_0, 4, 2, 1, 1) self.button_dengyu = qtwidgets.qpushbutton(self.gridlayoutwidget) self.button_dengyu.setobjectname("button_dengyu") self.gridlayout.addwidget(self.button_dengyu, 4, 3, 1, 1) self.retranslateui(dialog) qtcore.qmetaobject.connectslotsbyname(dialog) def retranslateui(self, dialog): _translate = qtcore.qcoreapplication.translate dialog.setwindowtitle(_translate("dialog", "dialog")) self.edit_xianshi.sethtml(_translate("dialog", "<!doctype html public \"-//w3c//dtd html 4.0//en\" \"http://www.w3.org/tr/rec-html40/strict.dtd\">\n" "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n" "p, li { white-space: pre-wrap; }\n" "</style></head><body style=\" font-family:\'aharoni\'; font-size:16pt; font-weight:72; font-style:normal;\">\n" "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'simsun\'; font-weight:400;\">0</span></p></body></html>")) self.button_6.settext(_translate("dialog", "6")) self.button_2.settext(_translate("dialog", "2")) self.button_3.settext(_translate("dialog", "3")) self.button_fenzhi.settext(_translate("dialog", "1/^")) self.button_pingfang.settext(_translate("dialog", "^2")) self.button_add.settext(_translate("dialog", "+")) self.button_jian.settext(_translate("dialog", "-")) self.button_9.settext(_translate("dialog", "9")) self.button_chu.settext(_translate("dialog", "/")) self.button_cheng.settext(_translate("dialog", "*")) self.button_8.settext(_translate("dialog", "8")) self.button_4.settext(_translate("dialog", "4")) self.button_esc.settext(_translate("dialog", "esc")) self.button_7.settext(_translate("dialog", "7")) self.button_1.settext(_translate("dialog", "1")) self.button_5.settext(_translate("dialog", "5")) self.button_xiaoshu.settext(_translate("dialog", ".")) self.button_0.settext(_translate("dialog", "0")) self.button_dengyu.settext(_translate("dialog", "=")) if name == "main": import sys app = qtwidgets.qapplication(sys.argv) dialog = qtwidgets.qdialog() ui = ui_dialog() ui.setupui(dialog) dialog.show() sys.exit(app.exec_())
效果图:
以上就是python3.5 + pyqt5 +eric6实现的一个计算器方法(附代码)的详细内容。
其它类似信息

推荐信息