Python Tkinter | grid_location() 和 grid_size() 方法
Tkinter 用于开发 GUI(图形用户界面)应用程序。它支持各种小部件以及各种小部件方法或通用小部件方法。
grid_location() 方法——
此方法返回一个包含任何指定小部件的 (column, row) 的元组。由于此方法是一个小部件方法,因此您不能将其与主对象(或 Tk() 对象)一起使用。为了使用此方法,您应该首先创建一个 Frame 并将其视为父级(或主控)。
Syntax: widget.grid_location(x, y)
Parameters:
x and y are the positions, relative to the upper left corner of the widget (parent widget).
在下面的示例中,grid_location() 用于获取框架小部件中小部件的位置。
Python3
# This imports all functions in tkinter module
from tkinter import * from tkinter.ttk import *
# creating master window
master = Tk()
# This method is used to get the position
# of the desired widget available in any
# other widget
def click(event):
# Here retrieving the size of the parent
# widget relative to master widget
x = event.x_root - f.winfo_rootx()
y = event.y_root - f.winfo_rooty()
# Here grid_location() method is used to
# retrieve the relative position on the
# parent widget
z = f.grid_location(x, y)
# printing position
print(z)
# Frame widget, wil work as
# parent for buttons widget
f = Frame(master)
f.pack()
# Button widgets
b = Button(f, text = "Button")
b.grid(row = 2, column = 3)
c = Button(f, text = "Button2")
c.grid(row = 1, column = 0)
# Here binding click method with mouse
master.bind("", click)
# infinite loop
mainloop()
Python3
# This imports all functions in tkinter module
from tkinter import * from tkinter.ttk import *
# creating master window
master = Tk()
# This method is used to get the size
# of the desired widget i.e number of grids
# available in the widget
def grids(event):
# Here, grid_size() method is used to get
# the total number grids available in frame
# widget
x = f.grid_size()
# printing (columns, rows)
print(x)
# Frame widget, will work as
# parent for buttons widget
f = Frame(master)
f.pack()
# Button widgets
b = Button(f, text = "Button")
b.grid(row = 1, column = 2)
c = Button(f, text = "Button2")
c.grid(row = 1, column = 0)
# Here binding click method with mouse
master.bind("", grids)
# infinite loop
mainloop()
grid_size() 方法——
此方法用于获取任何父窗口小部件中存在的网格总数。这是一种小部件方法,因此不能将其与主对象一起使用。必须创建一个 Frame 小部件。
Syntax: (columns, rows) = widget.grid_size()
Return Value: It returns total numbers of columns and rows (grids).
下面是Python代码——
Python3
# This imports all functions in tkinter module
from tkinter import * from tkinter.ttk import *
# creating master window
master = Tk()
# This method is used to get the size
# of the desired widget i.e number of grids
# available in the widget
def grids(event):
# Here, grid_size() method is used to get
# the total number grids available in frame
# widget
x = f.grid_size()
# printing (columns, rows)
print(x)
# Frame widget, will work as
# parent for buttons widget
f = Frame(master)
f.pack()
# Button widgets
b = Button(f, text = "Button")
b.grid(row = 1, column = 2)
c = Button(f, text = "Button2")
c.grid(row = 1, column = 0)
# Here binding click method with mouse
master.bind("", grids)
# infinite loop
mainloop()
输出:
每次单击鼠标按钮时,它将返回相同的值,直到不添加更多小部件或不增加行数和列数。
(3, 2)