第一步:用PyQt4的窗体设计功能完成界面设计,如下图:
文件名为:calculator.ui(界面设计后存的格式就是这样)
第二步:通过控制台命令把calculator.ui格式的文件转换为*.py 格式的文件如:
>>>yuic4 calculator.ui > calculator.py
上面的命令就把界面文件转化为python的源文件
第三步:要把界面调出来还要自己写个文件来引用:
自己写的python的文件名为:test.py
详细代码如下:
import sys
import string
from PyQt4 import QtCore, QtGui
from calculator import Ui_Form#这条语句就是导入要引用的界面文件的python文件
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
#定义全局变量
self.str1 = ''#接收第一个要运算的数
self.str2 = ''#接收第二个要运算的数
self.flag = '0'
self.calFlag = ''
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
self.ui.display.setText(self.str1)
#菜单的设置
exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
exit.setShortcut('Ctrl+Q')
self.connect(exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
menubar = self.menuBar()
file = menubar.addMenu('&File')
file.addAction(exit)
#数字键事件处理
QtCore.QObject.connect(self.ui.button_7,QtCore.SIGNAL("clicked()"),self.func_button7)
QtCore.QObject.connect(self.ui.button_8,QtCore.SIGNAL("clicked()"),self.func_button8)
QtCore.QObject.connect(self.ui.button_9,QtCore.SIGNAL("clicked()"),self.func_button9)
QtCore.QObject.connect(self.ui.button_4,QtCore.SIGNAL("clicked()"),self.func_button4)
QtCore.QObject.connect(self.ui.button_5,QtCore.SIGNAL("clicked()"),self.func_button5)
QtCore.QObject.connect(self.ui.button_6,QtCore.SIGNAL("clicked()"),self.func_button6)
QtCore.QObject.connect(self.ui.button_1,QtCore.SIGNAL("clicked()"),self.func_button1)
QtCore.QObject.connect(self.ui.button_2,QtCore.SIGNAL("clicked()"),self.func_button2)
QtCore.QObject.connect(self.ui.button_3,QtCore.SIGNAL("clicked()"),self.func_button3)
QtCore.QObject.connect(self.ui.button_0,QtCore.SIGNAL("clicked()"),self.func_button0)
#运算符按键事件处理
QtCore.QObject.connect(self.ui.button_add,QtCore.SIGNAL("clicked()"),self.func_buttonAdd)
QtCore.QObject.connect(self.ui.button_dec,QtCore.SIGNAL("clicked()"),self.func_buttonDec)
QtCore.QObject.connect(self.ui.button_mul,QtCore.SIGNAL("clicked()"),self.func_buttonMul)
QtCore.QObject.connect(self.ui.button_chu,QtCore.SIGNAL("clicked()"),self.func_buttonChu)
QtCore.QObject.connect(self.ui.button_equal,QtCore.SIGNAL("clicked()"),self.func_buttonEqual)
QtCore.QObject.connect(self.ui.button_c,QtCore.SIGNAL("clicked()"),self.func_buttonClear)
def closeEvent(self,event):#窗口关闭时的处理,只实现这个函数就可以,不用去调用
reply = QtGui.QMessageBox.question(self,'Message',"Are you sure to quit?",QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
def func_button7(self):
if self.flag == '1':
self.str2 = self.str2 + '7'
self.ui.display.setText(self.str2)
else:
self.str1 = self.str1 + '7'
self.ui.display.setText(self.str1)
def func_button8(self):
if self.flag == '1':
self.str2 = self.str2 + '8'
self.ui.display.setText(self.str2)
else:
self.str1 = self.str1 + '8'
self.ui.display.setText(self.str1)
def func_button9(self):
if self.flag == '1':
self.str2 = self.str2 + '9'
self.ui.display.setText(self.str2)
else:
self.str1 = self.str1 + '9'
self.ui.display.setText(self.str1)
def func_button4(self):
if self.flag == '1':
self.str2 = self.str2 + '4'
self.ui.display.setText(self.str2)
else:
self.str1 = self.str1 + '4'
self.ui.display.setText(self.str1)
def func_button5(self):
if self.flag == '1':
self.str2 = self.str2 + '5'
self.ui.display.setText(self.str2)
else:
self.str1 = self.str1 + '5'
self.ui.display.setText(self.str1)
def func_button6(self):
if self.flag == '1':
self.str2 = self.str2 + '6'
self.ui.display.setText(self.str2)
else:
self.str1 = self.str1 + '6'
self.ui.display.setText(self.str1)
def func_button1(self):
if self.flag == '1':
self.str2 = self.str2 + '1'
self.ui.display.setText(self.str2)
else:
self.str1 = self.str1 + '1'
self.ui.display.setText(self.str1)
def func_button2(self):
if self.flag == '1':
self.str2 = self.str2 + '2'
self.ui.display.setText(self.str2)
else:
self.str1 = self.str1 + '2'
self.ui.display.setText(self.str1)
def func_button3(self):
if self.flag == '1':
self.str2 = self.str2 + '3'
self.ui.display.setText(self.str2)
else:
self.str1 = self.str1 + '3'
self.ui.display.setText(self.str1)
def func_button0(self):
if self.flag == '1':
self.str2 = self.str2 + '0'
self.ui.display.setText(self.str2)
else:
self.str1 = self.str1 + '0'
self.ui.display.setText(self.str1)
#运算符处理函数
def func_buttonAdd(self):
self.ui.display.setText(self.str2)
self.flag = '1'
self.calFlag = '1'
def func_buttonDec(self):
self.ui.display.setText(self.str2)
self.flag = '1'
self.calFlag = '2'
def func_buttonMul(self):
self.ui.display.setText(self.str2)
self.flag = '1'
self.calFlag = '3'
def func_buttonChu(self):
self.ui.display.setText(self.str2)
self.flag = '1'
self.calFlag = '4'
def func_buttonEqual(self):
#字符串先转换为数字,计算结果后再转换为字符串
if self.calFlag == '1':
num = str(int(self.str1) + int(self.str2))
elif self.calFlag == '2':
num = str(int(self.str1) - int(self.str2))
elif self.calFlag == '3':
num = str(int(self.str1) * int(self.str2))
elif self.calFlag == '4':
num = str(int(self.str1) / int(self.str2))
else:
self.calFlag = '0'
self.ui.display.setText(num)
def func_buttonClear(self):
self.str1 = ''
self.str2 = ''
self.ui.display.setText('')
self.flag = '0'
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
第四步:在命令行运行你的python源文件
>>>python test.py
运行后的结果为:
这样一个简单的计算器就完成了,算法很笨,但是主要熟悉了python和PyQt4界面设计结合开发的思想和流程
通过本例子你会明白了python和PyQt4的界面设计思想。这(是我的第一个python的例子)。