PyQt实战之计算器的实现
2015-07-17 10:23
696 查看
一.启动并进入eric主界面,如下图:
鼠标右键,”打开方式”选择“pthon.exe”
二.eric的主界面如下图所示:
选择“Project”–>”New”,设置如下图所示
点击“OK“,可以看到在source这一栏有个init.py这个文件,如下图所示
我们双击查看这个文件,发现这个init.py是个空文件,里面什么内容都没有。事实上这个[b]init.py的作用只是让整个工程成为Python的一个包而已[/b]。
三.选择”Form”选项卡
在空白处,鼠标右键选择”New Form“,弹出来一个框,如下图:
点击下拉列表后,
”随便“给这个新建的form取个名字,就可以在form选项卡下看到这个新建
的form了,如下图所示:
同时QT设计师也会自动打开,供你编辑这个.ui文件,然后拖动按钮,最终的效果图如下图所示:
”保存全部“之后回到eric,右键点击calc1.ui,选择compile form
回到source选项卡,发现多了一个文件
这个文件就是刚才calc1.ui编译生成的文件,我们双击这个Ui_cal1.py可以看到它的内容,然后按F2运行,也可以直接进到Ui_cal1.py所在的目录,用python.exe运行,这两种方法都可以。运行效果如下:
注意:这个编译生成的Ui_cal1.py我们是不能修改的,因为他是编译自动生成的代码,即使你修改了到下次编译后你所做的修改也会全部丢失。实际上官方也不建议我们修改这个文件,在这个.py文件中,我们可以看到这样一句话:
,翻译成中文就是”所有的修改都会被丢失“(请允许我卖弄一下自己的英文^_^)。
四.添加自定义代码
既然在那个文件中我们不能添加代码,那我应该在哪里添加自己的代码呢?
我们还是回到Form选项卡,右键点击calc1.ui(请原谅我无法截图),选择”Generate Dialog Code”,这个时候会弹出这样一个框,如图
我们单击New,设置好类名、文件名和路径后,点击OK即可,如下图
我们分别给每一个按钮添加slot(不明白slot的话建议先去学一下QT哦^_^),如下图所示:
,添加好后,点击OK即可。
在回到这个source选项卡,我们就可以看到又多了一个py文件,如下图所示:
我们可以在这个文件中添加代码了,并且在这个文件中可以看到刚才我们添加的slot了。
计算器的代码很简单,这里直接上代码咯。
保存后,按F2就可以运行咯,虽然这个计算器功能不多,但还是给大家看一下效果吧。
额,该死的静态图片。
视频教程见:http://www.duobei.com/course/6238671512
鼠标右键,”打开方式”选择“pthon.exe”
二.eric的主界面如下图所示:
选择“Project”–>”New”,设置如下图所示
点击“OK“,可以看到在source这一栏有个init.py这个文件,如下图所示
我们双击查看这个文件,发现这个init.py是个空文件,里面什么内容都没有。事实上这个[b]init.py的作用只是让整个工程成为Python的一个包而已[/b]。
三.选择”Form”选项卡
在空白处,鼠标右键选择”New Form“,弹出来一个框,如下图:
点击下拉列表后,
”随便“给这个新建的form取个名字,就可以在form选项卡下看到这个新建
的form了,如下图所示:
同时QT设计师也会自动打开,供你编辑这个.ui文件,然后拖动按钮,最终的效果图如下图所示:
”保存全部“之后回到eric,右键点击calc1.ui,选择compile form
回到source选项卡,发现多了一个文件
这个文件就是刚才calc1.ui编译生成的文件,我们双击这个Ui_cal1.py可以看到它的内容,然后按F2运行,也可以直接进到Ui_cal1.py所在的目录,用python.exe运行,这两种方法都可以。运行效果如下:
注意:这个编译生成的Ui_cal1.py我们是不能修改的,因为他是编译自动生成的代码,即使你修改了到下次编译后你所做的修改也会全部丢失。实际上官方也不建议我们修改这个文件,在这个.py文件中,我们可以看到这样一句话:
,翻译成中文就是”所有的修改都会被丢失“(请允许我卖弄一下自己的英文^_^)。
四.添加自定义代码
既然在那个文件中我们不能添加代码,那我应该在哪里添加自己的代码呢?
我们还是回到Form选项卡,右键点击calc1.ui(请原谅我无法截图),选择”Generate Dialog Code”,这个时候会弹出这样一个框,如图
我们单击New,设置好类名、文件名和路径后,点击OK即可,如下图
我们分别给每一个按钮添加slot(不明白slot的话建议先去学一下QT哦^_^),如下图所示:
,添加好后,点击OK即可。
在回到这个source选项卡,我们就可以看到又多了一个py文件,如下图所示:
我们可以在这个文件中添加代码了,并且在这个文件中可以看到刚才我们添加的slot了。
计算器的代码很简单,这里直接上代码咯。
# -*- coding: utf-8 -*- """ Module implementing Calc. """ import sys from PyQt4 import QtGui, QtCore from PyQt4.QtGui import QWidget from PyQt4.QtCore import pyqtSignature from Ui_Calc import Ui_Form class Calc(QWidget, Ui_Form): """ Class documentation goes here. """ num = 0 num2 = 0 add = False sub = False mul = False div = False def __init__(self, parent = None): """ Constructor """ QWidget.__init__(self, parent) self.setupUi(self) @pyqtSignature("") def on_button1_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError if self.add == False and self.sub == False and self.mul == False and self.div == False: self.num = self.num*10 + 1 self.resultLabel.setText(str(self.num)) else: self.num2 = self.num2*10 + 1 self.resultLabel.setText(str(self.num2)) @pyqtSignature("") def on_button2_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError if self.add == False and self.sub == False and self.mul == False and self.div == False: self.num = self.num*10 + 2 self.resultLabel.setText(str(self.num)) else: self.num2 = self.num2*10 + 2 self.resultLabel.setText(str(self.num2)) @pyqtSignature("") def on_button0_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError if self.add == False and self.sub == False and self.mul == False and self.div == False: self.num = self.num*10 + 0 self.resultLabel.setText(str(self.num)) else: self.num2 = self.num2*10 + 0 self.resultLabel.setText(str(self.num2)) @pyqtSignature("") def on_button3_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError if self.add == False and self.sub == False and self.mul == False and self.div == False: self.num = self.num*10 + 3 self.resultLabel.setText(str(self.num)) else: self.num2 = self.num2*10 + 3 self.resultLabel.setText(str(self.num2)) @pyqtSignature("") def on_button4_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError if self.add == False and self.sub == False and self.mul == False and self.div == False: self.num = self.num*10 + 4 self.resultLabel.setText(str(self.num)) else: self.num2 = self.num2*10 + 4 self.resultLabel.setText(str(self.num2)) @pyqtSignature("") def on_button5_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError if self.add == False and self.sub == False and self.mul == False and self.div == False: self.num = self.num*10 + 5 self.resultLabel.setText(str(self.num)) else: self.num2 = self.num2*10 + 5 self.resultLabel.setText(str(self.num2)) @pyqtSignature("") def on_button6_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError if self.add == False and self.sub == False and self.mul == False and self.div == False: self.num = self.num*10 + 6 self.resultLabel.setText(str(self.num)) else: self.num2 = self.num2*10 + 6 self.resultLabel.setText(str(self.num2)) @pyqtSignature("") def on_button7_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError if self.add == False and self.sub == False and self.mul == False and self.div == False: self.num = self.num*10 + 7 self.resultLabel.setText(str(self.num)) else: self.num2 = self.num2*10 + 7 self.resultLabel.setText(str(self.num2)) @pyqtSignature("") def on_button8_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError if self.add == False and self.sub == False and self.mul == False and self.div == False: self.num = self.num*10 + 8 self.resultLabel.setText(str(self.num)) else: self.num2 = self.num2*10 + 8 self.resultLabel.setText(str(self.num2)) @pyqtSignature("") def on_button9_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError if self.add == False and self.sub == False and self.mul == False and self.div == False: self.num = self.num*10 + 9 self.resultLabel.setText(str(self.num)) else: self.num2 = self.num2*10 + 9 self.resultLabel.setText(str(self.num2)) @pyqtSignature("") def on_addBtn_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError self.add = True self.sub = False self.mul = False self.div = False self.resultLabel.setText("+") @pyqtSignature("") def on_subBtn_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError self.add = False self.sub = True self.mul = False self.div = False self.resultLabel.setText("-") @pyqtSignature("") def on_mulBtn_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError self.add = False self.sub = False self.mul = True self.div = False self.resultLabel.setText("X") @pyqtSignature("") def on_divBtn_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError self.add = False self.sub = False self.mul = False self.div = True self.resultLabel.setText("/") @pyqtSignature("") def on_equBtn_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError if self.add == True: self.resultLabel.setText(str(self.num+self.num2)) elif self.sub == True: self.resultLabel.setText(str(self.num-self.num2)) elif self.mul == True: self.resultLabel.setText(str(self.num*self.num2)) elif self.div == True: if self.num2 == 0: self.resultLabel.setText(u"除数不能为0") else: self.resultLabel.setText(str(self.num/self.num2)) @pyqtSignature("") def on_clnBtn_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet #raise NotImplementedError self.add = False self.sub = False self.mul = False self.div = False self.num = 0 self.num2 = 0 self.resultLabel.setText("") if __name__ == "__main__": app = QtGui.QApplication(sys.argv) calc = Calc() calc.show() sys.exit(app.exec_())
保存后,按F2就可以运行咯,虽然这个计算器功能不多,但还是给大家看一下效果吧。
额,该死的静态图片。
视频教程见:http://www.duobei.com/course/6238671512
相关文章推荐
- Qt笔记——MOC(莫克)
- (一) Qt Model/View 的简单说明
- Qt连接Sql server错误ConnectionOpen (Connect()). QODBC3: Unable to connect"
- pyQt不同窗体间的值传递(二)——使用信号槽机制
- Qt通过ODBC来操作Excel
- Visual Studio下Qt调用IDL
- ubuntu下QT与opencv的配置安装
- QT POST/GET 发送/获取网页数据
- qt5 mouseless 多子页面切换测试
- Qt5 mouseless 测试
- Qt数据库:(一)简介
- win8 64bit/win7 32bit/winxp 32bit +QT+opencv环境配置教程,亲测成功
- ArcGIS QT开发:ArcGIS Runtime dll不能被QT识别问题
- mac osx上为qt应用生成debug symbol
- qt中qDebug()无法输出解决办法
- Qt读写INI配置文件
- Qt下 QString转char*
- QT 应用程序自动重启
- Qt学习之路之ODBC访问SQLserver数据库
- Qt浅谈之二十九Qt多线程