Python Tkinter 类

本文详细介绍了Tkinter中的15个核心组件及其用途,包括按钮、画布、检查按钮等,并阐述了如何通过MVC和几何管理方法进行组件配置。此外,还讨论了Python2.3中新增的组件,如标签框、面板和旋转框,以及组件配置管理的实现方式。

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

Tkinter Classes

Widget classes

Tkinter supports 15 core widgets:

Tkinter 支持15个核心组件:

Button
A simple button, used to execute a command or other operation.
按钮:一个简单的按钮,用于执行一个命令行或者其他操作。
Canvas
Structured graphics. This widget can be used to draw graphs and plots, create graphics editors, and to implement custom widgets.
画布:结构化图像。这个组件可以被用户画图像和图表,创建图像编辑器和部署定制化组件。

Checkbutton
Represents a variable that can have two distinct values. Clicking the button toggles between the values.
选择按钮:代表一个变量可以有两个相反的值。通过点击按钮可以在两个值之间切换。
Entry
A text entry field.
文本区:文本输入区域
Frame
A container widget. The frame can have a border and a background, and is used to group other widgets when creating an application or dialog layout.
框架:容器组件。容器可以有一个边框和背景。在创建一个应用或者会话布局的时候用户群分其他组件。
Label
Displays a text or an image.
标签:显示一个文本或者图像。
Listbox
Displays a list of alternatives. The listbox can be configured to get radiobutton or checklist behavior.
列表框:显示一列候选值,可以被配置成单选或者复选。
Menu
A menu pane. Used to implement pulldown and popup menus.
菜单:一个菜单方框。用户部署下拉或者弹出菜单。
Menubutton
A menubutton. Used to implement pulldown menus.
菜单按钮:用户部署下拉菜单。
Message
Display a text. Similar to the label widget, but can automatically wrap text to a given width or aspect ratio.
消息:显示一个文本。跟标签组件类似,但可以自动的把文本包装成给定的宽度或比例。
Radiobutton
Represents one value of a variable that can have one of many values. Clicking the button sets the variable to that value, and clears all other radiobuttons associated with the same variable.
单选按钮:表示一个拥有多个值的变量的一个值。点击这个按钮可以把在这个变量设为相应的值,同时会清除这个变量关联的其他单选按钮。
Scale
Allows you to set a numerical value by dragging a “slider”.
滑动条:允许通过拖到滑块去设置一个数字化的数值。
Scrollbar
Standard scrollbars for use with canvas, entry, listbox, and text widgets.
滚动条:标准的滚动条使用在画布,文本输入区,列表框和文本组件。
Text
Formatted text display. Allows you to display and edit text with various styles and attributes. Also supports embedded images and windows.
文本:格式化文本显示。允许以各式各样的格式和属性去显示和编辑文本。支持嵌入的图像和窗口。
Toplevel
A container widget displayed as a separate, top-level window.
顶层窗口,一个显示为独立的顶层窗口的容器组件。

In Python 2.3 (Tk 8.4), the following widgets were added:

Python2.3(Tk8.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.

可以注意到在Tkinter中没有组件类等级,在继承树里面所有的组件类都是兄弟姐妹。

All these widgets provide the Misc and geometry management methods, the configuration management methods, and additional methods defined by the widget itself. In addition, the Toplevel class also provides the window manager interface. This means that a typical widget class provides some 150 methods.

所有的组件提供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.

Tkinter模块提供对应Tk中各种组件类型的类和一系列mixin和其他帮助类(mixin类用户通过多重继承整个其他类)。

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.

Misc类被根窗口和组件类用作一个mixin。它提供了大量的Tk和窗口的服务,这些服务队Tkinter核心组件非常有帮助。这些通过授权来实现:组件只需要向合适的内部对象传递需求即可。

The Wm class is used as a mixin by the root window and Toplevel widget classes. It provides window manager services, also by delegation.

Wm类被根窗口和toplevel组件用作一个mixin,它通过授权提供窗口管理服务。

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.

想这样的授权简化了应用代码:一旦拥有一个组件,就可以使用组件实例的方法访问Tkinter的所有部分。

Geometry mixins

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

Grid、pack、Place类被组件用作mixin。它们通过授权提供了访问各种几何管理器的权限。

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.
网格:网格几何管理器可以通过把组件组织成一个二维网格的方式创建图表样的布局。使用grid方法可以使用这种几何管理。
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.
place几个管理器可以准确的把组件定位到一个给定的位置。可以使用place方法去使用这个几个管理器。

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.

Widget类通过几个minixs混合Misc类,通过cget和configure方法添加了配置管理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值