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