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

输出如下:
Python Tkinter中的pack()方法介绍

文章图片
代码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()

输出如下:
Python Tkinter中的pack()方法介绍

文章图片
代码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中的pack()方法介绍

文章图片
【Python Tkinter中的pack()方法介绍】首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

    推荐阅读