📜  Python| Tkinter 中的 pack() 方法

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

Python| Tkinter 中的 pack() 方法

包几何管理器相对于早期的小部件打包小部件。 Tkinter 将所有小部件一个接一个地打包在一个窗口中。我们可以使用fillexpandside等选项来控制这个几何管理器。
网格管理器相比,管理器有些局限,但在一些但很常见的情况下使用起来要容易得多:

  • 将一个小部件放在一个框架(或任何其他容器小部件)内,并让它填满整个框架
  • 将许多小部件放在一起
  • 并排放置多个小部件

代码#1:将一个小部件放在框架内并填充整个框架。我们可以在扩展填充选项的帮助下做到这一点。

Python3
# Importing tkinter module
from tkinter import * from tkinter.ttk import *
 
# creating Tk window
master = Tk()
 
# creating a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
 
# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text = "Click me !")
b1.pack(fill = BOTH, expand = True)
 
# Button 2
b2 = Button(pane, text = "Click me too")
b2.pack(fill = BOTH, expand = True)
 
# Execute Tkinter
master.mainloop()


Python3
# Importing tkinter module
from tkinter import *
# from tkinter.ttk import *
 
# creating Tk window
master = Tk()
 
# creating a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
 
# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text = "Click me !",
            background = "red", fg = "white")
b1.pack(side = TOP, expand = True, fill = BOTH)
 
# Button 2
b2 = Button(pane, text = "Click me too",
            background = "blue", fg = "white")
b2.pack(side = TOP, expand = True, fill = BOTH)
 
# Button 3
b3 = Button(pane, text = "I'm also button",
            background = "green", fg = "white")
b3.pack(side = TOP, expand = True, fill = BOTH)
 
# Execute Tkinter
master.mainloop()


Python3
# Importing tkinter module
from tkinter import *
# from tkinter.ttk import *
 
# creating Tk window
master = Tk()
 
# creating a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
 
# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text = "Click me !",
            background = "red", fg = "white")
b1.pack(side = LEFT, expand = True, fill = BOTH)
 
# Button 2
b2 = Button(pane, text = "Click me too",
            background = "blue", fg = "white")
b2.pack(side = LEFT, expand = True, fill = BOTH)
 
# Button 3
b3 = Button(pane, text = "I'm also button",
            background = "green", fg = "white")
b3.pack(side = LEFT, expand = True, fill = BOTH)
 
# Execute Tkinter
master.mainloop()


输出:

代码 #2:将小部件彼此叠放并并排放置。我们可以通过side option来做到这一点。

Python3

# Importing tkinter module
from tkinter import *
# from tkinter.ttk import *
 
# creating Tk window
master = Tk()
 
# creating a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
 
# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text = "Click me !",
            background = "red", fg = "white")
b1.pack(side = TOP, expand = True, fill = BOTH)
 
# Button 2
b2 = Button(pane, text = "Click me too",
            background = "blue", fg = "white")
b2.pack(side = TOP, expand = True, fill = BOTH)
 
# Button 3
b3 = Button(pane, text = "I'm also button",
            background = "green", fg = "white")
b3.pack(side = TOP, expand = True, fill = BOTH)
 
# Execute Tkinter
master.mainloop()

输出:

代码#3:

Python3

# Importing tkinter module
from tkinter import *
# from tkinter.ttk import *
 
# creating Tk window
master = Tk()
 
# creating a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
 
# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text = "Click me !",
            background = "red", fg = "white")
b1.pack(side = LEFT, expand = True, fill = BOTH)
 
# Button 2
b2 = Button(pane, text = "Click me too",
            background = "blue", fg = "white")
b2.pack(side = LEFT, expand = True, fill = BOTH)
 
# Button 3
b3 = Button(pane, text = "I'm also button",
            background = "green", fg = "white")
b3.pack(side = LEFT, expand = True, fill = BOTH)
 
# Execute Tkinter
master.mainloop()

输出: