Simple calculator based on python tkinter (v1.0)
Import tkinter# defines calculator class class Calc: # initialization magic method def _ _ init__ (self): # initialization common attribute # defines a list for storing calculated strings self.operationList = [] # defines operation tag determination Whether the operational symbol self.isOper = False # initialization interface self.initWindows () # is entered or not. Change by keyboard color method def changeBg (self Evt): evt.widget ['bg'] =' cyan' # restore the keyboard color method def backBg (self,evt): evt.widget ['bg'] =' lightgray' # numeric button operation method def buttonAction (self Number): # determines whether the user presses the operation button if self.isOper = = True: # the number self.num.set (number) # after the operator is displayed on the interface the operation flag resets self.isOper = False else: # No ammonium operation button # determine whether the original interface number is 0 existNumber = self.num.get () if existNumber = = '0operations: # if the initial number in the interface If the initial data is 0, get the user input data and display self.num.set (number) else: # if the initial data in the interface is not 0, accumulate the characters self.num.set (self.num.get () + number) # Operation button operation method def operation (self OpFlag): # the operation flag is set to true self.isOper = True # to get the number that exists in the interface and write the list self.operationList.append (self.num.get ()) # the current operation symbol will not be written in the previous step, it needs to be written to self separately. OperationList.append (opFlag) # Operation method def getResult (self): # add the numbers in the current interface to the calculation list self.operationList.append (self.num.get ()) # start the calculation result = eval ('.join (self.operationList)) Self.num.set (result) # all empty recalculation method def clearAll (self): # interface set 0 calculation list set 0 self.num.set ('0') self.operationList.clear () # Operation flag reset self.isOper = False # implement the backspace key method def backSpace (self): # get the current display digit length strLength = len (self.num.get ()) # if there is a number if strLength > 1: # Delete the last word in the string PresentStr = self.num.get () presentStr = presentStr [: strLength-1] self.num.set (presentStr) else: self.num.set ('0') # positive and negative sign implementation method def pm (self): presentStr = Self.num.get () # implements the addition and removal of the negative sign if presentStr [0] = ='-': self.num.set (string [1:]) # the original string must not start with the-sign and 0 elif presentStr [0] not in ('-' '0'): self.num.set (' -'+ presentStr) # Interface layout method def initWindows (self): # generate main window Custom window size root = tkinter.Tk () root.minsize (400500) root.title ('Micro hard Calculator') # generate the variable self.num = tkinter.StringVar () self.num.set (0) # the operation result output position result = tkinter.Label (root) Width=20,height=2,bg='white',bd=10,anchor='e',font= ('Song style', 50), textvariable=self.num) result.place (relx=0,rely=0,relwidth=1.0 Relheight=0.4) # # the following is the key part # # buttonCE = tkinter.Button (root,text='CE',bg='lightgray',command = self.clearAll) buttonCE.place (relx=0,rely=0.4,relwidth=0.25 Relheight=0.1) # bind button to generate mouse discoloration effect buttonCE.bind (', self.changeBg) buttonCE.bind (', self.backBg) buttonC = tkinter.Button (root,text='C',bg='lightgray',command = self.clearAll) buttonC.place (relx=0.25,rely=0.4,relwidth=0.25) Relheight=0.1) # bind button to generate mouse discoloration effect buttonC.bind (', self.changeBg) buttonC.bind (', self.backBg) buttonDel = tkinter.Button (root,text=')