Python Simple Calculator

Source: Internet
Author: User

- -Coding:utf-8--form implementation generated from reading UI file ' Calculator.ui '

#

Created:fri Apr 12:33:04 2016by:pyqt4 UI code generator 4.9.6

#

warning! All changes made in this file would be lost!

ui_calculator.py
From PYQT4 import Qtcore, Qtgui

Try
_fromutf8 = QtCore.QString.fromUtf8
Except Attributeerror:
def _fromutf8 (s):
return s
Try
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate (context, text, disambig):
Return QtGui.QApplication.translate (context, text, disambig, _encoding)
Except Attributeerror:
def _translate (context, text, disambig):
Return QtGui.QApplication.translate (context, text, disambig)

Class Ui_calculator (object):
def setupui (self, Calculator):
Calculator.setobjectname (_fromutf8 ("Calculator"))
Calculator.resize (201, 250)
icon = Qtgui.qicon ()
Icon.addpixmap (Qtgui.qpixmap (_fromutf8 ("Icon.ico")), QtGui.QIcon.Normal, QTGUI.QICON.OFF)
Calculator.setwindowicon (Icon)
Calculator.settooltip (_fromutf8 (""))
Calculator.setwindowfilepath (_fromutf8 (""))
Self.widgetkeys = Qtgui.qwidget (Calculator)
Self.widgetKeys.setGeometry (Qtcore.qrect (0, 50, 201, 201))
Self.widgetKeys.setObjectName (_fromutf8 ("Widgetkeys"))
Self.pushbuttonce = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonCE.setGeometry (qtcore.qrect (0, 0, 51, 41))
Self.pushButtonCE.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonCE.setObjectName (_fromutf8 ("Pushbuttonce"))
Self.pushbuttonc = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonC.setGeometry (Qtcore.qrect (50, 0, 51, 41))
Self.pushButtonC.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonC.setObjectName (_fromutf8 ("Pushbuttonc"))
Self.pushbuttonbac = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonBac.setGeometry (qtcore.qrect (100, 0, 51, 41))
Self.pushButtonBac.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonBac.setObjectName (_fromutf8 ("Pushbuttonbac"))
Self.pushbuttondiv = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonDiv.setGeometry (Qtcore.qrect (150, 0, 51, 41))
Self.pushButtonDiv.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonDiv.setObjectName (_fromutf8 ("Pushbuttondiv"))
Self.pushbuttonnum7 = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonNum7.setGeometry (Qtcore.qrect (0, 40, 51, 41))
Self.pushButtonNum7.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonNum7.setObjectName (_fromutf8 ("PushButtonNum7"))
SELF.PUSHBUTTONNUM8 = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonNum8.setGeometry (Qtcore.qrect (50, 40, 51, 41))
Self.pushButtonNum8.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonNum8.setObjectName (_fromutf8 ("PUSHBUTTONNUM8"))
SELF.PUSHBUTTONNUM9 = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonNum9.setGeometry (Qtcore.qrect (100, 40, 51, 41))
Self.pushButtonNum9.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonNum9.setObjectName (_fromutf8 ("PushButtonNum9"))
Self.pushbuttonmul = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonMul.setGeometry (Qtcore.qrect (150, 40, 51, 41))
Self.pushButtonMul.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonMul.setObjectName (_fromutf8 ("Pushbuttonmul"))
SELF.PUSHBUTTONNUM4 = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonNum4.setGeometry (Qtcore.qrect (0, 80, 51, 41))
Self.pushButtonNum4.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonNum4.setObjectName (_fromutf8 ("PushButtonNum4"))
SELF.PUSHBUTTONNUM5 = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonNum5.setGeometry (Qtcore.qrect (50, 80, 51, 41))
Self.pushButtonNum5.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonNum5.setObjectName (_fromutf8 ("PUSHBUTTONNUM5"))
SELF.PUSHBUTTONNUM6 = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonNum6.setGeometry (Qtcore.qrect (100, 80, 51, 41))
Self.pushButtonNum6.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonNum6.setObjectName (_fromutf8 ("PUSHBUTTONNUM6"))
Self.pushbuttonsub = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonSub.setGeometry (Qtcore.qrect (150, 80, 51, 41))
Self.pushButtonSub.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonSub.setObjectName (_fromutf8 ("Pushbuttonsub"))
SELF.PUSHBUTTONNUM1 = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonNum1.setGeometry (qtcore.qrect (0, 120, 51, 41))
Self.pushButtonNum1.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonNum1.setObjectName (_fromutf8 ("PushButtonNum1"))
self.pushbuttonnum2 = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonNum2.setGeometry (Qtcore.qrect (50, 120, 51, 41))
Self.pushButtonNum2.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonNum2.setObjectName (_fromutf8 ("pushButtonNum2"))
SELF.PUSHBUTTONNUM3 = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonNum3.setGeometry (Qtcore.qrect (100, 120, 51, 41))
Self.pushButtonNum3.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonNum3.setObjectName (_fromutf8 ("pushButtonNum3"))
Self.pushbuttonadd = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonAdd.setGeometry (Qtcore.qrect (150, 120, 51, 41))
Self.pushButtonAdd.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonAdd.setObjectName (_fromutf8 ("Pushbuttonadd"))
Self.pushbuttondot = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonDot.setGeometry (qtcore.qrect (0, 160, 51, 41))
Self.pushButtonDot.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonDot.setObjectName (_fromutf8 ("Pushbuttondot"))
SELF.PUSHBUTTONNUM0 = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonNum0.setGeometry (Qtcore.qrect (50, 160, 51, 41))
Self.pushButtonNum0.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonNum0.setObjectName (_fromutf8 ("PUSHBUTTONNUM0"))
self.pushbuttonequ = Qtgui.qpushbutton (Self.widgetkeys)
Self.pushButtonEqu.setGeometry (Qtcore.qrect (100, 160, 101, 41))
Self.pushButtonEqu.setFocusPolicy (QtCore.Qt.NoFocus)
Self.pushButtonEqu.setObjectName (_fromutf8 ("pushbuttonequ"))
Self.texteditresult = Qtgui.qtextedit (Calculator)
Self.textEditResult.setGeometry (qtcore.qrect (0, 0, 201, 51))
Self.textEditResult.setReadOnly (True)
Self.textEditResult.setTextInteractionFlags (QtCore.Qt.TextSelectableByMouse)
Self.textEditResult.setObjectName (_fromutf8 ("Texteditresult"))

    Self.retranslateui (Calculator) QtCore.QMetaObject.connectSlotsByName (Calculator) def retranslateui (self, Calculator): Calculator.setwindowtitle (_translate ("Calculator", "Calculator", None)) Self.pushButtonCE.setText (_translate    ("Calculator", "CE", None)) Self.pushButtonC.setText (_translate ("Calculator", "C", None)) Self.pushButtonBac.setText (_translate ("Calculator", "<-", none)) Self.pushButtonDiv.setText (_translate ("Calculator", "/", None)) Self.pushButtonNum7.setText (_transl Ate ("Calculator", "7", none)) Self.pushButtonNum8.setText (_translate ("Calculator", "8", none)) Self.pushbuttonnum9.s Ettext (_translate ("Calculator", "9", none)) Self.pushButtonMul.setText (_translate ("Calculator", "*", none)) Self.pus Hbuttonnum4.settext (_translate ("Calculator", "4", None)) Self.pushButtonNum5.setText (_translate ("Calculator", "5", None)) Self.pushButtonNum6.setText (_translate ("Calculator", "6", None)) Self.pushButtonSub.setText (_translate (" Calculator ","-", None)) Self.pushButtonNum1.setText (_translate ("Calculator", "1", None)) Self.pushButtonNum2.setText (_translate (" Calculator "," 2 ", none)) Self.pushButtonNum3.setText (_translate (" Calculator "," 3 ", none)) Self.pushButtonAdd.setText (_translate ("Calculator", "+", none)) Self.pushButtonDot.setText (_translate ("Calculator", ".", none)) Self.pushbutto Nnum0.settext (_translate ("Calculator", "0", none)) Self.pushButtonEqu.setText (_translate ("Calculator", "=", none))

