码迷,mamicode.com
首页 > 编程语言 > 详细

Python简单计算器

时间:2016-05-07 09:52:42      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

-- coding: utf-8 --

Form implementation generated from reading ui file ‘Calculator.ui’

#

Created: Fri Apr 22 12:33:04 2016

by: PyQt4 UI code generator 4.9.6

#

WARNING! All changes made in this file will 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", "计算器", 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(_translate("Calculator", "7", None))
    self.pushButtonNum8.setText(_translate("Calculator", "8", None))
    self.pushButtonNum9.setText(_translate("Calculator", "9", None))
    self.pushButtonMul.setText(_translate("Calculator", "*", None))
    self.pushButtonNum4.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.pushButtonNum0.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

继承的类要和生成的ui对相

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下面是所有按键的信号

    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.operator == ‘+‘:  #如果原运算符为‘+’则进行加法运算并将结果存入第一目、将新运算符保存到运算符变量
        temp = float(self.valueOne)+float(self.valueTwo)
        self.history = self.valueOne+self.operator+self.valueTwo
        self.operator = ‘ ‘
        self.valueOne = ‘‘            
        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)
        self.history = self.valueOne+self.operator+self.valueTwo
        self.operator = ‘ ‘
        self.valueOne = ‘‘            
        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)
        self.history = self.valueOne+self.operator+self.valueTwo
        self.operator = ‘ ‘
        self.valueOne = ‘‘            
        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)
        self.history = self.valueOne+self.operator+self.valueTwo
        self.operator = ‘ ‘
        self.valueOne = ‘‘            
        self.valueTwo = ‘‘
        self.valueOut = str(temp)        
        self.valueOut = 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’且原第一目也为‘0’则只保留一个‘0’(就是数字不可能会出现‘00’)
            self.valueOne = ‘0‘
        elif value != ‘.‘ and self.valueOne == ‘0‘:#如果原第一目为‘0’而新输入的值不是小数点则清除原数以新输入数据代替(数字可能会出现‘0.8’、‘8’但不应该出现‘08’)
            self.valueOne = value
        else: 
            self.valueOne += value #如果不是上述两种情况则将输入数字直接加到第一目末尾
        self.OutputView(self.valueOne)
    else:                       #如果此时已经有了运算符则判定现在正在进行第二目输入
        if value == ‘0‘ and self.valueTwo == ‘0‘: #以下判断过程与第一目输入时相同,应保证数字输入时不出现‘00’、‘08’等情况
            self.valueTwo = ‘0‘
        elif value != ‘.‘ and self.valueTwo == ‘0‘:
            self.valueTwo = value
        else:
            self.valueTwo += value 
        self.OutputView(self.valueTwo)

def GetOperator(self,ope):      #获取运算符号
    if self.valueOne == ‘‘ and self.valueOut == ‘‘: #如果尚未输入第一目且无上次计算结果,则输入运算符没有意义,不进行任何操作
        pass
    elif self.valueOne == ‘‘ and self.valueOut != ‘‘:#如果尚未输入第一目而有上次计算结果,则将上次计算的结果作为第一目,并保存运算符
        self.valueOne = self.valueOut
        self.operator = ope       

    elif self.valueTwo == ‘‘:#如果尚未输入第二个数
        self.operator = ope#直接变更运算符
        self.history = self.valueOne+self.operator
        self.OutputView(self.valueOne)
    elif self.operator == ‘+‘: #如果原来有运算符且有两个运算目则进行运算,将结果保存为新的第一运算目,并保存新的运算符
        temp = float(self.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)
        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)
        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)
        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)
    else:
        pass        
def DeleteLastZeroWithDot(self,data): #清除小数点后多余的‘0’(例如函数带入参数250.0,返回结果为250)
    backData = data
    if(‘.‘ in data):
        if(data[-1] is ‘0‘):
            backData = data[0:-2]
    return backData
def OutputView(self,value): #显示输出函数
    self.ui.textEditResult.setText(value+‘\r\n‘+self.history)
def keyPressEvent(self, event): #获取键盘输入的消息(数字输入尚有问题)
        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简单计算器

标签:

原文地址:http://blog.csdn.net/sumuzhe1511/article/details/51332846

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!