组件类
Tkinter支持15种核心组件:
表 4-1. Tkinter 组件类
组件 | 描述 |
---|---|
Button | 一个简单的按钮, 被用来执行一个命令或其它操作. |
Canvas | 组织图形,. 这个组件可以被用来绘制图标和图形, 创建图形编辑器, 和实现常用组件. |
Checkbutton | 表示一个具有两个不同(或相反)值的变量,点击按钮在这两个值间切换. |
Entry | 文本输入条. |
Frame | 一个组件容器. frame可以有边框和背景色, 它北用来在应用程序或对话框中将组件分组管理. |
Label | 用来显示文字和图片. |
Listbox | 用来显示一个二选一列表. Displays a list of alternatives. The listbox can be configured to get radiobutton or checklist behavior. |
Menu | 一个菜单条. 被用来实现下拉式或层叠式菜单. |
Menubutton | 一个菜单按钮. 用来实现下拉菜单. |
Message | 显示一串文字. 和label 组件相似,但是可以自动根据宽度和宽高比进行文本换行. |
Radiobutton | 单选框.具有多个选项,但是在一个时刻只能有一个值被选中.点击它的按钮设定它所代表变量的值. |
Scale | 允许你创建一个可以通过托拽滑动条来改变一个数值的组件. |
Scrollbar | 标准滚动条,它与canvas,entry,listbox和text组件结合使用. |
Text | 格式化文本显示. 允许你使用多种分隔和属性显示和编辑文字.也支持内嵌图片和窗口. |
Toplevel | 一个容器组件,用来创建子窗口. |
注意,组件之间没有层次之分;所有组件类都同属一个继承树..
所有这些组件都提供了 Misc 和 几何管理方法, 配置管理方法和附件方法被组件自身定义.另外 Toplevel 类也提供了窗口管理接口. 它意味着一个典型组件类提供提供了150种方法.
混入
几个混入类型和其它辅助类(一个混入(Mixin)类型是一个被设计为与其它使用多继承类相结合的类).当你使用Tkinter时,你应该不去直接访问混入类.
执行混入
Misc 类class is used as a mixin by the root window and widget classes. It provides a large number of Tk and window related services, which are thus available for all Tkinter core widgets. This is done bydelegation ; the widget simply forwards the request to the appropriate internal object.
The Wm class is used as a mixin by the root window and Toplevel widget classes. It provides window manager services, also by delegation.
Using delegation like this simplifies your application code: once you have a widget, you can access all parts of Tkinter using methods on the widget instance.
几何混入
Grid , Pack , 和 Place 类 are used as mixins by the widget classes. They provide access to the various geometry managers, also via delegation.
表 4-2. 几何混入
Manager | Description |
---|---|
Grid | 栅格几何管理器允许你创建一个类似表的布局形式, 组件都被组织在一个2维栅格里.通过调用 grid 方法使用栅格管理器. |
Pack | pack 几何管理器让你创建一个packgeometry manager lets you create a layout by "packing" the widgets into a parent widget, by treating them as rectangular blocks placed in a frame. To use this geometry manager for a widget, use the pack method on that widget to set things up. |
Place | The place geometry manager lets you explicitly place a widget in a given position. To use this geometry manager, use the placemethod. |
组件配置管理
The Widget class mixes the Misc class with the geometry mixins, and adds configuration management through the cget and configure methods, as well as through a partial dictionary interface. The latter can be used to set and query individual options, and is explained in further detail in the next chapter.