python tkinter pack_Python Tkinter pack()用法及代码示例

打包几何图形管理器将小部件打包为行或列。我们可以使用诸如填充,展开和侧面之类的选项来控制此几何图形管理器。

与网格管理器相比,数据包管理器受到了一定的限制,但是在一些但很常见的情况下使用它要容易得多:

将小部件放入框架(或任何其他容器小部件)中,并使其充满整个框架

将许多小部件彼此放在顶部

并排放置许多小部件

代码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()

输出:

4f3bb49e2192939a44d9916011bed3d3.jpg

代码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()

输出:

d0854f65e629dfc1d48482da7efb0acc.jpg

代码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()

输出:

aab4071383966d737b59c5726e35e1c5.jpg

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值