打包幾何圖形管理器將小部件打包為行或列。我們可以使用諸如填充,展開和側麵之類的選項來控製此幾何圖形管理器。
與網格管理器相比,數據包管理器受到了一定的限製,但是在一些但很常見的情況下使用它要容易得多:
- 將小部件放入框架(或任何其他容器小部件)中,並使其充滿整個框架
- 將許多小部件彼此放在頂部
- 並排放置許多小部件
代碼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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。