Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 1 | :mod:`tkinter` --- Python interface to Tcl/Tk |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2 | ============================================= |
| 3 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 4 | .. module:: tkinter |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 5 | :synopsis: Interface to Tcl/Tk for graphical user interfaces |
| 6 | .. moduleauthor:: Guido van Rossum <guido@Python.org> |
| 7 | |
| 8 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 9 | The :mod:`tkinter` package ("Tk interface") is the standard Python interface to |
| 10 | the Tk GUI toolkit. Both Tk and :mod:`tkinter` are available on most Unix |
Georg Brandl | c575c90 | 2008-09-13 17:46:05 +0000 | [diff] [blame] | 11 | platforms, as well as on Windows systems. (Tk itself is not part of Python; it |
Alexander Belopolsky | c02cc27 | 2010-07-27 14:16:32 +0000 | [diff] [blame] | 12 | is maintained at ActiveState.) You can check that :mod:`tkinter` is properly |
| 13 | installed on your system by running ``python -m tkinter`` from the command line; |
| 14 | this should open a window demonstrating a simple Tk interface. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | .. seealso:: |
| 17 | |
Zachary Ware | 0886399 | 2014-03-18 09:19:18 -0500 | [diff] [blame] | 18 | `Python Tkinter Resources <https://wiki.python.org/moin/TkInter>`_ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 19 | The Python Tkinter Topic Guide provides a great deal of information on using Tk |
| 20 | from Python and links to other sources of information on Tk. |
| 21 | |
Andrew Svetlov | e708a8a | 2012-07-26 17:02:57 +0300 | [diff] [blame] | 22 | `TKDocs <http://www.tkdocs.com/>`_ |
| 23 | Extensive tutorial plus friendlier widget pages for some of the widgets. |
| 24 | |
Georg Brandl | b7354a6 | 2014-10-29 10:57:37 +0100 | [diff] [blame] | 25 | `Tkinter reference: a GUI for Python <http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html>`_ |
Andrew Svetlov | e708a8a | 2012-07-26 17:02:57 +0300 | [diff] [blame] | 26 | On-line reference material. |
| 27 | |
| 28 | `Tkinter docs from effbot <http://effbot.org/tkinterbook/>`_ |
| 29 | Online reference for tkinter supported by effbot.org. |
| 30 | |
| 31 | `Tcl/Tk manual <http://www.tcl.tk/man/tcl8.5/>`_ |
| 32 | Official manual for the latest tcl/tk version. |
| 33 | |
| 34 | `Programming Python <http://www.amazon.com/Programming-Python-Mark-Lutz/dp/0596158106/>`_ |
| 35 | Book by Mark Lutz, has excellent coverage of Tkinter. |
| 36 | |
| 37 | `Modern Tkinter for Busy Python Developers <http://www.amazon.com/Modern-Tkinter-Python-Developers-ebook/dp/B0071QDNLO/>`_ |
| 38 | Book by Mark Rozerman about building attractive and modern graphical user interfaces with Python and Tkinter. |
| 39 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 | `Python and Tkinter Programming <http://www.amazon.com/exec/obidos/ASIN/1884777813>`_ |
| 41 | The book by John Grayson (ISBN 1-884777-81-3). |
| 42 | |
| 43 | |
| 44 | Tkinter Modules |
| 45 | --------------- |
| 46 | |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 47 | Most of the time, :mod:`tkinter` is all you really need, but a number of |
| 48 | additional modules are available as well. The Tk interface is located in a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 | binary module named :mod:`_tkinter`. This module contains the low-level |
| 50 | interface to Tk, and should never be used directly by application programmers. |
| 51 | It is usually a shared library (or DLL), but might in some cases be statically |
| 52 | linked with the Python interpreter. |
| 53 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 54 | In addition to the Tk interface module, :mod:`tkinter` includes a number of |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 55 | Python modules, :mod:`tkinter.constants` being one of the most important. |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 56 | Importing :mod:`tkinter` will automatically import :mod:`tkinter.constants`, |
| 57 | so, usually, to use Tkinter all you need is a simple import statement:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 59 | import tkinter |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | |
| 61 | Or, more often:: |
| 62 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 63 | from tkinter import * |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | .. class:: Tk(screenName=None, baseName=None, className='Tk', useTk=1) |
| 67 | |
| 68 | The :class:`Tk` class is instantiated without arguments. This creates a toplevel |
| 69 | widget of Tk which usually is the main window of an application. Each instance |
| 70 | has its own associated Tcl interpreter. |
| 71 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 72 | .. FIXME: The following keyword arguments are currently recognized: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
| 75 | .. function:: Tcl(screenName=None, baseName=None, className='Tk', useTk=0) |
| 76 | |
| 77 | The :func:`Tcl` function is a factory function which creates an object much like |
| 78 | that created by the :class:`Tk` class, except that it does not initialize the Tk |
| 79 | subsystem. This is most often useful when driving the Tcl interpreter in an |
| 80 | environment where one doesn't want to create extraneous toplevel windows, or |
| 81 | where one cannot (such as Unix/Linux systems without an X server). An object |
| 82 | created by the :func:`Tcl` object can have a Toplevel window created (and the Tk |
| 83 | subsystem initialized) by calling its :meth:`loadtk` method. |
| 84 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 | |
| 86 | Other modules that provide Tk support include: |
| 87 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 88 | :mod:`tkinter.scrolledtext` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 89 | Text widget with a vertical scroll bar built in. |
| 90 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 91 | :mod:`tkinter.colorchooser` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 92 | Dialog to let the user choose a color. |
| 93 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 94 | :mod:`tkinter.commondialog` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 95 | Base class for the dialogs defined in the other modules listed here. |
| 96 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 97 | :mod:`tkinter.filedialog` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 98 | Common dialogs to allow the user to specify a file to open or save. |
| 99 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 100 | :mod:`tkinter.font` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 101 | Utilities to help work with fonts. |
| 102 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 103 | :mod:`tkinter.messagebox` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 | Access to standard Tk dialog boxes. |
| 105 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 106 | :mod:`tkinter.simpledialog` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | Basic dialogs and convenience functions. |
| 108 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 109 | :mod:`tkinter.dnd` |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 110 | Drag-and-drop support for :mod:`tkinter`. This is experimental and should |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 111 | become deprecated when it is replaced with the Tk DND. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | |
Georg Brandl | 23d11d3 | 2008-09-21 07:50:52 +0000 | [diff] [blame] | 113 | :mod:`turtle` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 114 | Turtle graphics in a Tk window. |
| 115 | |
| 116 | |
| 117 | Tkinter Life Preserver |
| 118 | ---------------------- |
| 119 | |
| 120 | .. sectionauthor:: Matt Conway |
| 121 | |
| 122 | |
| 123 | This section is not designed to be an exhaustive tutorial on either Tk or |
| 124 | Tkinter. Rather, it is intended as a stop gap, providing some introductory |
| 125 | orientation on the system. |
| 126 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 127 | Credits: |
| 128 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 129 | * Tk was written by John Ousterhout while at Berkeley. |
| 130 | |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 131 | * Tkinter was written by Steen Lumholt and Guido van Rossum. |
| 132 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 133 | * This Life Preserver was written by Matt Conway at the University of Virginia. |
| 134 | |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 135 | * The HTML rendering, and some liberal editing, was produced from a FrameMaker |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 136 | version by Ken Manheimer. |
| 137 | |
| 138 | * Fredrik Lundh elaborated and revised the class interface descriptions, to get |
| 139 | them current with Tk 4.2. |
| 140 | |
| 141 | * Mike Clarkson converted the documentation to LaTeX, and compiled the User |
| 142 | Interface chapter of the reference manual. |
| 143 | |
| 144 | |
| 145 | How To Use This Section |
| 146 | ^^^^^^^^^^^^^^^^^^^^^^^ |
| 147 | |
| 148 | This section is designed in two parts: the first half (roughly) covers |
| 149 | background material, while the second half can be taken to the keyboard as a |
| 150 | handy reference. |
| 151 | |
| 152 | When trying to answer questions of the form "how do I do blah", it is often best |
| 153 | to find out how to do"blah" in straight Tk, and then convert this back into the |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 154 | corresponding :mod:`tkinter` call. Python programmers can often guess at the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 | correct Python command by looking at the Tk documentation. This means that in |
| 156 | order to use Tkinter, you will have to know a little bit about Tk. This document |
| 157 | can't fulfill that role, so the best we can do is point you to the best |
| 158 | documentation that exists. Here are some hints: |
| 159 | |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 160 | * The authors strongly suggest getting a copy of the Tk man pages. |
| 161 | Specifically, the man pages in the ``manN`` directory are most useful. |
| 162 | The ``man3`` man pages describe the C interface to the Tk library and thus |
| 163 | are not especially helpful for script writers. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 164 | |
| 165 | * Addison-Wesley publishes a book called Tcl and the Tk Toolkit by John |
| 166 | Ousterhout (ISBN 0-201-63337-X) which is a good introduction to Tcl and Tk for |
| 167 | the novice. The book is not exhaustive, and for many details it defers to the |
| 168 | man pages. |
| 169 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 170 | * :file:`tkinter/__init__.py` is a last resort for most, but can be a good |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 171 | place to go when nothing else makes sense. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 172 | |
| 173 | |
| 174 | .. seealso:: |
| 175 | |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 176 | `Tcl/Tk 8.6 man pages <http://www.tcl.tk/man/tcl8.6/>`_ |
| 177 | The Tcl/Tk manual on www.tcl.tk. |
| 178 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 179 | `ActiveState Tcl Home Page <http://tcl.activestate.com/>`_ |
| 180 | The Tk/Tcl development is largely taking place at ActiveState. |
| 181 | |
| 182 | `Tcl and the Tk Toolkit <http://www.amazon.com/exec/obidos/ASIN/020163337X>`_ |
Serhiy Storchaka | a4d170d | 2013-12-23 18:20:51 +0200 | [diff] [blame] | 183 | The book by John Ousterhout, the inventor of Tcl. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 184 | |
| 185 | `Practical Programming in Tcl and Tk <http://www.amazon.com/exec/obidos/ASIN/0130220280>`_ |
| 186 | Brent Welch's encyclopedic book. |
| 187 | |
| 188 | |
| 189 | A Simple Hello World Program |
| 190 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 191 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 192 | :: |
| 193 | |
Andrew Svetlov | d3d7c90 | 2012-03-14 21:41:23 -0700 | [diff] [blame] | 194 | import tkinter as tk |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 195 | |
Andrew Svetlov | d3d7c90 | 2012-03-14 21:41:23 -0700 | [diff] [blame] | 196 | class Application(tk.Frame): |
| 197 | def __init__(self, master=None): |
| 198 | tk.Frame.__init__(self, master) |
| 199 | self.pack() |
| 200 | self.createWidgets() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 201 | |
Andrew Svetlov | d3d7c90 | 2012-03-14 21:41:23 -0700 | [diff] [blame] | 202 | def createWidgets(self): |
| 203 | self.hi_there = tk.Button(self) |
| 204 | self.hi_there["text"] = "Hello World\n(click me)" |
| 205 | self.hi_there["command"] = self.say_hi |
| 206 | self.hi_there.pack(side="top") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 207 | |
Andrew Svetlov | 1d56179 | 2012-03-25 11:44:59 +0300 | [diff] [blame] | 208 | self.QUIT = tk.Button(self, text="QUIT", fg="red", |
| 209 | command=root.destroy) |
| 210 | self.QUIT.pack(side="bottom") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 211 | |
Andrew Svetlov | d3d7c90 | 2012-03-14 21:41:23 -0700 | [diff] [blame] | 212 | def say_hi(self): |
| 213 | print("hi there, everyone!") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 214 | |
Andrew Svetlov | d3d7c90 | 2012-03-14 21:41:23 -0700 | [diff] [blame] | 215 | root = tk.Tk() |
| 216 | app = Application(master=root) |
| 217 | app.mainloop() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 218 | |
| 219 | |
| 220 | A (Very) Quick Look at Tcl/Tk |
| 221 | ----------------------------- |
| 222 | |
| 223 | The class hierarchy looks complicated, but in actual practice, application |
| 224 | programmers almost always refer to the classes at the very bottom of the |
| 225 | hierarchy. |
| 226 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 227 | Notes: |
| 228 | |
| 229 | * These classes are provided for the purposes of organizing certain functions |
| 230 | under one namespace. They aren't meant to be instantiated independently. |
| 231 | |
| 232 | * The :class:`Tk` class is meant to be instantiated only once in an application. |
| 233 | Application programmers need not instantiate one explicitly, the system creates |
| 234 | one whenever any of the other classes are instantiated. |
| 235 | |
| 236 | * The :class:`Widget` class is not meant to be instantiated, it is meant only |
| 237 | for subclassing to make "real" widgets (in C++, this is called an 'abstract |
| 238 | class'). |
| 239 | |
| 240 | To make use of this reference material, there will be times when you will need |
| 241 | to know how to read short passages of Tk and how to identify the various parts |
| 242 | of a Tk command. (See section :ref:`tkinter-basic-mapping` for the |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 243 | :mod:`tkinter` equivalents of what's below.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 244 | |
| 245 | Tk scripts are Tcl programs. Like all Tcl programs, Tk scripts are just lists |
| 246 | of tokens separated by spaces. A Tk widget is just its *class*, the *options* |
| 247 | that help configure it, and the *actions* that make it do useful things. |
| 248 | |
| 249 | To make a widget in Tk, the command is always of the form:: |
| 250 | |
| 251 | classCommand newPathname options |
| 252 | |
| 253 | *classCommand* |
| 254 | denotes which kind of widget to make (a button, a label, a menu...) |
| 255 | |
| 256 | *newPathname* |
| 257 | is the new name for this widget. All names in Tk must be unique. To help |
| 258 | enforce this, widgets in Tk are named with *pathnames*, just like files in a |
| 259 | file system. The top level widget, the *root*, is called ``.`` (period) and |
| 260 | children are delimited by more periods. For example, |
| 261 | ``.myApp.controlPanel.okButton`` might be the name of a widget. |
| 262 | |
| 263 | *options* |
| 264 | configure the widget's appearance and in some cases, its behavior. The options |
| 265 | come in the form of a list of flags and values. Flags are preceded by a '-', |
| 266 | like Unix shell command flags, and values are put in quotes if they are more |
| 267 | than one word. |
| 268 | |
| 269 | For example:: |
| 270 | |
| 271 | button .fred -fg red -text "hi there" |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 272 | ^ ^ \______________________/ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 273 | | | | |
| 274 | class new options |
| 275 | command widget (-opt val -opt val ...) |
| 276 | |
| 277 | Once created, the pathname to the widget becomes a new command. This new |
| 278 | *widget command* is the programmer's handle for getting the new widget to |
| 279 | perform some *action*. In C, you'd express this as someAction(fred, |
| 280 | someOptions), in C++, you would express this as fred.someAction(someOptions), |
| 281 | and in Tk, you say:: |
| 282 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 283 | .fred someAction someOptions |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 284 | |
| 285 | Note that the object name, ``.fred``, starts with a dot. |
| 286 | |
| 287 | As you'd expect, the legal values for *someAction* will depend on the widget's |
| 288 | class: ``.fred disable`` works if fred is a button (fred gets greyed out), but |
| 289 | does not work if fred is a label (disabling of labels is not supported in Tk). |
| 290 | |
| 291 | The legal values of *someOptions* is action dependent. Some actions, like |
| 292 | ``disable``, require no arguments, others, like a text-entry box's ``delete`` |
| 293 | command, would need arguments to specify what range of text to delete. |
| 294 | |
| 295 | |
| 296 | .. _tkinter-basic-mapping: |
| 297 | |
| 298 | Mapping Basic Tk into Tkinter |
| 299 | ----------------------------- |
| 300 | |
| 301 | Class commands in Tk correspond to class constructors in Tkinter. :: |
| 302 | |
| 303 | button .fred =====> fred = Button() |
| 304 | |
| 305 | The master of an object is implicit in the new name given to it at creation |
| 306 | time. In Tkinter, masters are specified explicitly. :: |
| 307 | |
| 308 | button .panel.fred =====> fred = Button(panel) |
| 309 | |
| 310 | The configuration options in Tk are given in lists of hyphened tags followed by |
| 311 | values. In Tkinter, options are specified as keyword-arguments in the instance |
| 312 | constructor, and keyword-args for configure calls or as instance indices, in |
| 313 | dictionary style, for established instances. See section |
| 314 | :ref:`tkinter-setting-options` on setting options. :: |
| 315 | |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 316 | button .fred -fg red =====> fred = Button(panel, fg="red") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 317 | .fred configure -fg red =====> fred["fg"] = red |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 318 | OR ==> fred.config(fg="red") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 319 | |
| 320 | In Tk, to perform an action on a widget, use the widget name as a command, and |
| 321 | follow it with an action name, possibly with arguments (options). In Tkinter, |
| 322 | you call methods on the class instance to invoke actions on the widget. The |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 323 | actions (methods) that a given widget can perform are listed in |
| 324 | :file:`tkinter/__init__.py`. :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 325 | |
| 326 | .fred invoke =====> fred.invoke() |
| 327 | |
| 328 | To give a widget to the packer (geometry manager), you call pack with optional |
| 329 | arguments. In Tkinter, the Pack class holds all this functionality, and the |
| 330 | various forms of the pack command are implemented as methods. All widgets in |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 331 | :mod:`tkinter` are subclassed from the Packer, and so inherit all the packing |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 332 | methods. See the :mod:`tkinter.tix` module documentation for additional |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 333 | information on the Form geometry manager. :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 334 | |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 335 | pack .fred -side left =====> fred.pack(side="left") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 336 | |
| 337 | |
| 338 | How Tk and Tkinter are Related |
| 339 | ------------------------------ |
| 340 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 341 | From the top down: |
| 342 | |
| 343 | Your App Here (Python) |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 344 | A Python application makes a :mod:`tkinter` call. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 345 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 346 | tkinter (Python Package) |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 347 | This call (say, for example, creating a button widget), is implemented in |
| 348 | the :mod:`tkinter` package, which is written in Python. This Python |
| 349 | function will parse the commands and the arguments and convert them into a |
| 350 | form that makes them look as if they had come from a Tk script instead of |
| 351 | a Python script. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 352 | |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 353 | _tkinter (C) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 354 | These commands and their arguments will be passed to a C function in the |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 355 | :mod:`_tkinter` - note the underscore - extension module. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 356 | |
| 357 | Tk Widgets (C and Tcl) |
| 358 | This C function is able to make calls into other C modules, including the C |
| 359 | functions that make up the Tk library. Tk is implemented in C and some Tcl. |
| 360 | The Tcl part of the Tk widgets is used to bind certain default behaviors to |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 361 | widgets, and is executed once at the point where the Python :mod:`tkinter` |
| 362 | package is imported. (The user never sees this stage). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 363 | |
| 364 | Tk (C) |
| 365 | The Tk part of the Tk Widgets implement the final mapping to ... |
| 366 | |
| 367 | Xlib (C) |
| 368 | the Xlib library to draw graphics on the screen. |
| 369 | |
| 370 | |
| 371 | Handy Reference |
| 372 | --------------- |
| 373 | |
| 374 | |
| 375 | .. _tkinter-setting-options: |
| 376 | |
| 377 | Setting Options |
| 378 | ^^^^^^^^^^^^^^^ |
| 379 | |
| 380 | Options control things like the color and border width of a widget. Options can |
| 381 | be set in three ways: |
| 382 | |
| 383 | At object creation time, using keyword arguments |
| 384 | :: |
| 385 | |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 386 | fred = Button(self, fg="red", bg="blue") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 387 | |
| 388 | After object creation, treating the option name like a dictionary index |
| 389 | :: |
| 390 | |
| 391 | fred["fg"] = "red" |
| 392 | fred["bg"] = "blue" |
| 393 | |
| 394 | Use the config() method to update multiple attrs subsequent to object creation |
| 395 | :: |
| 396 | |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 397 | fred.config(fg="red", bg="blue") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 398 | |
| 399 | For a complete explanation of a given option and its behavior, see the Tk man |
| 400 | pages for the widget in question. |
| 401 | |
| 402 | Note that the man pages list "STANDARD OPTIONS" and "WIDGET SPECIFIC OPTIONS" |
| 403 | for each widget. The former is a list of options that are common to many |
| 404 | widgets, the latter are the options that are idiosyncratic to that particular |
| 405 | widget. The Standard Options are documented on the :manpage:`options(3)` man |
| 406 | page. |
| 407 | |
| 408 | No distinction between standard and widget-specific options is made in this |
| 409 | document. Some options don't apply to some kinds of widgets. Whether a given |
| 410 | widget responds to a particular option depends on the class of the widget; |
| 411 | buttons have a ``command`` option, labels do not. |
| 412 | |
| 413 | The options supported by a given widget are listed in that widget's man page, or |
| 414 | can be queried at runtime by calling the :meth:`config` method without |
| 415 | arguments, or by calling the :meth:`keys` method on that widget. The return |
| 416 | value of these calls is a dictionary whose key is the name of the option as a |
| 417 | string (for example, ``'relief'``) and whose values are 5-tuples. |
| 418 | |
| 419 | Some options, like ``bg`` are synonyms for common options with long names |
| 420 | (``bg`` is shorthand for "background"). Passing the ``config()`` method the name |
| 421 | of a shorthand option will return a 2-tuple, not 5-tuple. The 2-tuple passed |
| 422 | back will contain the name of the synonym and the "real" option (such as |
| 423 | ``('bg', 'background')``). |
| 424 | |
| 425 | +-------+---------------------------------+--------------+ |
| 426 | | Index | Meaning | Example | |
| 427 | +=======+=================================+==============+ |
| 428 | | 0 | option name | ``'relief'`` | |
| 429 | +-------+---------------------------------+--------------+ |
| 430 | | 1 | option name for database lookup | ``'relief'`` | |
| 431 | +-------+---------------------------------+--------------+ |
| 432 | | 2 | option class for database | ``'Relief'`` | |
| 433 | | | lookup | | |
| 434 | +-------+---------------------------------+--------------+ |
| 435 | | 3 | default value | ``'raised'`` | |
| 436 | +-------+---------------------------------+--------------+ |
| 437 | | 4 | current value | ``'groove'`` | |
| 438 | +-------+---------------------------------+--------------+ |
| 439 | |
| 440 | Example:: |
| 441 | |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 442 | >>> print(fred.config()) |
Serhiy Storchaka | f47036c | 2013-12-24 11:04:36 +0200 | [diff] [blame] | 443 | {'relief': ('relief', 'relief', 'Relief', 'raised', 'groove')} |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 444 | |
| 445 | Of course, the dictionary printed will include all the options available and |
| 446 | their values. This is meant only as an example. |
| 447 | |
| 448 | |
| 449 | The Packer |
| 450 | ^^^^^^^^^^ |
| 451 | |
| 452 | .. index:: single: packing (widgets) |
| 453 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 454 | The packer is one of Tk's geometry-management mechanisms. Geometry managers |
| 455 | are used to specify the relative positioning of the positioning of widgets |
| 456 | within their container - their mutual *master*. In contrast to the more |
| 457 | cumbersome *placer* (which is used less commonly, and we do not cover here), the |
| 458 | packer takes qualitative relationship specification - *above*, *to the left of*, |
| 459 | *filling*, etc - and works everything out to determine the exact placement |
| 460 | coordinates for you. |
| 461 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 462 | The size of any *master* widget is determined by the size of the "slave widgets" |
| 463 | inside. The packer is used to control where slave widgets appear inside the |
| 464 | master into which they are packed. You can pack widgets into frames, and frames |
| 465 | into other frames, in order to achieve the kind of layout you desire. |
| 466 | Additionally, the arrangement is dynamically adjusted to accommodate incremental |
| 467 | changes to the configuration, once it is packed. |
| 468 | |
| 469 | Note that widgets do not appear until they have had their geometry specified |
| 470 | with a geometry manager. It's a common early mistake to leave out the geometry |
| 471 | specification, and then be surprised when the widget is created but nothing |
| 472 | appears. A widget will appear only after it has had, for example, the packer's |
| 473 | :meth:`pack` method applied to it. |
| 474 | |
| 475 | The pack() method can be called with keyword-option/value pairs that control |
| 476 | where the widget is to appear within its container, and how it is to behave when |
| 477 | the main application window is resized. Here are some examples:: |
| 478 | |
| 479 | fred.pack() # defaults to side = "top" |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 480 | fred.pack(side="left") |
| 481 | fred.pack(expand=1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 482 | |
| 483 | |
| 484 | Packer Options |
| 485 | ^^^^^^^^^^^^^^ |
| 486 | |
| 487 | For more extensive information on the packer and the options that it can take, |
| 488 | see the man pages and page 183 of John Ousterhout's book. |
| 489 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 490 | anchor |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 491 | Anchor type. Denotes where the packer is to place each slave in its parcel. |
| 492 | |
| 493 | expand |
| 494 | Boolean, ``0`` or ``1``. |
| 495 | |
| 496 | fill |
| 497 | Legal values: ``'x'``, ``'y'``, ``'both'``, ``'none'``. |
| 498 | |
| 499 | ipadx and ipady |
| 500 | A distance - designating internal padding on each side of the slave widget. |
| 501 | |
| 502 | padx and pady |
| 503 | A distance - designating external padding on each side of the slave widget. |
| 504 | |
| 505 | side |
| 506 | Legal values are: ``'left'``, ``'right'``, ``'top'``, ``'bottom'``. |
| 507 | |
| 508 | |
| 509 | Coupling Widget Variables |
| 510 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 511 | |
| 512 | The current-value setting of some widgets (like text entry widgets) can be |
| 513 | connected directly to application variables by using special options. These |
| 514 | options are ``variable``, ``textvariable``, ``onvalue``, ``offvalue``, and |
| 515 | ``value``. This connection works both ways: if the variable changes for any |
| 516 | reason, the widget it's connected to will be updated to reflect the new value. |
| 517 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 518 | Unfortunately, in the current implementation of :mod:`tkinter` it is not |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 519 | possible to hand over an arbitrary Python variable to a widget through a |
| 520 | ``variable`` or ``textvariable`` option. The only kinds of variables for which |
| 521 | this works are variables that are subclassed from a class called Variable, |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 522 | defined in :mod:`tkinter`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 523 | |
| 524 | There are many useful subclasses of Variable already defined: |
| 525 | :class:`StringVar`, :class:`IntVar`, :class:`DoubleVar`, and |
| 526 | :class:`BooleanVar`. To read the current value of such a variable, call the |
Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 527 | :meth:`get` method on it, and to change its value you call the :meth:`!set` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 528 | method. If you follow this protocol, the widget will always track the value of |
| 529 | the variable, with no further intervention on your part. |
| 530 | |
| 531 | For example:: |
| 532 | |
| 533 | class App(Frame): |
| 534 | def __init__(self, master=None): |
| 535 | Frame.__init__(self, master) |
| 536 | self.pack() |
| 537 | |
| 538 | self.entrythingy = Entry() |
| 539 | self.entrythingy.pack() |
| 540 | |
| 541 | # here is the application variable |
| 542 | self.contents = StringVar() |
| 543 | # set it to some value |
| 544 | self.contents.set("this is a variable") |
| 545 | # tell the entry widget to watch this variable |
| 546 | self.entrythingy["textvariable"] = self.contents |
| 547 | |
| 548 | # and here we get a callback when the user hits return. |
| 549 | # we will have the program print out the value of the |
| 550 | # application variable when the user hits return |
| 551 | self.entrythingy.bind('<Key-Return>', |
| 552 | self.print_contents) |
| 553 | |
| 554 | def print_contents(self, event): |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 555 | print("hi. contents of entry is now ---->", |
| 556 | self.contents.get()) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 557 | |
| 558 | |
| 559 | The Window Manager |
| 560 | ^^^^^^^^^^^^^^^^^^ |
| 561 | |
| 562 | .. index:: single: window manager (widgets) |
| 563 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 564 | In Tk, there is a utility command, ``wm``, for interacting with the window |
| 565 | manager. Options to the ``wm`` command allow you to control things like titles, |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 566 | placement, icon bitmaps, and the like. In :mod:`tkinter`, these commands have |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 567 | been implemented as methods on the :class:`Wm` class. Toplevel widgets are |
| 568 | subclassed from the :class:`Wm` class, and so can call the :class:`Wm` methods |
| 569 | directly. |
| 570 | |
| 571 | To get at the toplevel window that contains a given widget, you can often just |
| 572 | refer to the widget's master. Of course if the widget has been packed inside of |
| 573 | a frame, the master won't represent a toplevel window. To get at the toplevel |
| 574 | window that contains an arbitrary widget, you can call the :meth:`_root` method. |
| 575 | This method begins with an underscore to denote the fact that this function is |
| 576 | part of the implementation, and not an interface to Tk functionality. |
| 577 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 578 | Here are some examples of typical usage:: |
| 579 | |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 580 | from tkinter import * |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 581 | class App(Frame): |
| 582 | def __init__(self, master=None): |
| 583 | Frame.__init__(self, master) |
| 584 | self.pack() |
| 585 | |
| 586 | |
| 587 | # create the application |
| 588 | myapp = App() |
| 589 | |
| 590 | # |
| 591 | # here are method calls to the window manager class |
| 592 | # |
| 593 | myapp.master.title("My Do-Nothing Application") |
| 594 | myapp.master.maxsize(1000, 400) |
| 595 | |
| 596 | # start the program |
| 597 | myapp.mainloop() |
| 598 | |
| 599 | |
| 600 | Tk Option Data Types |
| 601 | ^^^^^^^^^^^^^^^^^^^^ |
| 602 | |
| 603 | .. index:: single: Tk Option Data Types |
| 604 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 605 | anchor |
| 606 | Legal values are points of the compass: ``"n"``, ``"ne"``, ``"e"``, ``"se"``, |
| 607 | ``"s"``, ``"sw"``, ``"w"``, ``"nw"``, and also ``"center"``. |
| 608 | |
| 609 | bitmap |
| 610 | There are eight built-in, named bitmaps: ``'error'``, ``'gray25'``, |
| 611 | ``'gray50'``, ``'hourglass'``, ``'info'``, ``'questhead'``, ``'question'``, |
| 612 | ``'warning'``. To specify an X bitmap filename, give the full path to the file, |
| 613 | preceded with an ``@``, as in ``"@/usr/contrib/bitmap/gumby.bit"``. |
| 614 | |
| 615 | boolean |
Serhiy Storchaka | a4d170d | 2013-12-23 18:20:51 +0200 | [diff] [blame] | 616 | You can pass integers 0 or 1 or the strings ``"yes"`` or ``"no"``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 617 | |
| 618 | callback |
| 619 | This is any Python function that takes no arguments. For example:: |
| 620 | |
| 621 | def print_it(): |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 622 | print("hi there") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 623 | fred["command"] = print_it |
| 624 | |
| 625 | color |
| 626 | Colors can be given as the names of X colors in the rgb.txt file, or as strings |
| 627 | representing RGB values in 4 bit: ``"#RGB"``, 8 bit: ``"#RRGGBB"``, 12 bit" |
| 628 | ``"#RRRGGGBBB"``, or 16 bit ``"#RRRRGGGGBBBB"`` ranges, where R,G,B here |
| 629 | represent any legal hex digit. See page 160 of Ousterhout's book for details. |
| 630 | |
| 631 | cursor |
| 632 | The standard X cursor names from :file:`cursorfont.h` can be used, without the |
| 633 | ``XC_`` prefix. For example to get a hand cursor (:const:`XC_hand2`), use the |
| 634 | string ``"hand2"``. You can also specify a bitmap and mask file of your own. |
| 635 | See page 179 of Ousterhout's book. |
| 636 | |
| 637 | distance |
| 638 | Screen distances can be specified in either pixels or absolute distances. |
| 639 | Pixels are given as numbers and absolute distances as strings, with the trailing |
| 640 | character denoting units: ``c`` for centimetres, ``i`` for inches, ``m`` for |
| 641 | millimetres, ``p`` for printer's points. For example, 3.5 inches is expressed |
| 642 | as ``"3.5i"``. |
| 643 | |
| 644 | font |
| 645 | Tk uses a list font name format, such as ``{courier 10 bold}``. Font sizes with |
| 646 | positive numbers are measured in points; sizes with negative numbers are |
| 647 | measured in pixels. |
| 648 | |
| 649 | geometry |
| 650 | This is a string of the form ``widthxheight``, where width and height are |
| 651 | measured in pixels for most widgets (in characters for widgets displaying text). |
| 652 | For example: ``fred["geometry"] = "200x100"``. |
| 653 | |
| 654 | justify |
| 655 | Legal values are the strings: ``"left"``, ``"center"``, ``"right"``, and |
| 656 | ``"fill"``. |
| 657 | |
| 658 | region |
| 659 | This is a string with four space-delimited elements, each of which is a legal |
| 660 | distance (see above). For example: ``"2 3 4 5"`` and ``"3i 2i 4.5i 2i"`` and |
| 661 | ``"3c 2c 4c 10.43c"`` are all legal regions. |
| 662 | |
| 663 | relief |
| 664 | Determines what the border style of a widget will be. Legal values are: |
| 665 | ``"raised"``, ``"sunken"``, ``"flat"``, ``"groove"``, and ``"ridge"``. |
| 666 | |
| 667 | scrollcommand |
Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 668 | This is almost always the :meth:`!set` method of some scrollbar widget, but can |
Georg Brandl | 59b4472 | 2010-12-30 22:12:40 +0000 | [diff] [blame] | 669 | be any widget method that takes a single argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 670 | |
| 671 | wrap: |
| 672 | Must be one of: ``"none"``, ``"char"``, or ``"word"``. |
| 673 | |
| 674 | |
| 675 | Bindings and Events |
| 676 | ^^^^^^^^^^^^^^^^^^^ |
| 677 | |
| 678 | .. index:: |
| 679 | single: bind (widgets) |
| 680 | single: events (widgets) |
| 681 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 682 | The bind method from the widget command allows you to watch for certain events |
| 683 | and to have a callback function trigger when that event type occurs. The form |
| 684 | of the bind method is:: |
| 685 | |
| 686 | def bind(self, sequence, func, add=''): |
| 687 | |
| 688 | where: |
| 689 | |
| 690 | sequence |
| 691 | is a string that denotes the target kind of event. (See the bind man page and |
| 692 | page 201 of John Ousterhout's book for details). |
| 693 | |
| 694 | func |
| 695 | is a Python function, taking one argument, to be invoked when the event occurs. |
| 696 | An Event instance will be passed as the argument. (Functions deployed this way |
| 697 | are commonly known as *callbacks*.) |
| 698 | |
| 699 | add |
| 700 | is optional, either ``''`` or ``'+'``. Passing an empty string denotes that |
| 701 | this binding is to replace any other bindings that this event is associated |
| 702 | with. Passing a ``'+'`` means that this function is to be added to the list |
| 703 | of functions bound to this event type. |
| 704 | |
| 705 | For example:: |
| 706 | |
| 707 | def turnRed(self, event): |
| 708 | event.widget["activeforeground"] = "red" |
| 709 | |
| 710 | self.button.bind("<Enter>", self.turnRed) |
| 711 | |
| 712 | Notice how the widget field of the event is being accessed in the |
| 713 | :meth:`turnRed` callback. This field contains the widget that caught the X |
| 714 | event. The following table lists the other event fields you can access, and how |
| 715 | they are denoted in Tk, which can be useful when referring to the Tk man pages. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 716 | |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 717 | +----+---------------------+----+---------------------+ |
| 718 | | Tk | Tkinter Event Field | Tk | Tkinter Event Field | |
| 719 | +====+=====================+====+=====================+ |
| 720 | | %f | focus | %A | char | |
| 721 | +----+---------------------+----+---------------------+ |
| 722 | | %h | height | %E | send_event | |
| 723 | +----+---------------------+----+---------------------+ |
| 724 | | %k | keycode | %K | keysym | |
| 725 | +----+---------------------+----+---------------------+ |
| 726 | | %s | state | %N | keysym_num | |
| 727 | +----+---------------------+----+---------------------+ |
| 728 | | %t | time | %T | type | |
| 729 | +----+---------------------+----+---------------------+ |
| 730 | | %w | width | %W | widget | |
| 731 | +----+---------------------+----+---------------------+ |
| 732 | | %x | x | %X | x_root | |
| 733 | +----+---------------------+----+---------------------+ |
| 734 | | %y | y | %Y | y_root | |
| 735 | +----+---------------------+----+---------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 736 | |
| 737 | |
| 738 | The index Parameter |
| 739 | ^^^^^^^^^^^^^^^^^^^ |
| 740 | |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 741 | A number of widgets require "index" parameters to be passed. These are used to |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 742 | point at a specific place in a Text widget, or to particular characters in an |
| 743 | Entry widget, or to particular menu items in a Menu widget. |
| 744 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 745 | Entry widget indexes (index, view index, etc.) |
| 746 | Entry widgets have options that refer to character positions in the text being |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 747 | displayed. You can use these :mod:`tkinter` functions to access these special |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 748 | points in text widgets: |
| 749 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 750 | Text widget indexes |
| 751 | The index notation for Text widgets is very rich and is best described in the Tk |
| 752 | man pages. |
| 753 | |
| 754 | Menu indexes (menu.invoke(), menu.entryconfig(), etc.) |
| 755 | Some options and methods for menus manipulate specific menu entries. Anytime a |
| 756 | menu index is needed for an option or a parameter, you may pass in: |
| 757 | |
| 758 | * an integer which refers to the numeric position of the entry in the widget, |
| 759 | counted from the top, starting with 0; |
| 760 | |
Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 761 | * the string ``"active"``, which refers to the menu position that is currently |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 762 | under the cursor; |
| 763 | |
| 764 | * the string ``"last"`` which refers to the last menu item; |
| 765 | |
| 766 | * An integer preceded by ``@``, as in ``@6``, where the integer is interpreted |
| 767 | as a y pixel coordinate in the menu's coordinate system; |
| 768 | |
| 769 | * the string ``"none"``, which indicates no menu entry at all, most often used |
| 770 | with menu.activate() to deactivate all entries, and finally, |
| 771 | |
| 772 | * a text string that is pattern matched against the label of the menu entry, as |
| 773 | scanned from the top of the menu to the bottom. Note that this index type is |
| 774 | considered after all the others, which means that matches for menu items |
| 775 | labelled ``last``, ``active``, or ``none`` may be interpreted as the above |
| 776 | literals, instead. |
| 777 | |
| 778 | |
| 779 | Images |
| 780 | ^^^^^^ |
| 781 | |
| 782 | Bitmap/Pixelmap images can be created through the subclasses of |
Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 783 | :class:`tkinter.Image`: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 784 | |
| 785 | * :class:`BitmapImage` can be used for X11 bitmap data. |
| 786 | |
| 787 | * :class:`PhotoImage` can be used for GIF and PPM/PGM color bitmaps. |
| 788 | |
| 789 | Either type of image is created through either the ``file`` or the ``data`` |
| 790 | option (other options are available as well). |
| 791 | |
| 792 | The image object can then be used wherever an ``image`` option is supported by |
| 793 | some widget (e.g. labels, buttons, menus). In these cases, Tk will not keep a |
| 794 | reference to the image. When the last Python reference to the image object is |
| 795 | deleted, the image data is deleted as well, and Tk will display an empty box |
| 796 | wherever the image was used. |