📜  设置 TKinter 标签的位置

📅  最后修改于: 2022-05-13 01:54:55.519000             🧑  作者: Mango

设置 TKinter 标签的位置

Tkinter 是Python的标准 GUI 库。 Python中的 Tkinter 带有很多很好的小部件。 Widgets 是标准的 GUI 元素,Label 也会出现在这些 Widgets 之下
注意:更多信息请参考Python GUI – tkinter

标签:

Tkinter Label 是一个小部件,用于实现显示框,您可以在其中放置文本或图像。开发人员可以随时更改此小部件显示的文本。它还用于执行诸如下划线部分文本和跨越多行文本等任务。

例子:

设置 Tkinter 标签的位置

我们可以使用 place() 方法来设置 Tkinter 标签的位置。

示例 1:在窗口中间放置标签

Python3
import tkinter as tk
 
 
# Creating the root window
root = tk.Tk()
 
# creating the Label with
# the text Middle
Label_middle = tk.Label(root,
                        text ='Middle')
 
# Placing the Label at
# the middle of the root window
# relx and rely should be properly
# set to position the label on
# root window
Label_middle.place(relx = 0.5,
                   rely = 0.5,
                   anchor = 'center')
# Execute Tkinter
root.mainloop()


Python3
# Import Module
import tkinter as tk
 
# Create Object
root = tk.Tk()
 
# Create Label and add some text
Lower_left = tk.Label(root,text ='Lower_left')
 
# using place method we can set the position of label
Lower_left.place(relx = 0.0,
                 rely = 1.0,
                 anchor ='sw')
 
# Execute Tkinter
root.mainloop()


Python3
# Import Module
import tkinter as tk
 
# Create Object
root = tk.Tk()
 
# Create Label and add some text
Upper_right = tk.Label(root,text ='Upper_right')
 
# using place method we can set the position of label
Upper_right.place(relx = 1.0,
                  rely = 0.0,
                  anchor ='ne')
 
# Execute Tkinter
root.mainloop()


输出:

示例 2:在窗口左下方放置标签

Python3

# Import Module
import tkinter as tk
 
# Create Object
root = tk.Tk()
 
# Create Label and add some text
Lower_left = tk.Label(root,text ='Lower_left')
 
# using place method we can set the position of label
Lower_left.place(relx = 0.0,
                 rely = 1.0,
                 anchor ='sw')
 
# Execute Tkinter
root.mainloop()

输出

示例 3:在窗口右上方放置标签

Python3

# Import Module
import tkinter as tk
 
# Create Object
root = tk.Tk()
 
# Create Label and add some text
Upper_right = tk.Label(root,text ='Upper_right')
 
# using place method we can set the position of label
Upper_right.place(relx = 1.0,
                  rely = 0.0,
                  anchor ='ne')
 
# Execute Tkinter
root.mainloop()

输出