1 ui設(shè)計
使用qt designer ,按裝anaconda后,在如下路徑找到:
conda3.05Libraryin
designer.exe文件,雙擊啟動:
創(chuàng)建窗體,命名為XiaoDing,整個的界面如下所示:
私信小編01 領(lǐng)取完整項目代碼
qt 設(shè)計器提供的常用控件基本都能滿足開發(fā)需求,通過拖動左側(cè)的控件,很便捷的就能搭建出如下的UI界面,比傳統(tǒng)的手寫控件代碼要方便很多。
最終設(shè)計的計算器XiaoDing界面如下,
比如,其中一個用于計算器顯示的對象:lcdNumber,對象的類型為:LCD Number。右側(cè)為計算器中用到的所有對象。
2 轉(zhuǎn)py文件
使用如下命令,將設(shè)計好的ui文件轉(zhuǎn)為py文件
pyuic5 -o ./calculator/MainWindow.py ./calculator/mainwindow.ui
3 計算器實現(xiàn)邏輯
導(dǎo)入庫:
fromPyQt5.QtGuiimport* fromPyQt5.QtWidgetsimport* fromPyQt5.QtCoreimport* importoperator fromMainWindowimportUi_MainWindow
主題代碼邏輯很精簡:
#Calculatorstate. READY=0 INPUT=1 classMainWindow(QMainWindow,Ui_MainWindow): def__init__(self,*args,**kwargs): super(MainWindow,self).__init__(*args,**kwargs) self.setupUi(self) #Setupnumbers. forninrange(0,10): getattr(self,'pushButton_n%s'%n).pressed.connect(lambdav=n:self.input_number(v)) #Setupoperations. self.pushButton_add.pressed.connect(lambda:self.operation(operator.add)) self.pushButton_sub.pressed.connect(lambda:self.operation(operator.sub)) self.pushButton_mul.pressed.connect(lambda:self.operation(operator.mul)) self.pushButton_div.pressed.connect(lambda:self.operation(operator.truediv))#operator.divforPython2.7 self.pushButton_pc.pressed.connect(self.operation_pc) self.pushButton_eq.pressed.connect(self.equals) #Setupactions self.actionReset.triggered.connect(self.reset) self.pushButton_ac.pressed.connect(self.reset) self.actionExit.triggered.connect(self.close) self.pushButton_m.pressed.connect(self.memory_store) self.pushButton_mr.pressed.connect(self.memory_recall) self.memory=0 self.reset() self.show()
基礎(chǔ)方法:
definput_number(self,v): ifself.state==READY: self.state=INPUT self.stack[-1]=v else: self.stack[-1]=self.stack[-1]*10+v self.display() defdisplay(self): self.lcdNumber.display(self.stack[-1])
按鈕RE,M, RE對應(yīng)的實現(xiàn)邏輯:
defreset(self): self.state=READY self.stack=[0] self.last_operation=None self.current_op=None self.display() defmemory_store(self): self.memory=self.lcdNumber.value() defmemory_recall(self): self.state=INPUT self.stack[-1]=self.memory self.display()
+,-,x,/,/100對應(yīng)實現(xiàn)方法:
defoperation(self,op): ifself.current_op:#Completethecurrentoperation self.equals() self.stack.append(0) self.state=INPUT self.current_op=op defoperation_pc(self): self.state=INPUT self.stack[-1]*=0.01 self.display()
=號對應(yīng)的方法實現(xiàn):
defequals(self): ifself.state==READYandself.last_operation: s,self.current_op=self.last_operation self.stack.append(s) ifself.current_op: self.last_operation=self.stack[-1],self.current_op try: self.stack=[self.current_op(*self.stack)] exceptException: self.lcdNumber.display('Err') self.stack=[0] else: self.current_op=None self.state=READY self.display()
main函數(shù):
if__name__=='__main__': app=QApplication([]) app.setApplicationName("XiaoDing") window=MainWindow() app.exec_()
完整代碼請參考點擊閱讀原文下載,代碼只有100行。完整代碼請點擊文章最底部的【閱讀原文】。啟動后的界面如下:
-
計算器
+關(guān)注
關(guān)注
16文章
439瀏覽量
38078 -
python
+關(guān)注
關(guān)注
56文章
4827瀏覽量
86718
發(fā)布評論請先 登錄
Qorvo全新設(shè)計計算器:晶振選型、能耗預(yù)算計算器和鏈路預(yù)算與覆蓋范圍計算器

VirtualLab:衍射角計算器
VirtualLab Fusion應(yīng)用:相干時間和相干長度計算器
VirtualLab:衍射角計算器
Made with KiCad(121):OpenRPNCalc 開源科學(xué) RPN 計算器

VirtualLab Fusion應(yīng)用:相干時間和相干長度計算器
Debye-Wolf積分計算器的用法
LP光纖模式計算器
使用DRV421進(jìn)行設(shè)計:系統(tǒng)參數(shù)計算器

基于FPGA的計算器設(shè)計

評論