Python 06 Calculator

Source: Internet
Author: User
Tags string format

Import Tkinterimport MathClass deom:operation_sign = False Sign1 = False Sign2 = False List1 = [] List2 = [] def __init__ (self): Self.root = Tkinter. Tk () self.root.minsize (Self.root.title) (' Nothing Is wrong card calculator ') Self.layout () Self.root.mainloo P () def layout (self): Self.val0 = Tkinter. Stringvar () Self.val0.set (' 0 ') Self.val = Tkinter. Stringvar () Self.val.set (' 0 ') # text set #标题 def newwindow (): # equivalent to the main interface tp = TK Inter.            TopLevel (width=300, height=330, bg= ' White ') # disables resizing #tp. Resizable (Width=false, Height=false) # define title Tp.title (' Help ') Self.val3 = Tkinter. Stringvar () self.val3.set (' implementation function: \ n Simple calculator, subtraction, square, percent, score, positive and negative \ n number, clear error, clear,delete\n resolve bug:\n1. Always click on the operation symbol to And the equals sign will add \n2. A divisor of zero indicates that the divisor cannot be 0\n3. If the decimal result is greater than 15 bits, 15 bits are reserved, and the integer indicates that the \N4 is out of range. The decimal point is controlled by one, and the first decimal point is 0 \n5. Fixed the current known bug\n\n\ntips:\n if found bug please contact the author to modify!! \ n This interface tends to be native to \ nthe copyright @Zhao python04 ') # message logging Label3 = Tkinter.        Label (TP, BG = ' white ', textvariable = Self.val3,anchor = ' NW ', justify = ' left ') label3.place (x = 0, y = 0) # Create Total Menu menubar = Tkinter. Menu (SELF.ROOT,BG = ' #F2F2F2 ', activeforeground = ' white ', activebackground = ' #1717FF ') # Create a drop-down menu and join the File menu F Ilemenu = Tkinter. Menu (MENUBAR,BG = ' #F2F2F2 ', activeforeground = ' white ', activebackground = ' #1717FF ') # The option to create down menus filemenu.ad D_command (label= "about", Command=newwindow) # Create a drop-down menu of Split lines Filemenu.add_separator () Filemenu.add_command (label= "Exit", Command=self.root.quit) # Adds the File menu as a drop-down menu to the total menu and will be named file Menubar.add_cascade (label= "Help", men U=filemenu) # Displays the Total menu self.root.config (menu=menubar) # label label0 = Tkinter. Label (self.root,textvariable=self.val0, bg= ' white ', FG = ' #7B7B7B ', font= (' blackbody ', '), Width= ' ", anchor= ' e ',        Borderwidth= '). Place (x= ' 0 ', y= ' 0 ', width= 320,height = 40)# key) Label1 = Tkinter. Label (Self.root, Textvariable=self.val, bg= ' White ', font= (' blackbody ', '), width= ', anchor= ' e ', borderwidth= '). Place ( x= ' 0 ', y= ' + ', width = 320,height = 60) # Key BTN9 = Tkinter. Button (Self.root, text= ' 9 ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', Command=lambda:self.num (' 9 ')) . place (x=160, y=200, width=80, height=50) Btn8 = Tkinter. Button (Self.root, text= ' 8 ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', Command=lambda:self.num (' 8 ')) . place (x=80, y=200, width=80, height=50) btn7 = Tkinter. Button (Self.root, text= ' 7 ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', Command=lambda:self.num (' 7 ')) . place (X=0, y=200, width=80, height=50) btn6 = Tkinter. Button (Self.root, text= ' 6 ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', Command=lambda:self.num (' 6 ')) . place (x=160, y=250, width=80, height=50) btn5 = Tkinter. Button (Self.root, text= ' 5 ', bg = ' #F2F2F2 ', font= (' blackbody ',Activebackground = ' #CFEFFE ', Command=lambda:self.num (' 5 ')). Place (x=80, y=250, width=80, height=50) Btn4 = Tkin ter. Button (Self.root, text= ' 4 ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', Command=lambda:self.num (' 4 ')) . place (X=0, y=250, width=80, height=50) btn3 = Tkinter. Button (Self.root, text= ' 3 ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', Command=lambda:self.num (' 3 ')) . place (x=160, y=300, width=80, height=50) btn2 = Tkinter. Button (Self.root, text= ' 2 ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', Command=lambda:self.num (' 2 ')) . place (x=80, y=300, width=80, height=50) btn1 = Tkinter. Button (Self.root, text= ' 1 ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', Command=lambda:self.num (' 1 ')) . place (X=0, y=300, width=80, height=50) btn0 = Tkinter. Button (self.root, text= ' 0 ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', Command=lambda:self.num (' 0 ')) . place (x=80, y=350, width=80, height=50) btnsign = Tkinter. Button (self.root, text= ' ± ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', command = Self.plus_minus). Place (x=0, y=350, width=80, height=50) Btnpoint = Tkinter. Button (Self.root, text= '. ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', Command=lambda:self.num ('. ')). Place (x=160, y=350, width=80, height=50) Btndiv = Tkinter. Button (self.root, text= ' ÷ ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', command=lambda:self.operation (‘/‘)). Place (x=240, y=150, width=80, height=50) Btnmul = Tkinter. Button (Self.root, text= ' x ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', command=lambda:self.operation (‘*‘)). Place (x=240, y=200, width=80, height=50) Btnadd = Tkinter. Button (Self.root, text= ' + ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', command=lambda:self.operation (' + ')). Place (x=240, y=300, width=80, height=50) btnsub = Tkinter. Button (Self.root, text= '-', BG = ' #F2F2F2 ', font= (' blackbody ', ' activebackground '), ' #CFEFFE ', command=lambda:self.operation ('-') '). Place (x=240, y=250, width=80, height=50) btnequal = Tkinter. Button (self.root, text= ' = ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activeforeground = ' white ', activebackground = ' #1717FF ', command=self.eq_operation). Place (x=240, y=350, width=80, height=50) Btndel = Tkinter. Button (Self.root, text= ' del ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', command = Self.delete_ Operation). Place (x=160, y=150, width=80, height=50) btnclr = Tkinter. Button (Self.root, text= ' C ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activeforeground = ' white ', activebackground = ' #FE524E ', Command = Self.clear_all). Place (x=80, y=150, width=80, height=50) btnce = Tkinter. Button (self.root, text= ' CE ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', command = self.clear_error). Place (x=0, y=150, width=80, height=50) btnpercent = Tkinter. Button (self.root, text= '% ', bg = ' #F2F2F2 ', font= (' BlackActivebackground = ' #CFEFFE ', command = self.percent_operation). Place (X=0, y=100, width=80, height=50) Btnev Olution = Tkinter. Button (self.root, text= ' √ ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', command =self.evolution_ Operation). Place (x=80, y=100, width=80, height=50) Btnsquare = Tkinter. Button (self.root, text= ' x2 ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', command = self.square_ Operation). Place (x=160, y=100, width=80, height=50) Btngrade = Tkinter. Button (Self.root, text= ' 1/x ', bg = ' #F2F2F2 ', font= (' blackbody ', '), Activebackground = ' #CFEFFE ', command = Self.grade_ Operation). Place (x=240, y=100, width=80, height=50) #键盘输入操作 def num (self,number): a = number print (num ber) ' if self.sign2 = = true:if Number = = ' 0 ': print (' error ') ' If Self.operati On_sign = = True:self.val.set (' 0 ') self.operation_sign = False if self.val.get () = = ' 0 ': # if In the results screenThe number is 0, that is, the beginning of the default value, the judge, is 0 is the number of the press is not 0, to be stitched self.val.set (number) If no. = = '. ': Self.val.s                ET (' 0 ' + number) Else: #不是0的情况 if Self.val.get (). Count ('. ') = = 0: #如果获取的字符串里面只有一个. Self.val.set (Self.val.get () + number) # screen gets a numeric string to connect the next pressed digit elif number! = '. ': self.val.se                        T (Self.val.get () + number) "if self.sign2 = = True:if Self.val.get () = = ' 0 ':    Print (' error ') ' #self. sign2 = False Self.sign1 = True self.sign2 = true # positive or negative action def plus_minus (self): List2 = Self.val.get () if list2[0] = = '-': Self.val.set (list2[1:]) E Lif list2[0]! = '-' and list2! = ' 0 ': self.val.set ('-' + list2) self.sign1 = True #退格操作 def delete _operation (self): If Len (Self.val.get ()) >1:self.list1 = List (Self.val.get ()) Self.list1. Pop () result = ' '. Join (Self.list1) self.val.set (Result) Else:self.val.set (' 0 ') #平方操作 def Square_operati On (self): Self.val.get () Self.list1.append (Self.val.get ()) List2 = ' * * 2 ' self.list1.append (li        ST2) str1 = ". Join (SELF.LIST1) # converted to a string!  Print (STR1) Self.val0.set (str1) # Sub-box display string can also be a list result = eval (str1) # To perform operations, Python code if result >            1:RESULT1 = str (result) If Len (RESULT1) > 17:self.val.set (' Result exceeds range ') Else:self.val.set (Result) ELSE:RESULT1 = round (result, a) Self.val.set (resu LT1) print (Self.list1) self.list1.clear () Self.operation_sign = True #开平方操作 def evolution_opera tion (self): Try:self.val.get () List2 = ' Math.sqrt ({0}) '. Format (Self.val.get ()) PR      Int (list2) self.list1.append (list2) str1= ". Join (Self.list1)      Print (STR1) self.val.set (eval (str1)) result = eval (str1) # operation, Python code RESULT1        = Round (result, Self.val.set) (RESULT1) self.list1.clear () Self.operation_sign = True         Except ValueError:self.val.set (' cannot open root of negative number ') #% Operation def percent_operation (self): Self.val.get () Self.list1.append (Self.val.get ()) List2 = '/' Self.list1.append (list2) result = '. Join (S  Elf.list1) Self.val.set (eval (result)) print (Self.list1) self.list1.clear () self.operation_sign            = True #分数 def grade_operation (self): Try:self.val.get () print (SELF.LIST1)            List2 = ' 1/{} '. Format (Self.val.get ()) self.list1.append (list2) str1= '. Join (SELF.LIST1) Print (STR1) self.val.set (eval (str1)) result = eval (str1) # operation, python code RESULT1 = Rou          nd (result, 15)  Self.val.set (RESULT1) print (Self.list1) self.list1.clear () Self.operation_sign = True Except ZeroDivisionError:self.val.set (' divisor cannot be zero ') #清空全部数据操作 def Clear_all (self): Self.val.set (' 0 ') Self.list1 = [] Self.val0.set (' 0 ') self.operation_sign = False #清空错误操作 def clear_error (SE LF): If Self.val.get ()! = ": Self.val.set (' 0 ') #运算操作 def operation (self,addnum): If Self.s Ign1 = = True: #满足这个条件即可按下运算键, otherwise not, i.e. 0-9 with equal sign print (addnum) # Addnum for symbol print (Self.val.get ()) # Get the text This string format self.list1.append (Self.val.get ()) # Appends text to List1, where Val.get is the list of contents in the text Self.list1.append (a Ddnum) # "2 +" Print (SELF.LIST1) # need to determine if the operation symbol is pressed Self.val0.set (self.list1) self.operation_s                IGN = True self.sign1 = False #等号操作 def eq_operation (self): try:if self.sign2 = = true: If Self.sign1 = = true: #sign1 to True can be equal to Self.list1.append (Self.val.get ()) # The previous value is passed into the list1 after the equal sign is pressed                    Print (self.list1) str1 = '. Join (SELF.LIST1) # Convert to String! Print (STR1) Self.val0.set (str1) # Sub-box display string can also be a list result = eval (str1) # To perform the operation, Python Code if result > 1:RESULT1 = str (result) If Len (result1 ) > 15:self.val.set (' Results over range ') else:self. Val.set (Result) Else:result1 = round (result,15) self.val . Set (RESULT1) self.list1.clear () self.operation_sign = True #此时判断出按键是否按下 self.sig N1 = True #按下等号 becomes true number can be pressed SELF.SIGN2 = False except ZeroDivisionError:self.val.set ( ' Divisor cannot be zero ') Deom = Deom ()

  

Python 06 Calculator

Related Article

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.