Python uses tkinter to implement a simple calculator, pythontkinter
The examples in this article share the specific code of using tkinter to implement a simple calculator in python for your reference. The specific content is as follows:
Class Counter: # introduce tkinter import tkinter as tk # introduce the message pop-up window module import tkinter. messagebox as mbox # initialize Counter def _ init _ (self): # generate a window object self. window = self. tk. tk () # title self. window. title ('calculator') # Set the window size self. window. minsize (240,325) self. window. maxsize (240,325) # Whether to clear the display box to determine the parameter self. is_init_lable = False # Set Menu self. set_menu () # Set the display box self. lable_show = self. tk. label (text = '', anchor = 'se', font = ('black Body ', 30), fg = 'black') self. lable_show.place (x = 0, y = 0, width = 240, height = 80) # Set the button component self. set_buttons () # Put the window into the main message queue self. window. mainloop () # Set Menu def set_menu (self): # create total menu menubar = self. tk. menu (self. window) # create a drop-down menu and add the File menu filemenu = self. tk. menu (menubar, tearoff = 0) # create the Menu option filemenu. add_command (label = "Exit calculator", command = self. window. quit) # print author function def show_author (): self. mbox. showinfo (mes Sage = 'wiz333 @ XDL 2017 ') filemenu. add_command (label = "author", command = show_author) # Add the File menu to the total menu as a drop-down menu and name it the menubar operation. add_cascade (label = "operation", menu = filemenu) # display the total menu self. window. config (menu = menubar) # Set the button component def set_buttons (self): #7 btn7 = self. tk. button (text = '7', bd = 2, font = '') btn7.place (x = 0, y = 90, width = 60, height = 40) #8 btn8 = self. tk. button (text = '8', bd = 2, font =' ') btn8.place (x = 60, y = 90, width = 60, height = 40) #9 btn9 = self. tk. button (text = '9', bd = 2, font = '') btn9.place (x = 120, y = 90, width = 60, height = 40) # + btn_jia = self. tk. button (text = '+', bd = 2, font =' ') btn_jia.place (x = 180, y = 90, width = 60, height = 40) #4 btn4 = self. tk. button (text = '4', bd = 2, font = '') btn4.place (x = 0, y = 130, width = 60, height = 40) #5 btn5 = self. tk. button (text = '5', bd = 2, font = 'simhei ') btn5.place (x = 60, y = 130, width = 60, he Ight = 40) #6 btn6 = self. tk. button (text = '6', bd = 2, font = '') btn6.place (x = 120, y = 130, width = 60, height = 40) #-btn_jian = self. tk. button (text = '-', bd = 2, font =' ') btn_jian.place (x = 180, y = 130, width = 60, height = 40) #1 btn1 = self. tk. button (text = '1', bd = 2, font = '') btn1.place (x = 0, y = 170, width = 60, height = 40) #2 btn2 = self. tk. button (text = '2', bd = 2, font =' ') btn2.place (x = 60, y = 170, width = 60, heigh T = 40) #3 btn3 = self. tk. button (text = '3', bd = 2, font = 'simhei ') btn3.place (x = 120, y = 170, width = 60, height = 40) # * btn_cheng = self. tk. button (text = '*', bd = 2, font = '') btn_cheng.place (x = 180, y = 170, width = 60, height = 40) #0 btn0 = self. tk. button (text = '0', bd = 2, font =' ') btn0.place (x = 0, y = 210, width = 120, height = 40 )#. btn_point = self. tk. button (text = '. ', bd = 2, font = 'simhei') btn_point.place (x = 120, y = 210, widt H = 60, height = 40) #/btn_chu = self. tk. button (text = '/', bd = 2, font = '') btn_chu.place (x = 180, y = 210, width = 60, height = 40) # cancel btn_cancel = self. tk. button (text = 'C', bd = 2, font =' ') btn_cancel.place (x = 0, y = 250, width = 60, height = 40) # determine btn_ OK = self. tk. button (text = ', bd = 2, font = '') btn_ OK .place (x = 60, y = 250, width = 180, height = 40) # Click Event btn7.bind _ class ('button ',' <Button-1> ', self. click_ B Utton) # Click Event def click_button (self, e) bound to the Button: # determine whether the operation is new. if yes, clear the display box if self. is_init_lable: self. lable_show ['text'] = ''self. is_init_lable = False # accumulated font = e. widget ['text'] self. lable_show ['text'] + = font # exception capture try: # When determining duplicate operator numbers, use the final input symbol if self. lable_show ['text'] [-1] in ['+', '-', '*', '/'] and self. lable_show ['text'] [-2] in ['+', '-', '*', '/']: header = self. lable_show ['text'] [:-2] f Ooter = self. lable_show ['text'] [-1] self. lable_show ['text'] = header + footer character T: pass # normal calculation if e. widget ['text'] = ': try: res = eval (self. lable_show ['text'] [:-1]) # print (res) # obtain 9 digits of the decimal point self. lable_show ['text'] = str (round (float (res), 5) self. isinit = True distinct T ZeroDivisionError: # in Division, the divisor cannot be 0 self. mbox. showerror (message = 'division calculation! The divisor cannot be 0! ') Counter T: self. mbox. showerror (message = 'formula incorrect ') # cancel the current input character if e. widget ['text'] = 'C': cancel_res = self. lable_show ['text'] [:-2] self. lable_show ['text'] = cancel_res # instantiate the calculator object wiz = Counter ()
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.