calculator.py

- -Coding:utf-8--

“””
Created on Thu Apr 21 11:11:58 2016

@author: Administrator
“””
Import Sys
From PYQT4 import Qtgui
From PYQT4 import Qtcore
From Ui_calculator import Ui_calculator
Sys.argv.append (' Calculator ')

Try
_fromutf8 = QtCore.QString.fromUtf8
Except Attributeerror:
_fromutf8 = Lambda s:s

The inherited class is to be associated with the generated UI pair.

Class Calculator (Qtgui.qdialog):
Valueone = ' 0 ' #运算符号前数值变量
operator = "#运算符号变量
Valuetwo = "#运算符号后数值变量
Valueout = "#结果数值变量
History = "#历史纪录变量
def Init(self, Parent=none):
Super (Calculator, self). Init (parent)

    self.ui = Ui_Calculator() #实例化图形界面类
#########################################################################
    #设置窗口图标,下面两句实现任务栏图标与窗口图标一致    import ctypes    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("Calculator")       self.ui.setupUi(self)    #设置显示框初始值0    self.ui.textEditResult.setText(self.valueOne)            #固定窗口大小不变    self.setFixedSize(201, 250)    self.ui.pushButtonAdd.setShortcut(‘+‘)
Signals, here's the signal for all the keys.
    Self.ui.pushButtonCE. Clicked.connect (self. ACTIONPUSHCE) Self.ui.pushButtonC. Clicked.connect (self. ACTIONPUSHC) Self.ui.pushButtonBac.clicked.connect (self. ACTIONPUSHBAC) Self.ui.pushButtonAdd.clicked.connect (self. Actionpushadd) Self.ui.pushButtonDiv.clicked.connect (self. ACTIONPUSHDIV) Self.ui.pushButtonDot.clicked.connect (self. ACTIONPUSHDOT) Self.ui.pushButtonEqu.clicked.connect (self. actionpushequ) Self.ui.pushButtonMul.clicked.connect (self. Actionpushmul) Self.ui.pushButtonSub.clicked.connect (self. Actionpushsub) Self.ui.pushButtonNum0.clicked.connect (self. ACTIONPUSHNUM0) Self.ui.pushButtonNum1.clicked.connect (self. ACTIONPUSHNUM1) Self.ui.pushButtonNum2.clicked.connect (self. ACTIONPUSHNUM2) Self.ui.pushButtonNum3.clicked.connect (self. ACTIONPUSHNUM3) Self.ui.pushButtonNum4.clicked.connect (self. ACTIONPUSHNUM4) Self.ui.pushButtonNum5.clicked.connect (self. ACTIONPUSHNUM5) Self.ui.pushButtonNum6.clicked.connect (self. ACTIONPUSHNUM6) self.uI.pushbuttonnum7.clicked.connect (self. ACTIONPUSHNUM7) Self.ui.pushButtonNum8.clicked.connect (self. ACTIONPUSHNUM8) Self.ui.pushButtonNum9.clicked.connect (self. ACTIONPUSHNUM9) def actionpushce (self): #删除 if self.valuetwo! = ': Self.valuetwo = ' self. Outputview (self.valuetwo) elif self.operator! = ": Self.operator = ' self. Outputview (self.valueone) Else:self.valueOne = ' 0 ' self.    Outputview (Self.valueone) def ACTIONPUSHC (self): #清除 Self.valueone = ' 0 ' self.operator = ' Self.valuetwo = ' Self.valueout = ' self '.         Outputview (Self.valueone) def Actionpushbac (self): #退格 if Self.valuetwo! = ": Self.valuetwo = self.valuetwo[0:-1] Self. Outputview (self.valuetwo) elif self.operator! = ": Self.operator = ' self. Outputview (self.valueone) elif Self.valueone! = ": Self.valueone = self.valueone[0:-1] self. Outputview (Self.valueone) def actionpushadd (self): #加 self. GEtoperator (' + ') def actionpushdiv (self): #除以 self. Getoperator ('/') def Actionpushmul (self): #乘以 self. Getoperator (' * ') def actionpushsub (self): #减 self. Getoperator ('-') def Actionpushdot (self): #点 self. Getavalue ('. ') def ACTIONPUSHNUM0 (self): self. Getavalue (' 0 ') def ActionPushNum1 (self): self. Getavalue (' 1 ') def ActionPushNum2 (self): self. Getavalue (' 2 ') def ActionPushNum3 (self): self. Getavalue (' 3 ') def ActionPushNum4 (self): self. Getavalue (' 4 ') def ACTIONPUSHNUM5 (self): self. Getavalue (' 5 ') def ACTIONPUSHNUM6 (self): self. Getavalue (' 6 ') def ActionPushNum7 (self): self. Getavalue (' 7 ') def ACTIONPUSHNUM8 (self): self. Getavalue (' 8 ') def ActionPushNum9 (self): self. Getavalue (' 9 ') def actionpushequ (self): #等于 if Self.operator is ": #如果没有运算符则不进行任何运算 pass Elif Self.operato r = = ' + ': #如果原运算符为 ' + ' to perform addition operations and save the result to the first goal, saving the new operator to the operator variable temp = float (self.valueone) +float (self.valuetwo) self.h istory = Self.valueone+self.operator+self.valuetwo Self.operator = ' Self.valueone = ' Self.valuetwo = ' self.valueout = str (temp) SEL F.valueout = self. Deletelastzerowithdot (self.valueout) self. Outputview (self.valueout) elif Self.operator = = '-': #如果原运算符为 '-' then subtract and save the result to the first goal, saving the new operator to the operator variable temp = float (sel F.valueone)-float (self.valuetwo) self.history = self.valueone+self.operator+self.valuetwo Self.operator = ' ' Self.valueone = ' Self.valuetwo = ' self.valueout = str (temp) self.val Ueout = self. Deletelastzerowithdot (self.valueout) self. Outputview (self.valueout) elif Self.operator = = ' * ': #如果原运算符为 ' * ' is multiplied and the result is stored in the first order, the new operator is saved to the operator variable temp = float (sel F.valueone) *float (self.valuetwo) self.history = self.valueone+self.operator+self.valuetwo Self.operator = ' ' Self.valueone = ' Self.valuetwo = ' self.valueout = str (temp) self.val Ueout = self. DeleTelastzerowithdot (self.valueout) self. Outputview (self.valueout) elif Self.operator = = '/': #如果原运算符为 '/' then divides and saves the result to the first order, saving the new operator to the operator variable temp = float (sel F.valueone)/float (self.valuetwo) self.history = self.valueone+self.operator+self.valuetwo Self.operator = ' ' Self.valueone = ' Self.valuetwo = ' self.valueout = str (temp) self.val Ueout = self. Deletelastzerowithdot (self.valueout) self.  Outputview (self.valueout) else:pass def getavalue (self,value): #获取一个输入的数字并根据实际情况将其分配到第一目或者第二目 if Self.operator = = ": #如果还没有运算符输入则判断当前正在进行第一目输入 if value = = ' 0 ' and self.valueone = = ' 0 ': #如果输入为 ' 0 ' and the original first eye is ' 0 ' only Keep a ' 0 ' (that is, the number is not likely to appear ' xx ') Self.valueone = ' 0 ' elif value! = '. ' and self.valueone = = ' 0 ': #如果原第一目为 ' 0 ' and the newly entered value Not a decimal point clears the original number to replace with the new input data (the number may appear ' 0.8 ', ' 8 ' but should not appear ') Self.valueone = value Else:self.valueOne + =       Value #如果不是上述两种情况则将输入数字直接加到第一目末尾 Self. Outputview (Self.valueone) Else: #如果此时已经有了运算符则判定现在正在进行第二目输入 if value = = ' 0 ' and Self.valuet Wo = = ' 0 ': #以下判断过程与第一目输入时相同, should ensure that the digital input does not appear ' 00 ', ' 08 ' and other cases self.valuetwo = ' 0 ' elif value! = '. ' And Self.value both = = ' 0 ': Self.valuetwo = value Else:self.valueTwo + = value self. Outputview (Self.valuetwo) def getoperator (self,ope): #获取运算符号 if Self.valueone = = "and Self.valueout = =": #如果尚未输 The input operator is meaningless and does not take any action if you enter the first eye without the last calculated result Pass elif Self.valueone = = "and Self.valueout! =": #如果尚未输入第一目而有上次计算结果, the last computed The result acts as the first goal, and the save operator Self.valueone = self.valueout Self.operator = ope elif Self.valuetwo = = ": #如果尚未输入第 Two number self.operator = ope# Direct change operator self.history = Self.valueone+self.operator self. Outputview (self.valueone) elif Self.operator = = ' + ': #如果原来有运算符且有两个运算目则进行运算, save the result as a new first operand, and save the new operator temp = float (s Elf.valueone) +float (self.valuetwo) self.history = Self.valueone+self.operator+self.valuetwo Self.operator = ope self.valueone = str (temp) Self.valuetwo = "Self.valueout = str (temp) self.valueout = self. Deletelastzerowithdot (self.valueout) self. Outputview (self.valueout) elif Self.operator = = '-': temp = float (self.valueone)-float (self.valuetwo) sel                    F.history = Self.valueone+self.operator+self.valuetwo Self.operator = ope self.valueone = str (temp) Self.valuetwo = "Self.valueout = str (temp) self.valueout = self. Deletelastzerowithdot (self.valueout) self. Outputview (self.valueout) elif Self.operator = = ' * ': temp = float (self.valueone) *float (self.valuetwo) sel                    F.history = Self.valueone+self.operator+self.valuetwo Self.operator = ope self.valueone = str (temp) Self.valuetwo = "Self.valueout = str (temp) self.valueout = self. DeleteLastzerowithdot (self.valueout) self. Outputview (self.valueout) elif Self.operator = = '/': temp = float (self.valueone)/float (self.valuetwo) sel                    F.history = Self.valueone+self.operator+self.valuetwo Self.operator = ope self.valueone = str (temp) Self.valuetwo = "Self.valueout = str (temp) self.valueout = self. Deletelastzerowithdot (self.valueout) self. Outputview (self.valueout) else:pass def deletelastzerowithdot (self,data): #清除小数点后多余的 ' 0 ' (for example, the function takes into parameter 250.0, returns result = Backdata = Data if ('. ' in data): if (data[-1] was ' 0 '): backdata = Data[0:-2] return back Datadef Outputview (self,value): #显示输出函数 self.ui.textEditResult.setText (value+ ' \ r \ n ' +self.history) def keypressevent (Self, event): #获取键盘输入的消息 (There is a problem with the digital input) if event.key () = = QtCore.Qt.Key_Escape:self.close () if event . Key () = = QtCore.Qt.Key_Backspace:self. ACTIONPUSHBAC () if event. Key () = = QtCore.Qt.Key_Delete:self. ACTIONPUSHCE () if event.key () = = QtCore.Qt.Key_0:self. ACTIONPUSHNUM0 () if event.key () = = QtCore.Qt.Key_1:self. ACTIONPUSHNUM1 () if event.key () = = QtCore.Qt.Key_2:self. ACTIONPUSHNUM2 () if event.key () = = QtCore.Qt.Key_3:self. ACTIONPUSHNUM3 () if event.key () = = QtCore.Qt.Key_4:self. ACTIONPUSHNUM4 () if event.key () = = QtCore.Qt.Key_5:self. ACTIONPUSHNUM5 () if event.key () = = QtCore.Qt.Key_6:self. ACTIONPUSHNUM6 () if event.key () = = QtCore.Qt.Key_7:self. ACTIONPUSHNUM7 () if event.key () = = QtCore.Qt.Key_8:self. ACTIONPUSHNUM8 () if event.key () = = QtCore.Qt.Key_9:self.            ACTIONPUSHNUM9 () if event.key () = = qtcore.qt.key_period:#. Self. Actionpushdot () if event.key () = = qtcore.qt.key_enter:#= self. Actionpushequ () if event.key () = = Qtcore.qt.key_plus:#+ self. Actionpushadd () if event.key () = = qtcore.qt.key_minus:#-self. Actionpushsub () if event.key () = = qtcore.qt.key_asterisk:#* self. Actionpushmul () if event.key () = = qtcore.qt.key_slash:#/self. Actionpushdiv ()
#########################################################################

If name = = 'main':

app = QtGui.QApplication(sys.argv)window = Calculator()window.show()sys.exit(app.exec_())
END

Python Simple Calculator

Related Article

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.