📜  Python| Tkinter 中的 grid() 方法

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

Python| Tkinter 中的 grid() 方法

网格几何管理器将小部件放在二维表中。主小部件分为许多行和列,结果表中的每个“单元格”都可以容纳一个小部件。网格管理器是 Tkinter 中最灵活的几何管理器。如果您不想学习如何以及何时使用所有三个管理器,您至少应该确保学习这个。考虑下面的例子——

使用管理器创建此布局是可能的,但它需要一些额外的框架小部件,并且需要大量工作才能使事情看起来不错。如果您改用网格管理器,则每个小部件只需要一次调用即可正确布局所有内容。使用网格管理器很容易。只需创建小部件,并使用网格方法告诉管理器将它们放置在哪一行和哪一列中。您不必事先指定网格的大小;管理器会自动从其中的小部件中确定。

代码#1:

Python3
# import tkinter module
from tkinter import * from tkinter.ttk import *
 
# creating main tkinter window/toplevel
master = Tk()
 
# this will create a label widget
l1 = Label(master, text = "First:")
l2 = Label(master, text = "Second:")
 
# grid method to arrange labels in respective
# rows and columns as specified
l1.grid(row = 0, column = 0, sticky = W, pady = 2)
l2.grid(row = 1, column = 0, sticky = W, pady = 2)
 
# entry widgets, used to take entry from user
e1 = Entry(master)
e2 = Entry(master)
 
# this will arrange entry widgets
e1.grid(row = 0, column = 1, pady = 2)
e2.grid(row = 1, column = 1, pady = 2)
 
# infinite loop which can be terminated by keyboard
# or mouse interrupt
mainloop()


Python3
# import tkinter module
from tkinter import * from tkinter.ttk import *
 
# creating main tkinter window/toplevel
master = Tk()
 
# this will create a label widget
l1 = Label(master, text = "Height")
l2 = Label(master, text = "Width")
 
# grid method to arrange labels in respective
# rows and columns as specified
l1.grid(row = 0, column = 0, sticky = W, pady = 2)
l2.grid(row = 1, column = 0, sticky = W, pady = 2)
 
# entry widgets, used to take entry from user
e1 = Entry(master)
e2 = Entry(master)
 
# this will arrange entry widgets
e1.grid(row = 0, column = 1, pady = 2)
e2.grid(row = 1, column = 1, pady = 2)
 
# checkbutton widget
c1 = Checkbutton(master, text = "Preserve")
c1.grid(row = 2, column = 0, sticky = W, columnspan = 2)
 
# adding image (remember image should be PNG and not JPG)
img = PhotoImage(file = r"C:\Users\Admin\Pictures\capture1.png")
img1 = img.subsample(2, 2)
 
# setting image with the help of label
Label(master, image = img1).grid(row = 0, column = 2,
       columnspan = 2, rowspan = 2, padx = 5, pady = 5)
 
# button widget
b1 = Button(master, text = "Zoom in")
b2 = Button(master, text = "Zoom out")
 
# arranging button widgets
b1.grid(row = 2, column = 2, sticky = E)
b2.grid(row = 2, column = 3, sticky = E)
 
# infinite loop which can be terminated
# by keyboard or mouse interrupt
mainloop()


输出:

代码 #2:创建如上所示的布局。

Python3

# import tkinter module
from tkinter import * from tkinter.ttk import *
 
# creating main tkinter window/toplevel
master = Tk()
 
# this will create a label widget
l1 = Label(master, text = "Height")
l2 = Label(master, text = "Width")
 
# grid method to arrange labels in respective
# rows and columns as specified
l1.grid(row = 0, column = 0, sticky = W, pady = 2)
l2.grid(row = 1, column = 0, sticky = W, pady = 2)
 
# entry widgets, used to take entry from user
e1 = Entry(master)
e2 = Entry(master)
 
# this will arrange entry widgets
e1.grid(row = 0, column = 1, pady = 2)
e2.grid(row = 1, column = 1, pady = 2)
 
# checkbutton widget
c1 = Checkbutton(master, text = "Preserve")
c1.grid(row = 2, column = 0, sticky = W, columnspan = 2)
 
# adding image (remember image should be PNG and not JPG)
img = PhotoImage(file = r"C:\Users\Admin\Pictures\capture1.png")
img1 = img.subsample(2, 2)
 
# setting image with the help of label
Label(master, image = img1).grid(row = 0, column = 2,
       columnspan = 2, rowspan = 2, padx = 5, pady = 5)
 
# button widget
b1 = Button(master, text = "Zoom in")
b2 = Button(master, text = "Zoom out")
 
# arranging button widgets
b1.grid(row = 2, column = 2, sticky = E)
b2.grid(row = 2, column = 3, sticky = E)
 
# infinite loop which can be terminated
# by keyboard or mouse interrupt
mainloop()

输出:

警告:切勿在同一个主窗口中混合grid()pack()