Tkinter 8.5 参考手册: a GUI for Python (四)

本文深入探讨了Tkinter框架中网格布局管理方法,包括如何使用`grid_bbox()`获取组件边界,`grid_forget()`与`grid_remove()`使组件消失并记录配置,`grid_info()`获取组件选项,`grid_location()`定位组件位置,`grid_propagate()`强制组件大小,以及如何通过`columnconfigure()`和`rowconfigure()`配置列和行大小,实现组件随窗口大小动态调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


4.2. 其他grid管理方法

这些与grid相关的方法被定义在所有的组件:

w.grid_bbox(column=None, row=None, col2=None, row2=None)

返回一个4元素的元祖,描述了组件 w 的grid系统中一部分或者全部的边框。 前两个数字返回区域左上角的 x 和y 坐标,后两个数字是宽度和高度。

如果传入 column 和 row 参数, 返回的边框描述了在该行列的单元格的区域。如果你也传入了 col2 和 row2 参数, 返回的边框描述了在该行列 grid 从从列 column 到 col2 ,从行 row 到 row2 包含的区域。

例如, w.grid_bbox(0, 0, 1, 1) 返回四个单元格的边框,不是一个。

w.grid_forget()

这个方法使组件 w 从屏幕上消失。 它仍然存在,只是看不见了。 你可以使用 .grid() 使它重新出现, 但是不保留它的网格操作。

w.grid_info()

返回一个字典, 它的关键字是 w 的选项名, 和选项名对应的值。

w.grid_location(xy)

给定一个相对于容器组件的相对坐标 (xy) , 此方法返回一个元祖 (colrow) ,描述了组件 w 网格系统包含屏幕坐标的单元格

w.grid_propagate()

通常情况下,所有的组件都调整它们的大小,这意味着它们调正大小以适应内容。 但是, 有时候你想要强制设定一个组件的大小,忽视内容。 为了做到这一点,调用 w.grid_propagate(0) ,这里 w 是你想要强制设置大小的组件。

w.grid_remove()

这个方法类似 .grid_forget(), 但是它的选项会被记住,所以如果你使用 .grid() 使它重现, 它将使用同样的网格配置选项。

w.grid_size()

返回一个2元素的元祖,包含了列数和行数。 在组件 的网格系统中。

w.grid_slaves(row=None, column=None)

返回一个由组件 w 管理的子组件列表,如果没有提供参数,你将得到一个所有的管理组件列表。 用 row= argument 选择某一行的组件列表, 或者 column= argument 选择某一列的组件列表。


4.3. 配置行列的大小

除非你采取一定的措施,一个给定的组件内部的网格列的宽度将等于其最宽的单元格的宽度,一个网格行的高度将是其最高的单元格的高度。组件的sticky 属性,如果它不完全填充的单元格,只被放置在那里

如果你想覆盖列和行的自动调整大小,请使用包含网格布局的父组件 w 的这些方法:

w.columnconfigure(Noption=value, ...)

组件 内部网格布局,配置列 N ,使给定的value option 。对于选项,请参阅下表。

w.rowconfigure(Noption=value, ...)

组件 内部网格布局,配置行 N 使给定的value option 。对于选项,请参阅下表。

下面是用于配置的列和行的尺寸的选项。

Table 2. Column and row configuration options for the .grid() geometry manager

minsize以像素为单位的列或行的最小尺寸。如果有什么的给定列或行中,也即使您使用此选项它将不会出现,。
pad像素数将被添加到给定的行列,增量加到最大的单元格的行或列。
weight使用此选项使列或行拉伸,提供一个值,分配额外的空间时,给出了这样的列或行的相对权重。例如,如果一个部件 w 包含一个网格布局,这些线将分发四分之三的额外空间到第一列和四分之一到第二列:
    w.columnconfigure(0, weight=3)
    w.columnconfigure(1, weight=1)
如果不使用此选项,则列或行不会延长。



4.4. 使跟窗口大小可调

你想让用户调整整个应用程序的窗口大小,并且分配内部部件额外的空间吗? 这些需要一些不明显的操作。

对于行列大小管理来说,使用这些技术是很必要的。 详细描述在Section 4.3, “Configuring column and row sizes”, 使你的应用程序部件网格可拉伸。 当然,单独使用是不够的。

考虑到琐碎应用详细描述在 Section 2, “A minimal application”, 其中只包含一个Quit按钮。如果你运行这个应用程序,并调整窗口的大小,按钮保持相同的大小,窗口内居中。

这是在 minimal application中一个 .__createWidgets() 的一个替代方法。 在这个版本, Quit 按钮总是填充所有可用空间。

    def createWidgets(self):
        top=self.winfo_toplevel()                1
        top.rowconfigure(0, weight=1)            2
        top.columnconfigure(0, weight=1)         3
        self.rowconfigure(0, weight=1)           4
        self.columnconfigure(0, weight=1)        5
        self.quit = Button(self, text='Quit', command=self.quit)
        self.quit.grid(row=0, column=0,          6
            sticky=tk.N+tk.S+tk.E+tk.W)
1

 “top level window”是屏幕上的最外层的窗口。然而,这个窗口是不是你的应用程序窗口,它是应用实例的父。要获得顶层窗口,在你应用程序中的任何组件调用 .winfo_toplevel() 方法 ; 参考 Section 26, “Universal widget methods”.

2

这行使顶层窗口的的第0行网格拉伸。

3

这行使顶层窗口的的第0列网格拉伸。

4

使应用程序组件的的第0行网格拉伸。

5

使应用程序组件的的第0列网格拉伸。

6

参数 sticky=tk.N+tk.S+tk.E+tk.W 使按钮扩大以填充他的单元格网格。

这是另一个必须被做的改变。 在构造函数中, 向下面那样改变第二行:

    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid(sticky=tk.N+tk.S+tk.E+tk.W)
        self.createWidgets()

参数 sticky=tk.N+tk.S+tk.E+tk.W 对于 self.grid() 是必要的,使应用程序部件将扩大,以填补顶层窗口的网格的单元格。




Abstract Describes the Tkinter widget set for constructing graphical user interfaces (GUIs) in the Python programming language. This publication is available in Web form1 and also as a PDF document2. Please forward any comments to tcc-doc@nmt.edu. Table of Contents 1. What is Tkinter?.......................................................................................................................3 2. A minimal application..............................................................................................................3 3. Definitions..............................................................................................................................4 4. Layout management.................................................................................................................5 4.1. The .grid() method....................................................................................................5 4.2. Other grid management methods...................................................................................6 4.3. Configuring column and row sizes.................................................................................7 4.4. Making the root window resizeable................................................................................8 5. Standard attributes...................................................................................................................8 5.1. Dimensions...................................................................................................................9 5.2. The coordinate system...................................................................................................9 5.3. Colors...........................................................................................................................9 5.4. Type fonts...................................................................................................................10 5.5. Anchors......................................................................................................................11 5.6. Relief styles.................................................................................................................12 5.7. Bitmaps.......................................................................................................................12 5.8. Cursors.......................................................................................................................12 5.9. Images........................................................................................................................14 5.10. Geometry strings........................................................................................................14 5.11. Window names...........................................................................................................15 5.12. Cap and join styles.....................................................................................................15 5.13. Dash patterns.............................................................................................................16 5.14. Matching stipple patterns............................................................................................16 6. The Button widget................................................................................................................17 7. The Canvas widget................................................................................................................19 7.1. Canvas coordinates......................................................................................................20 7.2. The Canvas display list................................................................................................20 7.3. Canvas object IDs........................................................................................................21 7.4. Canvas tags................................................................................................................21 1http://www.nmt.edu/tcc/help/pubs/tkinter/ 2http://www.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf 1 Tkinter reference New Mexico Tech Computer Center 7.5. CanvastagOrId arguments......................................................................................21 7.6. Methods on Canvas widgets........................................................................................21 7.7. Canvas arc objects.......................................................................................................26 7.8. Canvas bitmap objects.................................................................................................28 7.9. Canvas image objects..................................................................................................29 7.10. Canvas line objects.....................................................................................................29 7.11. Canvas oval objects....................................................................................................31 7.12. Canvas polygon objects..............................................................................................32 7.13. Canvas rectangle objects.............................................................................................34 7.14. Canvas text objects.....................................................................................................35 7.15. Canvas window objects..............................................................................................36 8. The Checkbutton widget......................................................................................................37 9. The Entry widget..................................................................................................................40 9.1. Scrolling an Entry widget............................................................................................43 10. The Frame widget................................................................................................................43 11. The Label widget................................................................................................................44 12. The LabelFrame widget......................................................................................................46 13. The Listbox widget............................................................................................................48 13.1. Scrolling a Listbox widget........................................................................................52 14. The Menu widget..................................................................................................................52 14.1. Menu item creation (coption) options.........................................................................55 14.2. Top-level menus.........................................................................................................56 15. The Menubutton widget......................................................................................................57 16. The Message widget............................................................................................................59 17. The OptionMenu widget.......................................................................................................60 18. The PanedWindow widget....................................................................................................61 18.1. PanedWindow child configuration options...................................................................63 19. The Radiobutton widget....................................................................................................64 20. The Scale widget................................................................................................................67 21. The Scrollbar widget........................................................................................................70 21.1. The Scrollbarcommand callback............................................................................72 21.2. Connecting a Scrollbar to another widget................................................................73 22. The Spinbox widget............................................................................................................73 23. The Text widget..................................................................................................................78 23.1. Text widget indices...................................................................................................80 23.2. Text widget marks....................................................................................................81 23.3. Text widget images...................................................................................................82 23.4. Text widget windows...............................................................................................82 23.5. Text widget tags.......................................................................................................82 23.6. Setting tabs in a Text widget......................................................................................83 23.7. The Text widget undo/redo stack..............................................................................83 23.8. Methods on Text widgets..........................................................................................84 24. Toplevel: Top-level window methods..................................................................................91 25. Universal widget methods.....................................................................................................93 26. Standardizing appearance...................................................................................................101 26.1. How to name a widget class......................................................................................102 26.2. How to name a widget instance.................................................................................102 26.3. Resource specification lines.......................................................................................102 26.4. Rules for resource matching......................................................................................103 27. Connecting your application logic to the widgets...................................................................104 28. Control variables: the values behind the widgets...................................................................104 29. Focus: routing keyboard input.............................................................................................106 New Mexico Tech Computer Center Tkinter reference 2 30. Events................................................................................................................................107 30.1. Levels of binding......................................................................................................108 30.2. Event sequences.......................................................................................................109 30.3. Event types..............................................................................................................109 30.4. Event modifiers........................................................................................................110 30.5. Key names...............................................................................................................111 30.6. Writing your handler: The Event class......................................................................113 30.7. The extra arguments trick..........................................................................................115 30.8. Virtual events...........................................................................................................116 31. Pop-up dialogs....................................................................................................................116 31.1. The tkMessageBox dialogs module..........................................................................116 31.2. The tkFileDialog module.....................................................................................118 31.3. The tkColorChooser module.................................................................................119
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值