This article mainly introduces the python3.5 + PyQt5 +ERIC6 Implementation of a calculator method (with code), in the Windows7 32-bit system can run the calculator perfectly, interested can understand.
A simple calculation can now be achieved. Before the calculation, please reset the default number of the design is 0, learned a half-day to make a result, a lot of bugs. python3.5 + PyQt5 +eric6 in the Windows7 32-bit system can be perfect to run the calculator, simple to learn a half-day to draw a picture implementation of the existence of the bug, some buttons have not been implemented, follow-up optimization.
Code structure
jisuan.py
Import re# matches the multiplication method of integers or decimals, including the case where there is a minus sign at the beginning Mul_p=re.compile ("(-?\d+) (\.\d+)?" ( \*|/) (-?\d+) (\.\d+)? ") #匹配整数或小数的加减法, including the case where there is a minus sign at the beginning Plus_minus = Re.compile ("(-?\d+) (\.\d+)? ( -|\+) (-?\d+) (\.\d+)? ") #匹配括号bracket =re.compile ("\ ([^ ()]*\)") #匹配乘法的时候出现乘以负数的情况, including the case where there is a minus sign at the beginning Mul_minus_minus = Re.compile (-?\d+) (\.\d+)? ( \*-) (\d+) (\.\d+)? ") #匹配除法的时候出现乘以负数的情况, including the case where there is a minus sign at the beginning P_minus_minus = Re.compile ("(-?\d+) (\.\d+)? ( /-) (\d+) (\.\d+)? ") #定义一个两位数的加减乘除法的运算, match the left-hand number to the left and then calculate the 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+le Ft_num) elif Str_expire[1:].count ("-") = = 1:right_num = Float (Str_expire[:str_expire.find ("-", 1)]) Left_num = Floa T (str_expire[(Str_expire.find ("-", 1) + 1):]) return str (right_num-left_num) elif str_expire.count ("*") = = 1:rig Ht_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) #定义一个方法用于判断是否存在乘以负数和除以负数 Situation def Judge_mul_minus (Str_expire): #判断公式中乘以负数的部分 If Len (Re.findall ("(\*-)", str_expire))! = 0: #调用上面的正则取得 *-Formula Te Mp_mul_minus = Mul_minus_minus.search (str_expire). Group () #将匹配的部分的 * * Change to * and put-to front temp_mul_minus_2 = Temp_mul_minus.rep Lace (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 # Determine the part of the formula divided by negative elif Len (Re.finda LL (r "(/-)", str_expire))! = 0: # Call the above regular get/-Formula Temp_dev_minus = P_minus_minus.search (str_expire). Group () # will match the part Temp_dev_minus_2 = Temp_dev_minus.replace (Temp_dev_minus, "-" + temp_dev_minus.replace ("/-", "/")) # has been Replace the part with the original part Str_expIRE = Str_expire.replace (temp_dev_minus,temp_dev_minus_2) return Judge_mul_minus (str_expire) #调用change_sign将公式中的 + + Replace = + +--return change_sign (Str_expire) #定义一个方法取将--Change to + + + to-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_EXPI re# defines a method for calculating only subtraction formulas, preferentially dealing with multiplication def cale_mix (str_expire): This case of #如果公式中出现符号数字的情况即 +5-6 *8/8 is put back directly to the number otherwise the multiplication is calculated first in processing the plus and minus while Len ( Re.findall ("[-+*/]", str_expire[1:]) = 0:if len (Re.findall ("(\*|/)", str_expire))! = 0:str_expire = STR_EXPIRE.R Eplace (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# defines a method for using the parentheses and calls the above method to calculate the Def remove_bracket (str_expire): #判断公式中是否有括号 If Len (Bracket.findall (str_expire)) = = 0:r Eturn 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 ()) #只有存在括号优先处理括号中的内容并对内容进行替换 until there is no parenthesis position Str_expire = s Tr_expire.replace (Bracket.search (str_expire) group (), Cale_mix (Judge_mul_minus (bracket.search). Group () [1:-1])) Str_expire = Cale_mix (Judge_mul_minus (Str_expire)) return str_expireif name = = "Main": While True:u Ser_input_expire = input ("Please enter your formula: (Do not take a space, q means exit):") 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, QTWIDGETSFR Om ui_untitled import ui_dialogfrom Jisuan import Remove_bracketclass Dialog (Qdialog, Ui_dialog): Def init (self, parent=n One): 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 ' hint ', u ' 0 cannot be the denominator ') 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.proces Sevents () 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 would be Lost!from PyQt5 import Qtcore, Qtgui, Qtwidgetsclass ui_dialog (object): Def setupui (Self, Dialog): Dialog.setobjectname ("Dialog") dialog.resize (357,) dialog.setstylesheet ("font:75 16pt \" Ahar Oni\ "; \ n" "Background-color:rgb (206, 255, 251);") Self.label = Qtwidgets.qlabel (Dialog) self.label.setGeometry (Qtcore.qrect (201,, 301,)) Self.label.setText ("") Self.label.setObjectName ("label") self. Edit_xianshi = Qtwidgets.qtextedit (Dialog) self. Edit_xianshi.setgeometry (qtcore.qrect (0, 0, 351,)) 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, 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 retranslate Ui (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" "
: