簡單計算器(PyQt5 + Qt Designer)


Qt Designer設計完是這樣的(基本就是16個Push Button和1個lineEdit,再加上尺寸調整以及簡單的布局)

設計結束后,保存為name.ui(比如calculator.ui),打開cmd,cd到name.ui所在文件夾,執行以下代碼:pyuic5 -o name.py name.ui,生成name.py

打開name.py(比如Spyder),新建main.py,執行如下代碼

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from calculator import Ui_MainWindow

class Calcu(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.connection()
        self.show()
    
    def num_0(self):
        self.lineEdit.insert('0')
    
    def num_1(self):
        self.lineEdit.insert('1')
        
    def num_2(self):
        self.lineEdit.insert('2')
        
    def num_3(self):
        self.lineEdit.insert('3')
        
    def num_4(self):
        self.lineEdit.insert('4')
        
    def num_5(self):
        self.lineEdit.insert('5')
        
    def num_6(self):
        self.lineEdit.insert('6')
        
    def num_7(self):
        self.lineEdit.insert('7')
        
    def num_8(self):
        self.lineEdit.insert('8')
    
    def num_9(self):
        self.lineEdit.insert('9')
    
    def op_plus(self):
        self.lineEdit.insert('+')
        
    def op_minus(self):
        self.lineEdit.insert('-')
        
    def op_multiply(self):
        self.lineEdit.insert('*')
    
    def op_divide(self):
        self.lineEdit.insert('/')
    
    def op_ce(self):
        self.lineEdit.clear()
        
    def calculate(self):
        text = self.lineEdit.text()
        try:
            self.lineEdit.setText(str(eval(text)))
        except:
            self.lineEdit.setText('invalid syntax, check your input!')
        
    def connection(self):
        self.Num_0.clicked.connect(self.num_0)
        self.Num_1.clicked.connect(self.num_1)
        self.Num_2.clicked.connect(self.num_2)
        self.Num_3.clicked.connect(self.num_3)
        self.Num_4.clicked.connect(self.num_4)
        self.Num_5.clicked.connect(self.num_5)
        self.Num_6.clicked.connect(self.num_6)
        self.Num_7.clicked.connect(self.num_7)
        self.Num_8.clicked.connect(self.num_8)
        self.Num_9.clicked.connect(self.num_9)
        self.OP_plus.clicked.connect(self.op_plus)
        self.OP_minus.clicked.connect(self.op_minus)
        self.OP_multiply.clicked.connect(self.op_multiply)
        self.OP_divide.clicked.connect(self.op_divide)
        self.OP_CE.clicked.connect(self.op_ce)
        self.OP_equal.clicked.connect(self.calculate)

if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ca = Calcu()
    sys.exit(app.exec_())

結果如下:

 

-----------------------------------------------------------------------------------------------------The end


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM