四、tk布局

本文最后更新于:2022年1月17日 晚上

GUI编程必须有的布局(Layout)

Java Swing、Qt等都有布局管理,tkinter也不例外,且一共有三种类型:

布局 描述
pack 相对布局
grid 网格布局
place 绝对布局
  1. pack布局

    默认将先使用的放在上面,然后依次向下排列,它自动给组件一个自认为合适的位置和大小,这是最简单的模式,可以直接使用pack()函数,不需要添加参数,但是不代表没有参数。下面我们来看一下源码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    class Pack:
    """Geometry manager Pack.

    Base class to use the methods pack_* in every widget."""

    def pack_configure(self, cnf={}, **kw):
    """Pack a widget in the parent widget. Use as options:
    after=widget - pack it after you have packed widget
    anchor=NSEW (or subset) - position widget according to
    given direction
    before=widget - pack it before you will pack widget
    expand=bool - expand widget if parent size grows
    fill=NONE or X or Y or BOTH - fill widget if widget grows
    in=master - use master to contain this widget
    in_=master - see 'in' option description
    ipadx=amount - add internal padding in x direction
    ipady=amount - add internal padding in y direction内边距垂直方向
    padx=amount - add padding in x direction外边距水平方向
    pady=amount - add padding in y direction外边距垂直方向
    side=TOP or BOTTOM or LEFT or RIGHT - where to add this widget.
    """
    self.tk.call(
    ('pack', 'configure', self._w)
    + self._options(cnf, kw))

    可以看到还是有不少的参数的,通过描述可以看到一些它们的功能。

    参数 功能
    after 在其他组件pack之后再pack,理解为降低优先级,可选值为某个widget
    before pack后再pack其他组件,理解为提高优先级,可选值为某个widget
    in 用哪个父组件包含,可选值为某个widget
    expand 是否随着父组件的尺寸变化而跟着改变位置,可选值为YESNO
    fill 在某个方向填充,可选值为XYNONEBOTH
    ipadx 水平方向内边距,可选值为数值
    ipady 垂直方向内边距,可选值为数值
    padx 水平方向外边距,可选值为数值
    pady 垂直方向外边距,可选值为数值
    side 组件的停靠位置或方向,可选值为TOPBOTTOMLEFTRIGHT
  2. grid布局

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    class Grid:
    """Geometry manager Grid.

    Base class to use the methods grid_* in every widget."""
    # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)

    def grid_configure(self, cnf={}, **kw):
    """Position a widget in the parent widget in a grid. Use as options:
    column=number - use cell identified with given column (starting with 0)
    columnspan=number - this widget will span several columns
    in=master - use master to contain this widget
    in_=master - see 'in' option description
    ipadx=amount - add internal padding in x direction
    ipady=amount - add internal padding in y direction
    padx=amount - add padding in x direction
    pady=amount - add padding in y direction
    row=number - use cell identified with given row (starting with 0)
    rowspan=number - this widget will span several rows
    sticky=NSEW - if cell is larger on which sides will this
    widget stick to the cell boundary
    """
    self.tk.call(
    ('grid', 'configure', self._w)
    + self._options(cnf, kw))
  3. place布局

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    class Place:
    """Geometry manager Place.

    Base class to use the methods place_* in every widget."""

    def place_configure(self, cnf={}, **kw):
    """Place a widget in the parent widget. Use as options:
    in=master - master relative to which the widget is placed
    in_=master - see 'in' option description
    x=amount - locate anchor of this widget at position x of master
    y=amount - locate anchor of this widget at position y of master
    relx=amount - locate anchor of this widget between 0.0 and 1.0
    relative to width of master (1.0 is right edge)
    rely=amount - locate anchor of this widget between 0.0 and 1.0
    relative to height of master (1.0 is bottom edge)
    anchor=NSEW (or subset) - position anchor according to given direction
    width=amount - width of this widget in pixel
    height=amount - height of this widget in pixel
    relwidth=amount - width of this widget between 0.0 and 1.0
    relative to width of master (1.0 is the same width
    as the master)
    relheight=amount - height of this widget between 0.0 and 1.0
    relative to height of master (1.0 is the same
    height as the master)
    bordermode="inside" or "outside" - whether to take border width of
    master widget into account
    """
    self.tk.call(
    ('place', 'configure', self._w)
    + self._options(cnf, kw))

四、tk布局
https://jialiangz.github.io/2021/11/23/tkinter-4/
作者
爱吃菠萝
发布于
2021年11月23日
更新于
2022年1月17日
许可协议