PyQt5 Simple Calculator

Source: Internet
Author: User

The rest of the calculation function (Self.calculator) is not implemented, and interested friends can implement it

"Knowledge points"

1. Use the Loop to add button parts, and to set the signal/slot for each button

2, give the button set fixed size:button.setfixedsize (qtcore.qsize (60,30))

3. Take the text of the sender of the event (this example is the individual button): Self.sender (). Text ()

【】

"Source Code"

1 ImportSYS2  fromPyQt5ImportQtwidgets,qtcore,qtgui3 4 5 classExample (qtwidgets.qwidget):6 7     def __init__(self):8Super (Example, self).__init__()9 Self.initui ()Ten          One Self.reset () A  -     defInitui (self): -Self.setwindowtitle ('Simple Calculator') theGrid =qtwidgets.qgridlayout () -          -Self.display = Qtwidgets.qlineedit ('0') -Self.display.setFont (Qtgui.qfont (" Times", 20)) + self.display.setReadOnly (True) - self.display.setAlignment (QtCore.Qt.AlignRight) +Self.display.setMaxLength (15) AGrid.addwidget (self.display,0,0,1,4) at  -names = ['Clear',' Back',"','Close',  -                 '7','8','9','/', -                 '4','5','6','*',  -                 '1','2','3','-', -                 '0','.','=','+'] inpos = [(0, 0), (0, 1), (0, 2), (0, 3), -(1, 0), (1, 1), (1, 2), (1, 3), to(2, 0), (2, 1), (2, 2), (2, 3), +(3, 0), (3, 1), (3, 2), (3, 3 ), -(4, 0), (4, 1), (4, 2), (4, 3)] thec =0 *          forNameinchnames: $button =Qtwidgets.qpushbutton (name)Panax NotoginsengButton.setfixedsize (Qtcore.qsize (60,30)) -Button.clicked.connect (self.buttonclicked)#set the signal/slot for each button the             ifc = = 2: +                 Pass A                 #Grid.addwidget (Qtwidgets.qlabel ("), 0, 2) #替换 The third button is a text label!  the             Else:  +Grid.addwidget (Button, pos[c][0]+1, pos[c][1]) -c = C + 1 $          $ self.setlayout (GRID) -          -          the          -         Wuyi     defbuttonclicked (self): the         #sender = Self.sender (); # Identify the sender of the signal -         #Self.display.setText (Sender.text ()) WuText =Self.sender (). Text () -         ifTextinch '+-*/': AboutSelf.history.append (Self.number)#digital into the stack $Self.history.append (text)#operator into the stack -Self.operator = text#Set Current operator -Self.number ="" #Digital emptying -Self.numbertype ="int" A             return +         elifText = ="=": theSelf.calculate ()#Calculation -         elifText = =" Back": $             Pass the         elifText = ="Clear": the Self.reset () the         elifText = ="Close": the self.close () -         elifText = =".": in             ifSelf.numbertype = ="int": theSelf.number + =text theSelf.numbertype ="float" About         Else: theSelf.number = self.number + textifSelf.number! ="0" Elsetext the  the Self.display.setText (Self.number) +          -     defCalculate (self): the         PassBayi          the     defReset (self): theSelf.number ="0" -Self.result =0 -Self.history = [] theSelf.operator ="' # +,-,*,/ theSelf.numbertype ='int' #int and float two, if the decimal point is a real number the  theApp =qtwidgets.qapplication (SYS.ARGV) -ex =Example () the ex.show () theSys.exit (App.exec_ ())

PyQt5 Simple Calculator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.