Both Tk and tkinter are available on most Unix platforms, as well as on Windows systems. Tk itself is not part of Python; it is maintained at ActiveState. The Python Tkinter Topic Guide provides a great deal of information on using Tk from Python and links to other sources of information on Tk.
Book by Mark Roseman about building attractive and modern graphical user interfaces with Python and Tkinter. Most commands are available as tkinter or tkinter. Most of the time, tkinter is all you really need, but a number of additional modules are available as well.
This module contains the low-level interface to Tk, and should never be used directly by application programmers. It is usually a shared library or DLLbut might in some cases be statically linked with the Python interpreter. In addition to the Tk interface module, tkinter includes a number of Python tkinter download windows free modules, tkinter.
Importing tkinter will automatically import tkinter. The Tk class is instantiated without arguments. This creates a toplevel widget of Tk which usually is the main window of an application. Each instance has its own associated Tcl interpreter. The Tcl function is a factory function which creates an object much like that created by the Tk class, except that it does not initialize the Tk subsystem. An object created by the Tcl object can have a Toplevel window created and the Tk subsystem initialized by calling its loadtk method.
Drag-and-drop support for tkinter. This is experimental and python tkinter download windows free become deprecated when it is replaced with the Tk DND. This section is not designed to be an exhaustive tutorial on either Tk or Tkinter. Rather, it is intended as a stop gap, providing some introductory orientation on the system. Fredrik Lundh elaborated and revised the class interface descriptions, to get them current with Tk 4. This section is designed in two parts: the first half roughly covers background material, while the second half can be taken to the keyboard as a handy reference.
Python programmers can often guess at the correct Python command by looking at the Tk documentation. This means that in order to use Tkinter, you will have to know a little bit about Tk. Here are some hints:. The authors strongly suggest getting a copy of the Tk man pages. Specifically, the man pages in the manN directory are most useful.
The man3 man pages describe the C interface to the Tk library and thus are not especially helpful for script writers. The book is not exhaustive, and for many details it defers to the man pages. The class hierarchy looks complicated, but in actual practice, application programmers almost always refer to the classes at the very bottom of the hierarchy.
These classes are provided for the purposes of organizing certain functions under one namespace. The Tk class is meant to be instantiated only once in an application. Application programmers need not instantiate python tkinter download windows free explicitly, the system creates python tkinter download windows free whenever any of the other classes are instantiated. To make use of this reference material, there will be times when you will need to know how to read short passages of Tk and how to identify the various parts of a Tk command.
Tk scripts are Tcl programs. Like all Tcl programs, Tk scripts are just lists of python tkinter download windows free separated by spaces.
A Tk widget is just its classthe options that help configure it, and the actions that make it do useful things. All names in Tk must be unique. To help enforce this, widgets in Tk are named with pathnamesjust like files in a file system. The python tkinter download windows free level widget, the rootis called. For example. The options come in the form of a list of flags and values.
Once created, the pathname to the widget becomes a new command. Note that the object name. The legal values of someOptions is action dependent. The master of an object is implicit in the python tkinter download windows free name given to it at creation time.
In Tkinter, masters are specified explicitly. The configuration options in Tk are given in lists of hyphened tags followed by values. In Tkinter, options are specified as keyword-arguments in the instance constructor, and keyword-args for configure calls or as instance indices, in dictionary style, for established instances.
See section Setting Options on setting options. In Tk, to perform an action on a widget, use the widget name as a command, and follow it with an action name, possibly with arguments options. In Tkinter, you call methods on the class instance to invoke actions on the widget. To give a widget to the packer geometry manageryou call pack with optional arguments. In Tkinter, the Pack class holds all this functionality, and the various forms of the pack command are implemented as methods.
All widgets in tkinter are subclassed from the Packer, and so inherit all the packing methods. See the tkinter. A Python application makes a tkinter call. This call say, for example, creating a button widgetis implemented in the tkinter package, which is written in Python. This Python function will parse the python tkinter download windows free and the arguments and convert them into a form that makes them look as if they had come from a Tk script instead of a Python script.
This C function is able to make calls into other C modules, including the C functions that make up the Tk library. Tk is implemented in C and some Tcl. The Tcl part of the Tk widgets is used to bind certain default behaviors to widgets, and is executed once at the point python tkinter download windows free the Python tkinter package is imported. The user never sees this stage. Options control python tkinter download windows free like the color and border width of a widget.
Options can be set in three ways:. For a complete explanation of a given option and its behavior, see the Tk man pages for the widget in question. 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. 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 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.
Passing the config method the name of a shorthand option will return a 2-tuple, not 5-tuple. Of course, the dictionary printed will include all the options available and their values. This is meant only as an example. Geometry managers are used to specify the relative 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 herethe packer takes qualitative relationship specification - aboveto the left offillingetc - and works everything out to determine the exact placement coordinates for you.
The packer is used python tkinter download windows free 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. Here are some examples:. Boolean, 0 or 1. Legal values: 'x''y''both''none'. Legal values are: 'left''right''top''bottom'. The current-value setting of some widgets like text entry widgets can python tkinter download windows free connected directly to application variables by using special options.
These options are variabletextvariableonvalueoffvalueand 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 tkinter.
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.
In Tk, there python tkinter download windows free a utility command, wmfor interacting with the window manager. Options to the wm command allow you to control things like titles, placement, icon bitmaps, and the like. In tkinterthese 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. 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.
Legal values are points of the compass: "n""ne""e""se""s""sw""w""nw"and also "center". There are eight built-in, named bitmaps: 'error''gray25''gray50''hourglass''info''questhead''question''warning'. You can pass integers 0 or 1 or the strings "yes" or "no". Colors can be given as the python tkinter download windows free of X colors in the rgb. The standard X cursor names from cursorfont.