📜  Tkinter 中的 place_info()、pack_info() 和 grid_info() 方法

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

Tkinter 中的 place_info()、pack_info() 和 grid_info() 方法

要获取有关小部件place_info()的几何管理选项的所有信息,相应地使用 tkinter 中的pack_info ()grid_info()方法。

place_info() 方法

此方法用于获取有关小部件几何管理的信息,例如窗口的位置和大小,无论是绝对值还是相对于另一个窗口。

代码 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()


输出:

地方信息方法工作

pack_info() 方法

此方法用于获取有关小部件几何管理的信息,例如展开、侧面、填充、填充值等。

代码 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() 

输出:

打包信息方法工作

grid_info() 方法

此方法用于获取有关小部件几何管理的信息,例如行号、列号、rowsapn、columnspan、填充值等。

代码 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() 

输出:

网格信息方法工作