In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces python how to use tkinter to do a simple calculator, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Design ideas
First, import the package we need-tkinter, and create a window by instantiating a Tk object
Because I have some food, I can't control the position of the components, so the window uses the automatic default size.
Import tkinter as tkimport tkinter.messageboxwin = tkinter.Tk () win.title (calculator) win.mainloop ()
Roughly plan the location of each component
My goal is to make it look like this (final effect)
After roughly planning the location, I created four Frame, as follows
U1s1, it feels like two or three is enough.
# frame for prompt messages and input boxes entry_frame = tk.Frame (win) # frame for operands menu_frame = tk.Frame (win) # frame for numbers major_frame = tk.Frame (win) # frame for equal signs cal_frame = tk.Frame (win) entry_frame.pack (side= "top") menu_frame.pack (side= "left") major_frame.pack () cal_frame.pack (side= "right")
Let's make an input box, which is divided into two parts.
One part is the Chinese character part, prompt message, use Label control
Part of it is the input box, using the Entry control
T_label = tk.Label (entry_frame, text = "Please enter:") t_label.pack (side='left') word_entry = tk.Entry (entry_frame, fg = "blue", # input font color, set to blue bd = 3, # border width width = 39, # input box length justify = 'right' # set alignment to right) word_entry.pack ()
Then arrange the operation symbols on the left below
For char in ['+','-','×','/']: myButton (menu_frame, char, word_entry)
Where the myButton class instantiates a button, and when the button is clicked, the corresponding text appears in the input box
There was a problem at that time-you could not get the text on the button by clicking the button. After solving the problem, you wrote a blog, the portal.
Enumerate the numbers in the same way
For i in range (4): num_frame = tk.Frame (major_frame) num_frame.pack () if I < 3: for count in range (3*i+1, 3*i+4): myButton (num_frame, count, word_entry, side= (I, count)) continue myButton (num_frame, 0, word_entry, side= (I, 0))
Of course, the reset button and the calculation button should not be forgotten.
The final calculation is a little lazy, directly use entry.get () to get the formula to be calculated, use the eval () function to calculate, if the format is wrong, the pop-up window prompt
Def calculate (entry): try: result = entry.get () # if no string exists in the input box Then the = button does not work if result =': return result = eval (result) entry.delete (0, "end") entry.insert (0, str (result)) except: tkinter.messagebox.showerror ("error", "format error!\ nPlease re-enter!") Reset_btn = tk.Button (cal_frame, text = 'reset', activeforeground = "blue", activebackground = "pink", width = "13", command = lambda: word_entry.delete (0, "end") .pack (side= "left") result_btn = tk.Button (cal_frame, text ='=', activeforeground = "blue", activebackground = "pink", width = "13") Command = lambda: calculate (word_entry) .pack (side= "right")
All codes
Major.py
#-*-coding=utf-8-*-# @ Time: 2021-3-4 13 Author @ Author: lhys# @ FileName: major.pymyName = ringing dogs' Welcome, my master! My Name is: _ |\ / | |\ / | / / |\ / | / / | |\ _ / | / / _ / | _ | |\ /\ | |\ /\ | | | | _ |\ /\ | |\ /\ _ | | | / / | | _ | / | / / | _ | _ | | _ | / /''print (myName) import tkinter as tkfrom tools import * win = tk.Tk () win.title (' calculator') entry_frame = tk.Frame (win) menu_frame = tk.Frame (win) major_frame = tk.Frame (win) cal_ Frame = tk.Frame (win) entry_frame.pack (side= "top") menu_frame.pack (side= "left") major_frame.pack () cal_frame.pack () # input box t_label = tk.Label (entry_frame Text = "Please enter:") t_label.pack (side='left') word_entry = tk.Entry (entry_frame, fg = "blue", bd = 3, width = 39, justify = 'right') word_entry.pack () # menu bar for char in [' +','-','×','/']: myButton (menu_frame, char, word_entry) button_side = ['right' 'left'] for i in range (4): num_frame = tk.Frame (major_frame) num_frame.pack () if I < 3: for count in range (3*i+1, 3*i+4): myButton (num_frame, count, word_entry, side= (I, count)) continue myButton (num_frame, 0, word_entry, side= (I, 0)) reset_btn = tk.Button (cal_frame Text = 'reset', activeforeground = "blue", activebackground = "pink", width = "13", command = lambda: word_entry.delete (0, "end") .pack (side= "left") result_btn = tk.Button (cal_frame, text ='=', activeforeground = "blue", activebackground = "pink", width = "13") Command = lambda: calculate (word_entry) .pack (side= "right") win.mainloop ()
Tools.py
#-*-coding=utf-8-*-# @ Time: 2021-3-4 13 Author @ Author: lhys# @ FileName: try: result = entry.get () if result ='': return result = eval (result) print (result) entry.delete (0, "end") entry.insert (0 Str (result)) except: tkinter.messagebox.showerror ("error", "incorrect format!\ nPlease re-enter!") Class myButton (): def _ init__ (self, frame, text, entry, * * kwargs): side = kwargs.get ('side') if' side' in kwargs else () self.btn = tkinter.Button (frame, text = text, activeforeground= "blue", activebackground= "pink", width= "13", command=lambda: entry.insert ("end") Text) if side: self.btn.grid (row=side [0], column=side [1]) else: self.btn.pack () Thank you for reading this article carefully I hope the article "how to use python to make a simple calculator with tkinter" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.