In this article, we share the example of Pyqt to achieve a simple calculator specific code for your reference, the details are as follows
Environment: pycharm, python3.7
First use qtDesigner to design the following interface.
Then use pyUIC to convert the .ui file into an inner .py file. The source code is as follows
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file '' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): ("MainWindow") (726, 592) = (MainWindow) ("centralwidget") = () ((180, 50, 351, 41)) ("outLine") self.bt1 = () self.((190, 110, 61, 61)) self.("bt1") self.bt2 = () self.((270, 110, 61, 61)) self.("bt2") self.bt3 = () self.((360, 110, 61, 61)) self.("bt3") = () ((450, 110, 61, 61)) ("btPlus") self.bt6 = () self.((360, 190, 61, 61)) self.("bt6") self.bt5 = () self.((270, 190, 61, 61)) self.("bt5") = () ((450, 190, 61, 61)) ("btSub") self.bt4 = () self.((190, 190, 61, 61)) self.("bt4") self.bt9 = () self.((360, 270, 61, 61)) self.("bt9") self.bt8 = () self.((270, 270, 61, 61)) self.("bt8") = () ((450, 270, 61, 61)) ("btMul") self.bt7 = () self.((190, 270, 61, 61)) self.("bt7") = () ((360, 340, 61, 61)) ("btCal") self.bt0 = () self.((270, 340, 61, 61)) self.("bt0") = () ((450, 340, 61, 61)) ("btDiv") = () ((190, 340, 61, 61)) ("btClr") () = (MainWindow) ((0, 0, 726, 26)) ("menubar") () = (MainWindow) ("statusbar") () (MainWindow) (MainWindow) def retranslateUi(self, MainWindow): _translate = (_translate("MainWindow", "MainWindow")) self.(_translate("MainWindow", "1")) self.(_translate("MainWindow", "2")) self.(_translate("MainWindow", "3")) (_translate("MainWindow", "+")) self.(_translate("MainWindow", "6")) self.(_translate("MainWindow", "5")) (_translate("MainWindow", "-")) self.(_translate("MainWindow", "4")) self.(_translate("MainWindow", "9")) self.(_translate("MainWindow", "8")) (_translate("MainWindow", "*")) self.(_translate("MainWindow", "7")) (_translate("MainWindow", "=")) self.(_translate("MainWindow", "0")) (_translate("MainWindow", "/")) (_translate("MainWindow", "clear"))
Create a new py file to introduce the ui file just generated by pyUIC. Write the relevant code, the source code is as follows:
# -*- coding: utf-8 -*- # @Author:ct # @ProjectName: # @Desc: # @Time: import sys from calculatorUI import Ui_MainWindow from import QApplication, QWidget, QMainWindow class mWindow(QMainWindow, Ui_MainWindow): def __init__(self): super(mWindow, self).__init__() (self) # Define all button event functions def prs_bt0(self): ('0') def prs_bt1(self): ('1') def prs_bt2(self): ('2') def prs_bt3(self): ('3') def prs_bt4(self): ('4') def prs_bt5(self): ('5') def prs_bt6(self): ('6') def prs_bt7(self): ('7') def prs_bt8(self): ('8') def prs_bt9(self): ('9') def prs_btPlus(self): ('+') def prs_btSub(self): ('-') def prs_btMul(self): ('*') def prs_btDiv(self): ('/') def prs_btClr(self): () # Calculate expression event, string expression will be calculated by eval function def prs_btCal(self): # Get the content of the input box (in string form) exp = () res = eval(exp) ('=') (str(res)) if __name__ == '__main__': app = QApplication() mainWindow = mWindow() # (False) # Make the output box read-only, not inputted (True) # Associate all button events mainWindow.(mainWindow.prs_bt0) mainWindow.(mainWindow.prs_bt1) mainWindow.(mainWindow.prs_bt2) mainWindow.(mainWindow.prs_bt3) mainWindow.(mainWindow.prs_bt4) mainWindow.(mainWindow.prs_bt5) mainWindow.(mainWindow.prs_bt6) mainWindow.(mainWindow.prs_bt7) mainWindow.(mainWindow.prs_bt8) mainWindow.(mainWindow.prs_bt9) (mainWindow.prs_btPlus) (mainWindow.prs_btDiv) (mainWindow.prs_btMul) (mainWindow.prs_btSub) (mainWindow.prs_btCal) (mainWindow.prs_btClr) () # Make the program run in a loop (app.exec_())
Run the program with the following results:
Simple operations can be performed. Of course, there are still many bugs, such as input error expression can not tolerate error, etc., to be fixed later.
This is the whole content of this article.