Python3.5 + PyQt5 + Eric6 is a calculator (with code)

Source: Internet
Author: User
This article mainly introduces a calculator code implemented by python3.5 + PyQt5 + Eric6. the calculator can be run perfectly in the windows732-bit system. if you are interested, please take a look. This article mainly introduces a calculator method (with code) implemented by python3.5 + PyQt5 + Eric6. the calculator can be run perfectly in the Windows 7 32-bit system. if you are interested, please take a look.

Currently, simple computing can be implemented. Reset before calculation. the default number during design is 0. after learning for half a day, we made such a result, with many bugs. Python3.5 + PyQt5 + Eric6 can run the calculator perfectly in the 32-bit Windows 7 system. after a simple learning for half a day, there is a bug in drawing implementation. some buttons have not yet been implemented and will be optimized in the future.

Code structure

Jisuan. py

Import re # match the multiplication and division of integers or decimal places, including the case where there is a minus sign at the beginning of mul_p = re. compile ("(-? \ D +) (\. \ d + )? (\ * | /)(-? \ D +) (\. \ d + )? ") # Match the addition and subtraction of integers or decimal places, including the minus signs at the beginning. plus_minus = re. compile ("(-? \ D +) (\. \ d + )? (-| \ + )(-? \ D +) (\. \ d + )? ") # Matching brackets bracket = re. compile ("\ ([^ ()] * \)") # The multiplication result is multiplied by a negative number, including the minus sign at the beginning. mul_minus_minus = re. compile ("(-? \ D +) (\. \ d + )? (\ *-) (\ D +) (\. \ d + )? ") # When the division is matched, it is multiplied by a negative number, including the minus sign at the beginning. p_minus_minus = re. compile ("(-? \ D +) (\. \ d + )? (/-) (\ D +) (\. \ d + )? ") # Define a two-digit addition, subtraction, multiplication, division, matching the number on the right and the number on the left, and then calculate 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):]) retur N 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) # defines a method to determine whether a method exists by multiplying the number and division In the case of a negative number, def judge_mul_minus (str_expire): # determine the part of the formula multiplied by a negative number if len (re. findall ("(\ *-)", str_expire ))! = 0: # Call the regular expression above to obtain the *-formula temp_mul_minus = mul_minus_minus.search (str_expire ). group () # Replace *-with * and put-in front of temp_mul_minus_2 = temp_mul_minus.replace (temp_mul_minus, "-" + temp_mul_minus.replace ("*-","*")) # Replace the modified part with the original part str_expire = reverse (temp_mul_minus, temp_mul_minus_2) return second (str_expire) # return str_expire # elif len (re. findall (r "(/-)", str_expire ))! = 0: # Call the regular expression above to obtain the/-formula temp_dev_minus = p_minus_minus.search (str_expire ). group () # Replace/-with/And put-in front of temp_dev_minus_2 = temp_dev_minus.replace (temp_dev_minus, "-" + temp_dev_minus.replace ("/-","/")) # Replace the modified part with the original part str_expire = str_expire.replace (temp_dev_minus, temp_dev_minus_2) return judge_mul_minus (str_expire) # call change_sign to replace ++ in the formula with = +-with-return change_sign (str_expire) # define a method and change -- 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_expire # defines a method for calculating formulas with only addition, subtraction, multiplication, division, multiplication def cale_mix (str_expire) is prioritized ): # If there is a symbol number in the formula, that is, + 5-6*8/8, this is the case that the number is directly put back. otherwise, calculate the multiplication and division before processing the addition and subtraction 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 # define a method used to remove parentheses and call the preceding method to calculate def remove_bracket (str_expire): # determine whether the formula contains parentheses 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 () # only the content in the brackets is prioritized and replaced with the content until there is no bracket position 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_expireif name = "main": while True: user_input_expire = input ("Enter your formula: (do not include space, q indicates exit):") print ("% s = % s" % (user_input_expire, remove_bracket (user_input_expire) continue

Untitled. py

#-*-Coding: UTF-8-*-from PyQt5.QtCore import * from export import * from PyQt5 import QtCore, QtGui, QtWidgetsfrom export import Ui_Dialogfrom jisuan import Export 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_shortclicked (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' prompt ', U' zero cannot be used as 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. processEvents () ui = Dialog () ui. show () sys.exit(app.exe c _())

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, QtWidgetsclass 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", "\n""
 \n""

0

")) 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_())

:

The above is the details of a calculator method (with code) implemented by python3.5 + PyQt5 + Eric6. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.