使用工具eric6
安装包地址:
http://eric-ide.python-projects.org/eric-download.html
1.需要安装python和pyqt为前提,然后安装eric
2.安装好后在 C:\Python27\Lib\site-packages\eric6 路径下找到 eric6.pyw ,打开eric6
3.第一次打开需要设置API等参数
4.使用qt编辑器开发ui界面
5.在eric内ui文件上右键编译窗体
6.生成对话框代码
计算器测试程序
# -*- coding: utf-8 -*-
"""
Module implementing Dialog_calc.
"""
from PyQt4.QtCore import pyqtSignature
from PyQt4.QtGui import QDialog
from Ui_UI_calc import Ui_Dialog
from PyQt4 import QtCore, QtGui
import sys
class Dialog_calc(QDialog, Ui_Dialog):
"""
Class documentation goes here.
"""
lcdstring = ''
operation = ''
currentNum = 0
prevlousNum = 0
result = 0
def __init__(self, parent=None):
"""
Constructor
@param parent reference to the parent widget
@type QWidget
"""
QDialog.__init__(self, parent)
self.setupUi(self)
self.action()
def action(self):
#定义信号和槽关系
#按下数字执行的方法
self.connect(self.pb0, QtCore.SIGNAL('clicked()'), self.buttonClicked)
self.connect(self.pb1, QtCore.SIGNAL('clicked()'), self.buttonClicked)
self.connect(self.pb2, QtCore.SIGNAL('clicked()'), self.buttonClicked)
self.connect(self.pb3, QtCore.SIGNAL('clicked()'), self.buttonClicked)
self.connect(self.pb4, QtCore.SIGNAL('clicked()'), self.buttonClicked)
self.connect(self.pb5, QtCore.SIGNAL('clicked()'), self.buttonClicked)
self.connect(self.pb6, QtCore.SIGNAL('clicked()'), self.buttonClicked)
self.connect(self.pb7, QtCore.SIGNAL('clicked()'), self.buttonClicked)
self.connect(self.pb8, QtCore.SIGNAL('clicked()'), self.buttonClicked)
self.connect(self.pb9, QtCore.SIGNAL('clicked()'), self.buttonClicked)
self.connect(self.pb_point, QtCore.SIGNAL('clicked()'), self.buttonClicked)
#按下操作执行的方法
self.connect(self.pb_plus, QtCore.SIGNAL('clicked()'), self.opClicked)
self.connect(self.pb_sub, QtCore.SIGNAL('clicked()'), self.opClicked)
self.connect(self.pb_mul, QtCore.SIGNAL('clicked()'), self.opClicked)
self.connect(self.pb_div, QtCore.SIGNAL('clicked()'), self.opClicked)
#按下清除执行的方法
self.connect(self.pb_clear, QtCore.SIGNAL('clicked()'), self.clearClicked)
#按下等于号执行的方法
self.connect(self.pb_eq, QtCore.SIGNAL('clicked()'), self.eqClicked)
def buttonClicked(self):
#最多显示8位
# print len(Dialog_calc.lcdstring)
if len(Dialog_calc.lcdstring) <= 7 :
Dialog_calc.lcdstring += self.sender().text()
#第一个输入的是小数点时候替替换为0.
if Dialog_calc.lcdstring == '.':
Dialog_calc.lcdstring = '0.'
#去掉多次输入小数点时候的错误
elif str(Dialog_calc.lcdstring).count('.') > 1:
Dialog_calc.lcdstring = str(Dialog_calc.lcdstring)[:-1]
self.lcd.display(Dialog_calc.lcdstring)
Dialog_calc.currentNum = float(Dialog_calc.lcdstring)
def opClicked(self):
Dialog_calc.prevlousNum = Dialog_calc.currentNum
Dialog_calc.currentNum = 0
Dialog_calc.lcdstring = ''
Dialog_calc.operation = self.sender().objectName()
def clearClicked(self):
Dialog_calc.lcdstring = ''
Dialog_calc.operation = ''
Dialog_calc.currentNum = 0
Dialog_calc.prevlousNum = 0
Dialog_calc.result = 0
self.lcd.display('0')
def eqClicked(self):
if Dialog_calc.operation == 'pb_plus':
Dialog_calc.result = Dialog_calc.prevlousNum + Dialog_calc.currentNum
self.lcd.display(Dialog_calc.result)
elif Dialog_calc.operation == 'pb_sub':
Dialog_calc.result = Dialog_calc.prevlousNum - Dialog_calc.currentNum
self.lcd.display(Dialog_calc.result)
elif Dialog_calc.operation == 'pb_mul':
Dialog_calc.result = Dialog_calc.prevlousNum * Dialog_calc.currentNum
self.lcd.display(Dialog_calc.result)
elif Dialog_calc.operation == 'pb_div':
if Dialog_calc.currentNum == 0:
self.lcd.display('Error')
Dialog_calc.result = 0
else:
Dialog_calc.result = Dialog_calc.prevlousNum / Dialog_calc.currentNum
self.lcd.display(Dialog_calc.result)
Dialog_calc.currentNum = Dialog_calc.result
Dialog_calc.lcdstring = ''
def closeEvent(self, QCloseEvent):
reply = QtGui.QMessageBox.question(self,u'警告',u'是否退出',QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
QCloseEvent.accept()
else:
QCloseEvent.ignore()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mycalc = Dialog_calc()
mycalc.show()
sys.exit(app.exec_())
7.F2运行代码,当然也可以用PyCharm去写,比较习惯点。