In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use python Tkinter". In daily operation, I believe many people have doubts about how to use python Tkinter. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use python Tkinter". Next, please follow the editor to study!
Design
We are going to write a simple GUI tool that converts feet to me ters.
The program will have an input box to enter the number of feet, a display box to display converted numbers, several text areas for prompt characters, and equally important, there must be a conversion trigger button.
Code from tkinter import * from tkinter import ttkdef calculate (* args): try: value = float (feet.get ()) meters.set (0.3048 * value * 10000.0 + 10000.0) / 10000.0) except ValueError: passroot = Tk () root.title ("Feet to Meters") mainframe = ttk.Frame (root, padding= "3 315") mainframe.grid (column=0, row=0, sticky= (N, W, E, S)) mainframe.columnconfigure (0 Weight=1) mainframe.rowconfigure (0, weight=1) feet = StringVar () meters = StringVar () feet_entry = ttk.Entry (mainframe, width=7, textvariable=feet) feet_entry.grid (column=2, row=1, sticky= (W, E)) ttk.Label (mainframe, textvariable=meters) .grid (column=2, row=2, sticky= (W, E) ttk.Button (mainframe, text= "conversion", command=calculate) .grid (column=3, row=3, sticky=W) ttk.Label (mainframe, text= "feet") .grid (column=3, row=1, sticky=W) ttk.Label Text= "equals") .grid (column=1, row=2, sticky=E) ttk.Label (mainframe, text= "rice") .grid (column=3, row=2, sticky=W) for child in mainframe.winfo_children (): child.grid_configure (padx=5, pady=5) feet_entry.focus () root.bind (', calculate) root.mainloop () step by step
To write a Tk program, we first introduce the module of Tk in ter.
From tkinter import * from tkinter import ttk
These two lines tell Python that our program needs these two modules. First of all, tkinter is the standard package for Tk, and when it is loaded, it will also cause the Tk library to be loaded on your system. Second, ttk is a new addition in Tk 8.5.It provides access to the set of Tk theme widgets introduced in Tk 8.5.The basic idea is to separate the code that implements the behavior of the widget from the code that implements its appearance as much as possible, which we won't delve into here.
It is worth noting that we have imported all the functions from the tk in ter module, so we can call all the functions of tk in ter directly without adding a prefix. But we only imported the ttk module, so we should add the ttk prefix when using the functions in the ttk module.
If you change the old code to new code, you will find that the name of Tk in ter has changed from uppercase to lowercase tkinter, starting with Python 3.0.
Root = Tk () root.title ("Feet to Meters") mainframe = ttk.Frame (root, padding= "3 315 15") mainframe.grid (column=0, row=0, sticky= (N, W, E, S) mainframe.columnconfigure (0, weight=1) mainframe.rowconfigure (0, weight=1)
The cal cu late function will be explained later, and it is placed in front of it because many of the following statements need to be called.
The root = Tk () statement builds a main window, also known as a root window. Use root.title ("title") to give the window a name. Ttk.Frame (root, padding= "3 3 15 15") establishes a framework with three rows and three columns, with 15 pixels. We put the framework in the root window, except that all our components are placed in the frame instead of the root window.
Generally speaking, we can put all the components (Wid get) in the root window, but the background of the main window may not match the components we added. In this case, we add an intermediate framework (Frame) and put the components on the intermediate framework to ensure that the content matches the background.
Columncoonfigure and rowconfigure tell Tk that when the main window is resized, the Frame framework on top of it should also change to take up extra space.
Feet = StringVar () meters = StringVar () feet_entry = ttk.Entry (mainframe, width=7, textvariable=feet) feet_entry.grid (column=2, row=1, sticky= (W, E) ttk.Label (mainframe, textvariable=meters) .grid (column=2, row=2, sticky= (W, E)) ttk.Button (mainframe, text= "conversion", command=calculate) .grid (column=3, row=3, sticky=W)
The above statements create three components on the framework (main frame), the input box, the output area (La bel, which is used to place the results of the transformation), and the conversion button.
For each component (Wid get), we need to do two things:
Create
Place
They are all classes in the ttk module. When creating, we specify the parameters passed in: the frame to be placed, the size, the characters in the button, and so on. As for the meaning of textvariable, it refers to the variable associated with the value in the input or output box, and the type of this variable is the object of StringVar.
We use grid (grid) for geometric management, meaning that this component will be placed in which place (which row, which column), sticky indicates the component in the assigned grid cell (grid cell) in the arrangement (line up) way, E, W, S, N means north and south, similar to the text editor in the center, left, right, and so on.
Ttk.Label (mainframe, text= "feet") .grid (column=3, row=1, sticky=W) ttk.Label (mainframe, text= "equals") .grid (column=1, row=2, sticky=E) ttk.Label (mainframe, text= "meters") .grid (column=3, row=2, sticky=W)
The above three lines create three text labels for the specified content (La bel) and place them in the specified location.
For child in mainframe.winfo_children (): child.grid_configure (padx=5, pady=5) feet_entry.focus () root.bind ('', calculate)
These four lines of code make a beautiful finale for our graphics.
The first two lines of code iterate through all the components placed in main frame and add borders around them so that they don't all huddle together. Of course, you can traverse these components individually and set them one by one, but this is not a convenient approach.
The third line of code tells Tk to focus the cursor on the input box while the program is running so that the user does not have to click on the input box again.
The fourth line of code tells Tk that the cal cu late function is called when the user presses Return (En ter in Win dows). This is the same as pressing the button to call the cal cu late function.
Def calculate (* args): try: value = float (feet.get ()) meters.set (0.3048 * value * 10000.0 + 10000.0) / 10000.0) except ValueError: pass
Here we define a cal cu late function call that will be called when Return, Enter (Win dows), or the convert button is pressed. It takes the value entered by the user from the input box, converts it to a value in meters, and sets the value in the input box to the correct result.
You can clearly see that the cal cu late function changes the display of numeric values in their respective input boxes (Entry) and output boxes (Label) by getting feet and setting meters. When the user's input changes, the value of the corresponding feet will be modified to the corresponding input value; when the meters is modified, the value displayed in the corresponding output box (La bel) will also change. This is why you also specify the value of textvariable when defining feet_entry (input box) and label (output box), and its value should be an object of StringVar. Such as the following example:
Feet = StringVar () meters = StringVar () feet_entry = ttk.Entry (mainframe, width=7, textvariable=feet) ttk.Label (mainframe, textvariable=meters). Grid (column=2, row=2, sticky= (W, E)) root.mainloop () so far, the study on "how to use python Tkinter" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.