Python PyQt5 implements simple calculator function example, pythonpyqt5
This example describes the simple calculator function implemented by Python PyQt5. We will share this with you for your reference. The details are as follows:
The computing functions (self.calculator
) Not implemented. If you are interested, you can implement it.
[Knowledge point]
1. Add button parts cyclically and set signals/slots for each button
2. Set a fixed size for the button:button.setFixedSize(QtCore.QSize(60,30))
3. Take the text of the event sender (in this example, each button:self.sender().text()
【]
[Source code]
Import sysfrom PyQt5 import QtWidgets, QtCore, QtGuiclass Example (QtWidgets. QWidget): def _ init _ (self): super (Example, self ). _ init _ () self. initUI () self. reset () def initUI (self): self. setWindowTitle ('simple calculator') grid = QtWidgets. QGridLayout () self. display = QtWidgets. QLineEdit ('0') self. display. setFont (QtGui. QFont ("Times", 20) self. display. setReadOnly (True) self. display. setAlignment (QtCore. qt. alignR Ight) self. display. setMaxLength (15) grid. addWidget (self. display,) names = ['clear', 'back', '', 'close', '7', '8', '9 ','/', '4', '5', '6', '*', '1', '2', '3', '-', '0 ','. ',' = ',' + '] pos = [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2 ), (2, 3), (3, 0), (3, 1), (3, 2), (3, 3), (4, 0), (4, 1), (4, 2), (4, 3)] c = 0 for name in names: button = QtWidg Ets. QPushButton (name) button. setFixedSize (QtCore. QSize (60, 30) button. clicked. connect (self. buttonClicked) # Set the signal/slot for each button if c = 2: pass # grid. addWidget (QtWidgets. QLabel (''), 0, 2) # Replace the third button with the text label! Else: grid. addWidget (button, pos [c] [0] + 1, pos [c] [1]) c = c + 1 self. setLayout (grid) def buttonClicked (self): # sender = self. sender (); # determine the sender # self. display. setText (sender. text () text = self. sender (). text () if text in '+-*/': self. history. append (self. number) # number into Stack self. history. append (text) # Operator into Stack self. operator = text # set the current operator self. number = "" # clear self. numberType = "int" return elif text = "=": Self. calculate () # calculate elif text = "Back": pass elif text = "Clear": self. reset () elif text = "Close": self. close () elif text = ". ": if self. numberType = "int": self. number + = text self. numberType = "float" else: self. number = self. number + text if self. number! = "0" else text self. display. setText (self. number) def calculate (self): pass def reset (self): self. number = "0" self. result = 0 self. history = [] self. operator = ''# +,-, *,/self. numberType = 'int' # int and float. If the decimal point is entered, the real number app = QtWidgets. QApplication (sys. argv) ex = Example()ex.show()sys.exit(app.exe c _())
PS: Here are some recommended computing tools for your reference:
Calculation tool for online mona1 functions (equations:
Http://tools.jb51.net/jisuanqi/equ_jisuanqi
Scientific calculator online use _ advanced calculator online computing:
Http://tools.jb51.net/jisuanqi/jsqkexue
Online calculator _ standard calculator:
Http://tools.jb51.net/jisuanqi/jsq