Tkinter 模块(也称Tk接口)是pyhton用于处理Tk GUI工具集提供的一个标准接口。大多数Unix平台以及Windows系统都可以使用Tk和Tkinter。(Tk本身不是Python的一部分;它在ActiveState中维护。)
See also
大多数情况下,Tkinter模块是您真正需要的,但还有一些其他模块可用。Tk接口位于名为_tkinter的二进制模块中。该模块包含Tk的低级接口,应该不应由应用程序员直接使用。它通常是一个共享库(或DLL),但在某些情况下可能会与Python解释器静态链接。
除了Tk接口模块,Tkinter还包含许多Python模块。两个最重要的模块是Tkinter模块本身,一个名为Tkconstants的模块。前者自动导入后者,所以要使用Tkinter,你需要做的就是导入一个模块:
import Tkinter
Or, more often:
from Tkinter import *
Tk类没有参数被实例化。这创建了一个Tk的顶级小部件,它通常是一个应用程序的主窗口。每个实例都有自己的关联的Tcl解释器。
在版本2.4中更改:添加了useTk参数。
Tcl()函数是一个工厂函数,它创建一个非常类似于Tk类创建的对象,除了它不初始化Tk子系统。在一个不想创建无关的高层窗口的环境中,或者不能使用(如没有X服务器的Unix / Linux系统)的环境中,驱动Tcl解释器时,这通常很有用。由Tcl()对象创建的对象可以通过调用其loadtk()方法创建一个Toplevel窗口(并且Tk子系统已初始化)。
New in version 2.4.
提供Tk支持的其他模块包括:
这些在Python 3中也被重命名了;它们都是新的tkinter包的子模块。
本节不是设计为Tk或Tkinter的详尽教程。相反,这是一个暂时的差距,为系统提供了一些介绍性的方向。
学分:
本节设计分为两部分:前半部分(粗略地)涵盖背景材料,而下半部分可以作为方便的参考使用到键盘。
当尝试回答“我如何做blah”的形式的问题时,通常最好在直线Tk中找出如何做“blah”,然后将其转换回相应的Tkinter调用。Python程序员通常可以通过查看Tk文档来猜测正确的Python命令。这意味着为了使用Tkinter,你必须知道一点关于Tk。本文档无法履行该角色,所以我们可以做的最好的事情是指出存在的最佳文档。这里有一些提示:
See also
from Tkinter import *
class Application(Frame):
def say_hi(self):
print "hi there, everyone!"
def createWidgets(self):
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit
self.QUIT.pack({"side": "left"})
self.hi_there = Button(self)
self.hi_there["text"] = "Hello",
self.hi_there["command"] = self.say_hi
self.hi_there.pack({"side": "left"})
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()
类层次结构看起来很复杂,但在实践中,应用程序员几乎总是引用层次结构底层的类。
Notes:
为了使用这个参考资料,你将需要知道如何阅读Tk的简短段落以及如何识别Tk命令的各个部分。(参见将基本Tk映射到Tkinter中的Tkinter等效于下面的内容。)
Tk脚本是Tcl程序。像所有Tcl程序一样,Tk脚本只是用空格分隔的令牌列表。一个Tk小部件就是它的类,有助于配置它的选项以及使它变得有用的动作。
要在Tk中创建一个小部件,该命令始终具有以下形式:
classCommand newPathname options
例如:
button .fred -fg red -text "hi there"
^ ^ \_____________________/
| | |
class new options
command widget (-opt val -opt val ...)
创建后,窗口小部件的路径名将成为新的命令。这个新的小部件命令是程序员的手柄,用于获取新的小部件来执行一些动作。在C中,你会把这个表达为someAction(fred,someOptions),在C中,你会将其表示为fred.someAction(someOptions),在Tk中你会说:
.fred someAction someOptions
请注意,对象名称.fred以点开头。
正如你所料,someAction的合法值将取决于小部件的类:.fred disable如果fred是一个按钮(fred变灰),但是如果fred是标签,则不起作用(Tk中不支持禁用标签)。
The legal values of someOptions is action dependent. 某些操作(如disable)不需要任何参数,其他操作(如文本输入框的delete命令)将需要参数来指定要删除的文本范围。
Tk中的类命令对应于Tkinter中的类构造函数。
button .fred =====> fred = Button()
对象的主体在创建时给予的新名称是隐含的。在Tkinter中,明确指定主人。
button .panel.fred =====> fred = Button(panel)
Tk中的配置选项在连字符标签后面跟着值的列表中给出。在Tkinter中,选项在实例构造函数中被指定为关键字参数,关键字args用于配置调用或实例索引(字典样式),用于已建立的实例。有关设置选项,请参见设置选项。
button .fred -fg red =====> fred = Button(panel, fg = "red")
.fred configure -fg red =====> fred["fg"] = red
OR ==> fred.config(fg = "red")
在Tk中,要对窗口小部件执行操作,请使用窗口小部件名称作为命令,并使用动作名称(可能使用参数(选项))来跟踪它。在Tkinter中,您可以调用类实例上的方法来调用窗口小部件上的操作。Tkinter.py模块中列出了一个给定的窗口小部件可以执行的动作(方法)。
.fred invoke =====> fred.invoke()
要给封装器(几何管理器)提供一个小部件,你可以使用可选参数来调用pack。在Tkinter中,Pack类保存所有这些功能,并且pack命令的各种形式被实现为方法。Tkinter中的所有小部件都从封隔器进行子类化,因此继承了所有打包方法。有关Form几何管理器的其他信息,请参阅Tix模块文档。
pack .fred -side left =====> fred.pack(side = "left")
Options control things like the color and border width of a widget. Options can be set in three ways:
fred = Button(self, fg = "red", bg = "blue")
fred["fg"] = "red"
fred["bg"] = "blue"
fred.config(fg = "red", bg = "blue")
For a complete explanation of a given option and its behavior, see the Tk man pages for the widget in question.
Note that the man pages list “STANDARD OPTIONS” and “WIDGET SPECIFIC OPTIONS” for each widget. The former is a list of options that are common to many widgets, the latter are the options that are idiosyncratic to that particular widget. The Standard Options are documented on the options(3) man page.
No distinction between standard and widget-specific options is made in this document. Some options don’t apply to some kinds of widgets. Whether a given widget responds to a particular option depends on the class of the widget; buttons have a command option, labels do not.
The options supported by a given widget are listed in that widget’s man page, or can be queried at runtime by calling the config() method without arguments, or by calling the keys() method on that widget. The return value of these calls is a dictionary whose key is the name of the option as a string (for example, 'relief') and whose values are 5-tuples.
Some options, like bg are synonyms for common options with long names (bg is shorthand for “background”). Passing the config() method the name of a shorthand option will return a 2-tuple, not 5-tuple. The 2-tuple passed back will contain the name of the synonym and the “real” option (such as ('bg', 'background')).
Index | Meaning | Example |
---|---|---|
0 | option name | 'relief' |
1 | option name for database lookup | 'relief' |
2 | option class for database lookup | 'Relief' |
3 | default value | 'raised' |
4 | current value | 'groove' |
Example:
>>> print fred.config()
{'relief': ('relief', 'relief', 'Relief', 'raised', 'groove')}
Of course, the dictionary printed will include all the options available and their values. This is meant only as an example.
The packer is one of Tk’s geometry-management mechanisms. Geometry managers are used to specify the relative positioning of the positioning of widgets within their container - their mutual master. In contrast to the more cumbersome placer (which is used less commonly, and we do not cover here), the packer takes qualitative relationship specification - above, to the left of, filling, etc - and works everything out to determine the exact placement coordinates for you.
The size of any master widget is determined by the size of the “slave widgets” inside. The packer is used to control where slave widgets appear inside the master into which they are packed. You can pack widgets into frames, and frames into other frames, in order to achieve the kind of layout you desire. Additionally, the arrangement is dynamically adjusted to accommodate incremental changes to the configuration, once it is packed.
Note that widgets do not appear until they have had their geometry specified with a geometry manager. It’s a common early mistake to leave out the geometry specification, and then be surprised when the widget is created but nothing appears. A widget will appear only after it has had, for example, the packer’s pack() method applied to it.
The pack() method can be called with keyword-option/value pairs that control where the widget is to appear within its container, and how it is to behave when the main application window is resized. Here are some examples:
fred.pack() # defaults to side = "top"
fred.pack(side = "left")
fred.pack(expand = 1)
For more extensive information on the packer and the options that it can take, see the man pages and page 183 of John Ousterhout’s book.
The current-value setting of some widgets (like text entry widgets) can be connected directly to application variables by using special options. These options are variable, textvariable, onvalue, offvalue, and value. This connection works both ways: if the variable changes for any reason, the widget it’s connected to will be updated to reflect the new value.
Unfortunately, in the current implementation of Tkinter it is not possible to hand over an arbitrary Python variable to a widget through a variable or textvariable option. The only kinds of variables for which this works are variables that are subclassed from a class called Variable, defined in the Tkinter module.
There are many useful subclasses of Variable already defined: StringVar, IntVar, DoubleVar, and BooleanVar. To read the current value of such a variable, call the get() method on it, and to change its value you call the set() method. If you follow this protocol, the widget will always track the value of the variable, with no further intervention on your part.
For example:
class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.entrythingy = Entry()
self.entrythingy.pack()
# here is the application variable
self.contents = StringVar()
# set it to some value
self.contents.set("this is a variable")
# tell the entry widget to watch this variable
self.entrythingy["textvariable"] = self.contents
# and here we get a callback when the user hits return.
# we will have the program print out the value of the
# application variable when the user hits return
self.entrythingy.bind('<Key-Return>',
self.print_contents)
def print_contents(self, event):
print "hi. contents of entry is now ---->", \
self.contents.get()
In Tk, there is a utility command, wm, for interacting with the window manager. Options to the wm command allow you to control things like titles, placement, icon bitmaps, and the like. In Tkinter, these commands have been implemented as methods on the Wm class. Toplevel widgets are subclassed from the Wm class, and so can call the Wm methods directly.
To get at the toplevel window that contains a given widget, you can often just refer to the widget’s master. Of course if the widget has been packed inside of a frame, the master won’t represent a toplevel window. To get at the toplevel window that contains an arbitrary widget, you can call the _root() method. This method begins with an underscore to denote the fact that this function is part of the implementation, and not an interface to Tk functionality.
Here are some examples of typical usage:
from Tkinter import *
class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
# create the application
myapp = App()
#
# here are method calls to the window manager class
#
myapp.master.title("My Do-Nothing Application")
myapp.master.maxsize(1000, 400)
# start the program
myapp.mainloop()
This is any Python function that takes no arguments. For example:
def print_it():
print "hi there"
fred["command"] = print_it
The bind method from the widget command allows you to watch for certain events and to have a callback function trigger when that event type occurs. The form of the bind method is:
def bind(self, sequence, func, add=''):
where:
For example:
def turnRed(self, event):
event.widget["activeforeground"] = "red"
self.button.bind("<Enter>", self.turnRed)
Notice how the widget field of the event is being accessed in the turnRed() callback. This field contains the widget that caught the X event. The following table lists the other event fields you can access, and how they are denoted in Tk, which can be useful when referring to the Tk man pages.
Tk Tkinter Event Field Tk Tkinter Event Field
-- ------------------- -- -------------------
%f focus %A char
%h height %E send_event
%k keycode %K keysym
%s state %N keysym_num
%t time %T type
%w width %W widget
%x x %X x_root
%y y %Y y_root
A number of widgets require”index” parameters to be passed. These are used to point at a specific place in a Text widget, or to particular characters in an Entry widget, or to particular menu items in a Menu widget.
Entry widgets have options that refer to character positions in the text being displayed. You can use these Tkinter functions to access these special points in text widgets:
Some options and methods for menus manipulate specific menu entries. Anytime a menu index is needed for an option or a parameter, you may pass in:
Bitmap/Pixelmap images can be created through the subclasses of Tkinter.Image:
Either type of image is created through either the file or the data option (other options are available as well).
The image object can then be used wherever an image option is supported by some widget (e.g. labels, buttons, menus). In these cases, Tk will not keep a reference to the image. When the last Python reference to the image object is deleted, the image data is deleted as well, and Tk will display an empty box wherever the image was used.