打包几何图形管理器将小部件打包为行或列。我们可以使用诸如填充,展开和侧面之类的选项来控制此几何图形管理器。
与网格管理器相比,数据包管理器受到了一定的限制,但是在一些但很常见的情况下使用它要容易得多:
- 将小部件放入框架(或任何其他容器小部件)中,并使其充满整个框架
- 将许多小部件彼此放在顶部
- 并排放置许多小部件
代码1:将小部件放入框架中并填充整个框架。我们可以借助扩展和填充选项来做到这一点。
# Importing tkinter module
from tkinter import * from tkinter.ttk import *
# creating Tk window
master = Tk()
# cretaing 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
b1 = Button(pane, text = "Click me !")
b1.pack(fill = BOTH, expand = True)
b2 = Button(pane, text = "Click me too")
b2.pack(fill = BOTH, expand = True)
mainloop()
输出:
代码2:将小部件彼此并排放置。我们可以通过并排选项来实现。
# Importing tkinter module
from tkinter import *
# from tkinter.ttk import *
# creating Tk window
master = Tk()
# cretaing 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
b1 = Button(pane, text = "Click me !",
background = "red", fg = "white")
b1.pack(side = TOP, expand = True, fill = BOTH)
b2 = Button(pane, text = "Click me too",
background = "blue", fg = "white")
b2.pack(side = TOP, expand = True, fill = BOTH)
b3 = Button(pane, text = "I'm also button",
background = "green", fg = "white")
b3.pack(side = TOP, expand = True, fill = BOTH)
mainloop()
输出:
代码3:
# Importing tkinter module
from tkinter import *
# from tkinter.ttk import *
# creating Tk window
master = Tk()
# cretaing 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
b1 = Button(pane, text = "Click me !",
background = "red", fg = "white")
b1.pack(side = LEFT, expand = True, fill = BOTH)
b2 = Button(pane, text = "Click me too",
background = "blue", fg = "white")
b2.pack(side = LEFT, expand = True, fill = BOTH)
b3 = Button(pane, text = "I'm also button",
background = "green", fg = "white")
b3.pack(side = LEFT, expand = True, fill = BOTH)
mainloop()
输出:
相关用法
- Python Tkinter place()用法及代码示例
- Python Tkinter maxsize()用法及代码示例
- Python Tkinter minsize()用法及代码示例
- Python Tkinter iconphoto()用法及代码示例
- Python Tkinter destroy()用法及代码示例
- Python Tkinter grid()用法及代码示例
- Python Tkinter resizable()用法及代码示例
注:本文由纯净天空筛选整理自sanjeev2552大神的英文原创作品 Python | pack() method in Tkinter。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。