Tkinter Label
The Label( ) method can be used to create a text or image label within a window, and it has the following syntax format.
Label(master, options, ...)
The first parameter of the Label() method is the parent object, indicating which parent object (think of it as a parent window or container) this label will be built inside.
The following are other commonly used options parameters within the Label() method.
(1) anchor
: if the space is larger than required, control the position of the label, the default is CENTER (centered)
(2) bg or background
: background color.
(3) bitmap
: use the default icon as the label content.
(4) borderwidth or bd
: label border width, the default is 1.
(5) compound
: you can set the label contains images and text, the relationship between each other’s position.
(6) cursor
: the shape of the mouse cursor when it is above the label.
(7) fg or foreground
: foreground color.
(8) font
: optional font, font style and size.
(9) height
: the height of the label in characters.
(10) image
: the label is rendered as an image.
(11) justify
: the existence of multiple lines of text when the last line of alignment, can take the value of LEFT/CENTER/RIGHT (left/center/right), the default is centered alignment.
(12) padx/pady
: the spacing between the label text and the label area, the unit is pixels.
(13) relief
: the default is relief=FLAT, which can control the outer frame of the label.
(14) text
: label content, if there is "\n" then you can enter multiple lines of text.
(15) textvariable
: you can set the label to display in a variable way.
(16) underline
: you can set the first few text underline, starting from 0, the default is -1, that is, no underline.
(17) width
: width of the label, the unit is characters.
(18) wraplength
: this article to how much width after the line break, the unit is pixels.
We can also refer to the above parameter settings as attribute settings when designing programs.
Example#1
Create a label that reads I like tkinter
and also lists the Label’s data type in the Python Shell window.
from tkinter import *
root = Tk()
root.title("ch2_1")
label = Label(root,text="I like tkinter")
label.pack() # Packaging and positioning components
print(type(label)) # <class 'tkinter.Label'>
root.mainloop()
Output:
The image on the right below shows the result of increasing the window width by mouse dragging, and you can see the full window title.
The window size on the left above is the default size, and obviously the window height will be smaller than if there were no controls, because tkinter will only arrange enough space to display the controls. The pack( ) method in line 7 above mainly wraps the widget control of the window and positions the object of the window, so you can see the above widget control inside the window where the result is executed. Also, we can see the result of the label data type in the Python Shell window as tkinter.
The above knowledge is important because if you design a complex GUI program in the future, you will always need to use the object of the Widget control to do further operations, and you will need to use this object at this time.
If you see GUI programs designed by other people on the web or in the future, for lines 6 and 7 above, you will often see that they can be combined into one line, see the following program example –
Example#2
Redesign ch2_1.py using Label().pack()
method.
from tkinter import *
root = Tk()
root.title("ch2_1")
label = Label(root,text="I like tkinter").pack()
root.mainloop()
Output:
The result of the GUI window is the same as ch2_1.py.
But then the type of label data passed back in the Python Shell window is as follows.
Label is obviously not of type tkinter. If you need to use this object to further manipulate the widget control, an error will occur, which the reader should pay special attention to.
Line 4 of the above program has "label=", which can be omitted because its data type is no longer correct.
Label(root,text="I like tkinter").pack()
As for future programming, I suggest to separate the object declaration from the pack method, or use this declaration and pack together if you will not use the object for further operations, so that it is less prone to errors.