PyQt實戰之計算器的實現
阿新 • • 發佈:2019-01-27
一.啟動並進入eric主介面,如下圖:
滑鼠右鍵,”開啟方式”選擇“pthon.exe”
二.eric的主介面如下圖所示:
選擇“Project”–>”New”,設定如下圖所示
點選“OK“,可以看到在source這一欄有個init.py這個檔案,如下圖所示
我們雙擊檢視這個檔案,發現這個init.py是個空檔案,裡面什麼內容都沒有。事實上這個init.py的作用只是讓整個工程成為Python的一個包而已。
三.選擇”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就可以執行咯,雖然這個計算器功能不多,但還是給大家看一下效果吧。
額,該死的靜態圖片。