In this article, I will talk about how to assign values to form properties and how to obtain form property values in tkinter programming.
Understand what properties a form has
The form has properties that can be set. We can use the keys() method to check.
print(root.keys()) in the following code The function is the attribute that can be set in the terminal output form root. The complete code is as follows:
1 from tkinter import * 2 root=Tk() 3 print(root.keys()) # Output form properties 4 root.mainloop()
In my computer (windows 10 system), the output results are as follows:
['bd', 'borderwidth', 'class', 'menu', 'relief', 'screen', 'use', 'background', 'bg', 'colormap', 'container', 'cursor', 'height', 'highlightbackground', 'highlightcolor', 'highlightthickness', 'padx', 'pady', 'takefocus', 'visual', 'width']
It seems that the form has many properties, but there are only a few commonly used property values. We can ignore other less commonly used property settings for the time being.
Use the config() method to set the form properties
There are two methods for setting form properties and component (control) properties. The first method is to use config() Or configure()
1 root.config(bg='yellow',cursor='hand2',width=200,height=100) 2 root.configure(bg='yellow',cursor='hand2',width=200,height=100)
The function of the above two codes is the same. (I will use the config() method later) the function of the above code is to set the window background to yellow, and the mouse shape within the window becomes hand shape, with a width of 200 and a height of 100.
All codes are as follows:
1 from tkinter import * 2 root=Tk() 3 root.config(bg='yellow',cursor='hand2',width=200,height=100) 4 root.mainloop()
Let's run it and see the results:
Using the config() method, you can set one or more attributes at a time.
Set form properties through dictionary keys
This method can only be set separately for one property. For example:
1 root['bg']='yellow' 2 root['cursor']='hand2' 3 root['width']=200 4 root['height']=100
If this method is set for a non-existent property, the system will report a TclError error error.
Method for obtaining form properties 1
Change the method of using dictionary keys to set the form explained above. Go to the = sign and don't assign a value, you can get the attribute value. For example:
root['height'] # Get form height root['bg'] # Gets the form background color root['cursor'] # Gets the mouse style of the form
We can assign the obtained attribute to other variables, or print it directly.
1 from tkinter import * 2 root=Tk() 3 root['height']=300 4 root['bg']='red' 5 root['cursor']='hand2' 6 print(root['height']) 7 print(root['bg']) 8 print(root['cursor']) 9 root.mainloop()
In the above code, we can print the attribute value of the form with print.
Note that if the form does not have a property value set, you cannot get the correct value in this way. If we remove 3, 4 and 5 sentences of code and run it again, we will find that the output result indicates an error.
Method for obtaining form property value 2
The second method is to use the cget() method to obtain the form property value. For example:
root.cget('height') root.cget('bg') root.cget('cursor')
Similarly, we can assign the obtained attribute to other variables or print it directly.
1 from tkinter import * 2 root=Tk() 3 root.config(height=100,bg='red',cursor='hand2') 4 print(root.cget('height')) 5 print(root.cget('bg')) 6 print(root.cget('cursor')) 7 root.mainloop()
In the above code, we can print the attribute value of the form with print.
Note that this method is the same as the first method. If the form does not have a property value set, the correct property value cannot be obtained. If we remove the third sentence of code and run it again, we will find that the output result indicates an error.
Summary: the above two methods of setting properties and two methods of setting properties are also effective for the components to be explained later (in other programming languages, components are also called controls).
Other methods get the width and height of the form
root.winfo_width() # Gets the width of the form
root.winfo_height() # Gets the height of the form
We use the following code to output the width and height of the form
1 from tkinter import * 2 root=Tk() 3 root.geometry('300x150+888+444') # Set form size and position 4 root.update() # Refresh form 5 print(root.winfo_width()) # Output form width 6 print(root.winfo_height()) # Output form height 7 root.mainloop()
Note the code in line 4. This is a refresh form statement. If this sentence is missing, the output result is incorrect. You must add this line of code.
Remove the code for setting the size of the form in line 3, and you can get the width and height of the form. No
Other methods to get the position of the form
root.winfo_x() # Gets the left margin of the form
root.winfo_y() # Gets the top margin of the form
Output the position of the form with the following code:
1 from tkinter import * 2 root=Tk() 3 root.geometry('300x150+888+444') # Set form size and position 4 root.update() # Refresh form 5 print(root.winfo_x()) # Output form left margin 6 print(root.winfo_y()) # Output form top margin 7 root.mainloop()
tkinter form centered code
In the last article, I briefly explained the setting of the window position on the screen. Here, I'll go into a little detail. Code for setting the position of the form, such as:
root.geometry('±100±200')
The first parameter is: + value, which indicates the distance from the left side of the window to the left side of the screen
- Numeric value indicating the distance from the right side of the window to the right side of the screen
The second parameter is: + value, which indicates the distance between the upper edge of the window and the upper edge of the screen
- numeric value indicating the distance from the lower edge of the window to the lower edge of the screen
You can change the third line of the above code into one of the following code to see the different positions of the form on the screen.
root.geometry('+100+200') # The form is in the upper left corner of the screen
root.geometry('-100-200') # The form is in the lower right corner of the screen
root.geometry('+100-200') # The form is in the lower left corner of the screen
root.geometry('-100+200') # The form is in the upper right corner of the screen
But in the actual programming, it may be more practical to center the form. The code is as follows:
1 a,b=300,150 # a Is the width of the form, b Is the height of the form 2 c=(root.winfo_screenwidth()-a)/2 # (Screen width-Form width)/2 3 d=(root.winfo_screenheight()-b)/2 # (Screen height-Form height)/2 4 root.geometry('%dx%d+%d+%d' % (a,b,c,d)) # c Is the distance from the left side of the window to the left side of the screen, d Is the distance from the upper edge of the window to the upper edge of the screen
The above tkinter window displays the code in the middle. You can collect it. When using it, just change the value settings of a and B.
Explanation:
The code of root.geometry ('% DX% d +% d +% d'% (a, B, C, d)) requires you to understand the string formatting knowledge of python. Here, you use the% operator to format the string.
root.winfo_screenwidth() is the width of the screen. Parentheses cannot be omitted
root.winfo_screenheight() To get the height of the screen, parentheses cannot be omitted.
This article comes from jiheng.com, website: www.wb98.com The website also has a series of related course articles. Those who are interested can go to.
In the next article, we will explain tkinter's components (widget s in English). First, we will talk about the "tag" component.