Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the function of simple Calculator with python tkinter

Shulou Source: shulou.com Published: 2022-06-01 19:19:39 10月04日 Update

This article mainly explains "how to use python tkinter to achieve simple calculator function", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use python tkinter to achieve simple calculator function" bar!

Effect picture

Go directly to the code

Import tkinter as tkinput_num_ls = [] first_num = Nonecalculator_method = Nonedef get_num (ls): new_ls = [10 * * I * float (num) for I Num in enumerate (ls)] ls_sum = sum (new_ls) if int (ls_sum) = = ls_sum: return int (ls_sum) else: return ls_sumdef append_num (num): global input_num_ls if len (num) < 10: input_num_ls.append (num) else: input_num_ls.append (num [: 10]) Current_value.set (get_num (input_num_ls)) print (input_num_ls) def append_calculator (method): global input_num_ls First_num, calculator_method calculator_method = method first_num = get_num (input_num_ls) input_num_ls = [] print ('method', calculator_method) def calculator_result (): global first_num, input_num_ls Calculator_method second_num = get_num (input_num_ls) input_num_ls.clear () if calculator_method = ='+': current_value.set (second_num + first_num) input_num_ls.append (str (second_num + first_num) elif calculator_method = ='-': current_value.set (first_num-second_num) input_num_ls. Append (str (first_num-second_num)) elif calculator_method = ='*': current_value.set (first_num * second_num) input_num_ls.append (str (second_num * first_num)) elif calculator_method = ='/': current_value.set (first_num / second_num) input_num_ls.append (str (first_num / second_num)) print (first_num Second_num, calculator_method) def clear (): global first_num, input_num_ls Calculator_method first_num = None input_num_ls = [] calculator_method = None current_value.set (0) def func (): pass# body window window = tk.Tk () # set window title window.title ('simple Calculator') # set window width and height window.geometry ('400x300') # add user display screen background screen_area = tk.Frame (width='400', height='100' Bg='#ddd') # put into window screen_area.pack () # sample settings display data class current_value = tk.StringVar () current_value.set (0) # numeric display box # position of anchor text relative to the center of the tag is center N S W Eshow_screen_label = tk.Label (screen_area, textvariable=current_value, bg='white', width='400', height='2', font= {'boldface', 40, 'bold'} Anchor='e') show_screen_label.pack (padx=10, pady=6) # Button area button_area = tk.Frame (width='300', height='300', bg='#ccc') button_area.pack (padx=10, pady=5) # add buttontk.Button (button_area, text='C', width='5', height='1', command=lambda: clear ()) .grid (row='1', column='0') tk.Button (button_area, text='+', width='5', height='1' Command=lambda: append_calculator ('+'). Grid (row='1', column='1') tk.Button (button_area, text='-', width='5', height='1', command=lambda: append_calculator ('-'). Grid (row='1', column='2') tk.Button (button_area, text='*', width='5', height='1', command=lambda: append_calculator ('*'). Grid (row='1', column='3') tk.Button (button_area, text='7' Width='5', height='1', command=lambda: append_num ('7'). Grid (row='2', column='0') tk.Button (button_area, text='8', width='5', height='1', command=lambda: append_num ('8')). Grid (row='2', column='1') tk.Button (button_area, text='9', width='5', height='1', command=lambda: append_num ('9')). Grid (row='2' Column='2') tk.Button (button_area, text='/', width='5', height='1', command=lambda: append_calculator ('/')) .grid (row='2', column='3') tk.Button (button_area, text='4', width='5', height='1', command=lambda: append_num ('4')) .grid (row='3', column='0') tk.Button (button_area, text='5', width='5', height='1' Command=lambda: append_num ('5'). Grid (row='3', column='1') tk.Button (button_area, text='6', width='5', height='1', command=lambda: append_num ('6')). Grid (row='3', column='2') tk.Button (button_area, text='=', width='5', height='1', command=lambda: calculator_result ()). Grid (row='3', column='3') tk.Button (button_area, text='1' Width='5', height='1', command=lambda: append_num ('1'). Grid (row='4', column='0') tk.Button (button_area, text='2', width='5', height='1', command=lambda: append_num ('2')). Grid (row='4', column='1') tk.Button (button_area, text='3', width='5', height='1', command=lambda: append_num ('3')) .grid (row='4' Column='2') tk.Button (button_area, text='C', width='5', height='1', command=lambda: clear ()). Grid (row='4', column='3') window.mainloop () Thank you for your reading The above is the content of "how to use python tkinter to achieve simple calculator function". After the study of this article, I believe you have a deeper understanding of how to use python tkinter to achieve simple calculator function, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Calculators functions learning content subjects codes locations areas that is screens ideas situations buttons effects effect maps numbers digital displays data articles more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn NVidia OPPO Reno Shulou Information