0%

Python Tkinter 的窗口组件类

Tkinter 窗口组件类

Tkinter支持下列15种核心组件:

Button按钮

按钮,用于执行命令或其他操作。

Canvas画布

结构化的graphics。这个组件用于绘制graphs 和 plots,创建图像编辑器,部署其他组件。

Checkbutton复选按钮

有两个值,通过激活按钮来切换。

Entry 输入框

用于输入文本。

Frame

一个容器窗口部件。帧可以有边框和背景,当创建一个应用程序或 dialog(对话)版面时,帧被用来组织其它 的窗口部件。

Label 标签

用于显示文本或者图像。

Listbox

显示供选方案的一个列表。listbox 能够被配置来得到 radiobutton 或 checklist 的状态

菜单条,用来实现下拉或者弹出式菜单

菜单按钮,用来实现下拉式菜单,目前基本可以使用Menu来实现

Message

显示文本。与 label 窗口部件类似,但是能够自动地调整文本到给定的宽度或比率。

Radiobutton

代表一个变量,为多个值中的一个。点击它将为这个变量设置值,并且清除与这同一变量相关的其它
radiobutton。

Scale

允许你通过滑块来设置一数字值

Scrollbar

配合canvas, entry, listbox, and text窗口部件使用的标准滚动条。

Text

格式化文本显示。允许你用不同的样式和属性来显示和编辑文本。同时支持内嵌图象和窗口。

Toplevel

一个容器窗口部件,作为一个单独的、最上面的窗口显示。

在Python 2.3 (Tk 8.4),增加了下述组件:

LabelFrame

A variant of the Frame widget that can draw both a border and a title.

PanedWindow

A container widget that organizes child widgets in resizable panes.

Spinbox

A variant of the Entry widget for selecting values from a range or an ordered set.

Also note that there’s no widget class hierarchy in Tkinter; all widget classes are siblings in the inheritance tree.

所有这些窗口部件提供了 Misc 和几何管理方法、配置管理方法和部件自己定义的另外的方法。此外,Toplevel
类也提供窗口管理接口。这意味一个典型的窗口部件类提供了大约 150 种方法。

Mixins

The Tkinter module provides classes corresponding to the various widget types in Tk, and a number of mixin and other helper classes (a mixin is a class designed to be combined with other classes using multiple inheritance). When you use Tkinter, you should never access the mixin classes directly.

Implementation mixins

The 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 by delegation; 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.

Geometry mixins

The Grid, Pack, and Place classes are used as mixins by the widget classes. They provide access to the various geometry managers, also via delegation.

Grid
The grid geometry manager allows you to create table-like layouts, by organizing the widgets in a 2-dimensional grid. To use this geometry manager, use the grid method.
Pack
The pack geometry 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 place method.

Widget configuration management

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.

处无为之事,行不言之教;作而弗始,生而弗有,为而弗恃,功成不居!

欢迎关注我的其它发布渠道