码迷,mamicode.com
首页 > 其他好文 > 详细

pyqt练习x5.0计算器

时间:2015-07-16 19:27:32      阅读:398      评论:0      收藏:0      [点我收藏+]

标签:

from __future__ import division

# -*- coding: utf-8 -*-

import sys,os,decimal,tempfile,math,time

from PyQt4 import QtGui,QtCore,Qt

 

class Jsj(QtGui.QWidget):

    #print‘正在加入命令中

    def __init__(self,parent=None):

        QtGui.QWidget.__init__(self)

 

        #创建窗口界面

        self.widget=QtGui.QWidget()

        self.setWindowTitle(u‘Bug个人简易计算器 v0.2‘)#界面名字

        self.setWindowIcon(QtGui.QIcon(‘E:\\pyqt\\file1\\untitled1\\pic.jpg‘))#界面图标

        self.setGeometry(300,600,600,300)#界面大小

        self.setToolTip(u‘这是一个计算器‘)#界面提示

        self.setStyleSheet("* { rgba(153,214,200,255) }")

 

        #新增背景颜色

        self.resize(600,400)

        #self.m1=QtGui.QLabel(u‘Bug个人简易计算器 v0.2‘,self)

        #self.m1.setGeometry(400,250,300,40)

        #self.m1.setStyleSheet("* { rgba(20,21,128,240) }")

 

        #创建按钮:运算按钮

        self.run1=QtGui.QPushButton(u‘运算(R)‘,self)

        self.run1.setGeometry(200,250,80,30)

        self.run1.setToolTip(u‘运算,Ctrl+R‘)

        self.run1.setShortcut(‘Ctrl+R‘)

        self.run1.setStyleSheet("* { rgba(67,142,219,240) }")

 

        #创建按钮:清除按钮

        self.run10=QtGui.QPushButton(u‘清除(C)‘,self)

        self.run10.setGeometry(60,250,80,30)

        self.run10.setToolTip(u‘清除,Alt+C‘)

        self.run10.setShortcut(‘Alt+C‘)

        self.run10.setStyleSheet("* { rgba(67,142,219,240) }")

 

        self.time1=QtCore.QTimer(self)

        self.connect(self.time1,QtCore.SIGNAL(‘timeout()‘),self.show1)

        self.time1.start(1000)

 

        #label标签:数字1

        self.run3=QtGui.QLabel(u‘数字1;‘,self)

        self.run3.setGeometry(20,20,80,30)

 

        #label标签:数字2

        self.run4=QtGui.QLabel(u‘数字2;‘,self)

        self.run4.setGeometry(20,80,80,30)

 

        #label标签:结果

        self.run5=QtGui.QLabel(u‘结果:‘,self)

        self.run5.setGeometry(20,150,80,30)

 

        #输入数据

        #第一个数

        self.run7=QtGui.QLineEdit(u‘请输入第一个数‘,self)

        self.run7.setGeometry(60,20,400,30)

 

        #查询记录

        self.jl=QtGui.QPushButton(u‘查询上一次记录‘,self)

        self.jl.setGeometry(10,360,100,40)

        self.jl.setToolTip(u‘查询上一次记录,Alt+K‘)

        self.jl.setShortcut(‘Alt+K‘)

        self.jl.setStyleSheet("* { rgba(67,142,219,240) }")

 

        self.about=QtGui.QPushButton(u‘关于本软件?暂时不支持本按钮功能‘,self)

        self.about.setGeometry(440,360,70,30)

        self.about.setToolTip(u‘关于本软件?暂时不支持本按钮功能‘)

        #第二个数

        self.run8=QtGui.QLineEdit(u‘请输入第二个数‘,self)

        self.run8.setGeometry(60,80,400,30)

        #第三个数

        self.run6=QtGui.QLineEdit(u‘‘,self)

        self.run6.setGeometry(60,150,400,30)

 

        #用来显示符号,比如:+,-*等,现在用单选按钮

        self.runj0=QtGui.QCheckBox(u‘加法(+)‘,self)

        self.runj0.setGeometry(480,0,80,30)

 

        self.runj1=QtGui.QCheckBox(u‘减法(-)‘,self)

        self.runj1.setGeometry(480,20,80,30)

 

        self.runj2=QtGui.QCheckBox(u‘乘法(*)‘,self)

        self.runj2.setGeometry(480,40,80,30)

 

        self.runj3=QtGui.QCheckBox(u‘除法(/)‘,self)

        self.runj3.setGeometry(480,60,80,30)

 

        self.runj4=QtGui.QCheckBox(u‘除法(%)‘,self)

        self.runj4.setGeometry(480,80,80,30)

 

        self.runj5=QtGui.QCheckBox(u‘根号(sqrt)‘,self)

        self.runj5.setGeometry(480,100,80,30)

 

        self.runj6=QtGui.QCheckBox(u‘负数(Negative)暂停此功能无法使用‘,self)

        self.runj6.setGeometry(480,120,800,30)

 

        self.connect(self.run10, QtCore.SIGNAL("clicked()"),self.clearMask)

 

        #显示动态时间

        self.datateime1=QtGui.QLineEdit(u‘请按下回车显示时间:‘,self)

        self.datateime1.setGeometry(10,310,300,30)

        #其他说明

        #添加制作人员名单

        self.renyuan=QtGui.QLabel(u‘制作人员及程度:Bug,1‘,self)

        self.renyuan.setGeometry(400,180,800,30)

 

        #其他说明

        #添加制作软件版本

        self.renyuan=QtGui.QLabel(u‘制作软件及版本:python2.7+pyqt4.0‘,self)

        self.renyuan.setGeometry(400,205,800,30)

 

        #其他说明

        #添加联系方式

        self.lxfs=QtGui.QLabel(u‘邮箱:mhxy13867806343@live.cn‘,self)

        self.lxfs.setGeometry(400,230,800,30)

 

        #其他说明

        #添加其他说明:

        self.qtfs=QtGui.QLabel(u‘其他内容;暂无其他内容‘,self)

        self.qtfs.setGeometry(400,255,800,30)

 

        #制作年份

        self.year=QtGui.QLabel(u‘制作年份;2013-9-18‘,self)

        self.year.setGeometry(400,285,800,30)

 

        #更新年份

        self.year=QtGui.QLabel(u‘更新年份;2013-9-26‘,self)

        self.year.setGeometry(400,315,800,30)

 

        self.run6.selectAll()

        self.run7.selectAll()

        self.run8.selectAll()

        self.run7.setFocus()

 

        #单选符号

        self.buttonBox=QtGui.QButtonGroup(None)#None

        self.buttonBox.addButton(self.runj0,1)#加法

        self.buttonBox.addButton(self.runj1,2)#减法

        self.buttonBox.addButton(self.runj2,3)#乘法

        self.buttonBox.addButton(self.runj3,4)#除法

        self.buttonBox.addButton(self.runj4,5)#复数

        self.buttonBox.addButton(self.runj5,6)#复数

        self.buttonBox.addButton(self.runj6,7)#复数

 

        self.la=QtGui.QLineEdit(u‘(暂时不能使用这个功能和查询功能也不能使用)‘,self)

        self.la.setGeometry(120,360,300,40)

 

        #对单选事件信号进行说明

        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("buttonClicked(int)"), self.buttonJudge)

 

        self.connect(self.run1, QtCore.SIGNAL("clicked()"), self.mark)

        #self.connect(self.run1, QtCore.SIGNAL("clicked()"), self.float_mark)

 

        self.connect(self.run1, QtCore.SIGNAL("clicked()"), self.ChongShu)

        #self.connect(self.run1, QtCore.SIGNAL("clicked()"), self.floatChongShu)

 

        self.connect(self.buttonBox,QtCore.SIGNAL("clicked()"), self.buttonBoxx)

 

        #第二个单选框的

        self.connect(self.run1,QtCore.SIGNAL("clicked()"), self.error)

        self.connect(self.run1,QtCore.SIGNAL("clicked()"), self.buttonBoxx)

 

    def mouse1(self,e):

        if e.button()==Qt.leftButton:

            self.d1=e.globalPos()-self.frameGeometry().topLeft()

            e.accept()

        if e.button==Qt.RightButton:

            self.close()

    def mouse2(self,e):

        if e.buttons()&Qt.leftButton:

            self.move(e.globalPos()-self.dragPosition)

            e.accept()

    def show1(self):

        time1=QtCore.QTime.currentTime()

        data1=Qt.QDate.currentDate()

        text=data1.toString(‘yyyy-MM-dd‘)+‘  ‘+time1.toString(‘hh:mm:ss‘)

        #print text

        self.datateime1.setText(str(text))

        #self.datateime1.setText(text)

    def addButton(self):

        path=‘E:\\pyqt\\2013year9month1\\file1\\log.log‘

        f=open(path,‘r‘)

        if f.read() in ‘‘:

            self.la.setText( u‘当前无历史记录‘)

            print  QtGui.QMessageBox.information(self,‘Remind:‘,self.tr(‘No record‘))

            f.close()

    def write1(self):

        path=‘E:\\pyqt\\2013year9month1\\file1\\log.log‘

        f=open(path,‘w‘)

        if f.writelines([self.run7.text(),self.run8.text(),self.run5.text()]):

            print‘ff‘

            self.la.setText([self.run7.text(),self.run8.text(),self.run5.text()])

            f.close()

    #现在对按钮和那些符号进行控制

    #先对self.run7.text(),self.run8.text()进行判断,比如1+2的时候,就显示加法

    def mark(self):

        if self.runj0.isChecked():

           t1=decimal.Decimal(str(self.run7.text()))

           t2=decimal.Decimal(str(self.run8.text()))

           t3=t1+t2

           self.run6.setText(str(t3))

 

        elif self.runj1.isChecked():

            t1=decimal.Decimal(str(self.run7.text()))

            t2=decimal.Decimal(str(self.run8.text()))

            t3=t1-t2

            self.run6.setText(str(t3))

 

        elif self.runj2.isChecked():

            t1=decimal.Decimal(str(self.run7.text()))

            t2=decimal.Decimal(str(self.run8.text()))

            t3=t1*t2

            self.run6.setText(str(t3))

        elif self.runj3.isChecked():

            t1=decimal.Decimal(str(self.run7.text()))

            t2=decimal.Decimal(str(self.run8.text()))

            t3=t1/t2

            self.run6.setText(str(t3))

        elif self.runj4.isChecked():

            t1=decimal.Decimal(str(self.run7.text()))

            t2=decimal.Decimal(str(self.run8.text()))

            t3=t1%t2

            self.run6.setText(str(t3))

        elif self.runj5.isChecked():

            t1=decimal.Decimal(str(self.run7.text()))

            t2=decimal.Decimal(str(self.run8.text()))

            t3=math.sqrt(t1+t2)

            self.run6.setText(str(t3))

        elif self.runj6.isChecked():

            t1=decimal.Decimal(str(self.run7.text()))

            t2=decimal.Decimal(str(self.run8.text()))

            t3=-(t1+t2)

            self.run6.setText(str(t3))

    #这部分单选用来选择符号

    def buttonJudge(self):

        if self.buttonBox.checkedId()==1:

            print u"加法+"

        if self.buttonBox.checkedId()==2:

            print u"减法-"

        if self.buttonBox.checkedId()==3:

            print u"乘法*"

        if self.buttonBox.checkedId()==4:

            print u‘除法/‘

        if self.buttonBox.checkedId()==5:

            print u‘除法%‘

 

        if self.buttonBox.checkedId()==6:

            print u‘sqrt(根号)‘

 

    #判断输入的数据类型

    def clearMask(self):

        if  self.connect(self.run10, QtCore.SIGNAL("clicked()"),self.clearMask):

            self.run8.clear()

            #self.run6.clear()

            self.run7.clear()

            self.la.clear()

            #time.sleep(1)

            print u‘清除成功

            #print u‘清除时间:‘,time.strftime(‘%d-%m-%Y %H:%M:%S‘,time.localtime(time.time()))

            #print u‘请继续运算你需要的数据类型

    def close1(self):

        if self.connect(self.run2, QtCore.SIGNAL("clicked()"),self.close1):

            #print u‘退出时间:‘,time.strftime(‘%d-%m-%Y %H:%M:%S‘,time.localtime(time.time()))

            print u‘退出程序!‘

            #print u‘谢谢使用!‘

            #print u‘欢迎下次再次使用本软件

            os._exit(self.run2.close())

            #time.sleep(1)

 

    def closeEvent(self, event):

        reply = QtGui.QMessageBox.question(self, u‘提示(X)‘,

                                           u"是否要退出?", QtGui.QMessageBox.Yes,

                                           QtGui.QMessageBox.No)

        if reply == QtGui.QMessageBox.Yes:

            event.accept()

        else:

            event.ignore()

 

    #如果查询完了,要给用户一个提示,是否要清除之后 ,继续?

 

    #这部分是处理异常,因为用户可能会输入不是数字的

    #现在来设置对数字1或者数字2,或者2个出现的除数字之后的,如果不是数字,就出现错误,然后请重新输入

    def ChongShu(self):

        try:

            if decimal.Decimal(str(self.run7.text())) or  decimal.Decimal(str(self.run8.text())):

                pass

        except ValueError:

            print u‘invalid literal:‘,decimal.Decimal(str(self.run7.text())),decimal.Decimal(str(self.run8.text()))

            print‘开始清除非法数据

            self.run8.clear()

            self.run7.clear()

            print u‘清除非法数据

            print‘请继续运算

 

    #当未选中单选框时,提示错误

    def buttonBoxx(self):

        try:

            if self.buttonBox.checkedId():

                print u‘当前选中的是‘,self.buttonBox.checkedId()

        except Exception:

            print ‘error:‘,u‘未选中

 

 

    #全部信息都显示在界面上

    def error(self):

        try:

            if decimal.Decimal(str(self.run7.text())) or decimal.Decimal(str(self.run8.text())):

                pass

        except Exception:

            print  QtGui.QMessageBox.information(self,‘error message:‘,self.tr(‘invalid literal‘))

 

app=QtGui.QApplication(sys.argv)

b=Jsj()

b.show()

sys.exit(app.exec_())

技术分享

pyqt练习x5.0计算器

标签:

原文地址:http://www.cnblogs.com/mhxy13867806343/p/4156377.html

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