Tkinter 中的 place_info()、pack_info() 和 grid_info() 方法
要获取有关小部件place_info()的几何管理选项的所有信息,相应地使用 tkinter 中的pack_info ()和grid_info()方法。
此方法用于获取有关小部件几何管理的信息,例如窗口的位置和大小,无论是绝对值还是相对于另一个窗口。
Syntax : widget.place_info()
Parameter: None
Returns : Returns a dictionary of the info of the place options of the current widget
代码 1:
Python3
# Importing all functions/classes
# from tkinter module
from tkinter import *
# toplevel window
root = Tk()
# setting window size
root.geometry("810x350")
# create a Label widget whose
# place info is to be obtained
rect = Label(root,
text = "MY PLACE INFO IS SHOWN BELOW",
bg = "pink")
# place a widget in a specific
# position in the parent widget.
rect.place(rely = 0.1, relx = 0.2,
relwidth = 0.6,
relheight = 0.3)
# widget displaying place info of rect
label = Label(root)
# place a widget in a specific
# position in the parent widget.
label.place(rely = 0.6)
# get a info of the place
label['text'] = rect.place_info()
# start the GUI
root.mainloop()
Python3
# Importing all functions/classes
# from tkinter module
from tkinter import *
# toplevel window
root = Tk()
# create a Label widget whose
# pack info is to be obtained
rect = Label(root,
text = "MY PACK INFO IS SHOWN BELOW",
bg = "pink")
# placing them in a specific position
# in the parent widget.
rect.pack(expand = True)
# create a Label
label = Label(root)
label.pack()
label['text'] = rect.pack_info()
# start the GUI
root.mainloop()
Python3
# Importing all functions/classes
# from tkinter module
from tkinter import *
# toplevel window
root = Tk()
# widget whose grid info is to be obtained
rect = Label(root,
text = "MY GRID INFO IS SHOWN BELOW",
bg = "pink")
# grid method is used for placing
# the widgets at respective positions
# in table like structure .
rect.grid(stick = N)
# create a label
label = Label(root)
label.grid()
label['text'] = rect.grid_info()
# start the GUI
root.mainloop()
输出:
此方法用于获取有关小部件几何管理的信息,例如展开、侧面、填充、填充值等。
Syntax : widget.pack_info()
Parameter: None
Returns : Returns a dictionary of the info of the pack options of the current widget
代码 2:
Python3
# Importing all functions/classes
# from tkinter module
from tkinter import *
# toplevel window
root = Tk()
# create a Label widget whose
# pack info is to be obtained
rect = Label(root,
text = "MY PACK INFO IS SHOWN BELOW",
bg = "pink")
# placing them in a specific position
# in the parent widget.
rect.pack(expand = True)
# create a Label
label = Label(root)
label.pack()
label['text'] = rect.pack_info()
# start the GUI
root.mainloop()
输出:
此方法用于获取有关小部件几何管理的信息,例如行号、列号、rowsapn、columnspan、填充值等。
Syntax : widget.grid_info()
Parameter: None
Returns : Returns a dictionary of the info of the grid options of the current widget
代码 3:
Python3
# Importing all functions/classes
# from tkinter module
from tkinter import *
# toplevel window
root = Tk()
# widget whose grid info is to be obtained
rect = Label(root,
text = "MY GRID INFO IS SHOWN BELOW",
bg = "pink")
# grid method is used for placing
# the widgets at respective positions
# in table like structure .
rect.grid(stick = N)
# create a label
label = Label(root)
label.grid()
label['text'] = rect.grid_info()
# start the GUI
root.mainloop()
输出: