| 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 |
| Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | .. moduleauthor:: Guido van Rossum <guido@Python.org> |
| 8 | |
| Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 9 | **Source code:** :source:`Lib/tkinter/__init__.py` |
| 10 | |
| 11 | -------------- |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | |
| Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 13 | The :mod:`tkinter` package ("Tk interface") is the standard Python interface to |
| Miss Islington (bot) | 61ecd3e | 2021-08-22 11:54:46 -0700 | [diff] [blame] | 14 | the Tcl/Tk GUI toolkit. Both Tk and :mod:`tkinter` are available on most Unix |
| 15 | platforms, including macOS, as well as on Windows systems. |
| Andrés Delfino | 67a8f4f | 2018-04-25 14:53:58 -0300 | [diff] [blame] | 16 | |
| 17 | Running ``python -m tkinter`` from the command line should open a window |
| 18 | demonstrating a simple Tk interface, letting you know that :mod:`tkinter` is |
| 19 | properly installed on your system, and also showing what version of Tcl/Tk is |
| 20 | installed, so you can read the Tcl/Tk documentation specific to that version. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | |
| Miss Islington (bot) | 2666d70 | 2021-08-11 10:16:10 -0700 | [diff] [blame] | 22 | Tkinter supports a range of Tcl/Tk versions, built either with or |
| 23 | without thread support. The official Python binary release bundles Tcl/Tk 8.6 |
| 24 | threaded. See the source code for the :mod:`_tkinter` module |
| 25 | for more information about supported versions. |
| 26 | |
| 27 | Tkinter is not a thin wrapper, but adds a fair amount of its own logic to |
| 28 | make the experience more pythonic. This documentation will concentrate on these |
| 29 | additions and changes, and refer to the official Tcl/Tk documentation for |
| 30 | details that are unchanged. |
| 31 | |
| Miss Islington (bot) | 8351df6 | 2021-08-23 12:22:29 -0700 | [diff] [blame] | 32 | .. note:: |
| 33 | |
| 34 | Tcl/Tk 8.5 (2007) introduced a modern set of themed user interface components |
| 35 | along with a new API to use them. Both old and new APIs are still available. |
| 36 | Most documentation you will find online still uses the old API and |
| 37 | can be woefully outdated. |
| 38 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | .. seealso:: |
| 40 | |
| Łukasz Langa | 8e90f15 | 2021-08-22 21:04:57 +0200 | [diff] [blame] | 41 | * `TkDocs <http://tkdocs.com/>`_ |
| 42 | Extensive tutorial on creating user interfaces with Tkinter. Explains key concepts, |
| 43 | and illustrates recommended approaches using the modern API. |
| Andrés Delfino | 67a8f4f | 2018-04-25 14:53:58 -0300 | [diff] [blame] | 44 | |
| Łukasz Langa | 8e90f15 | 2021-08-22 21:04:57 +0200 | [diff] [blame] | 45 | * `Tkinter 8.5 reference: a GUI for Python <https://www.tkdocs.com/shipman/>`_ |
| 46 | Reference documentation for Tkinter 8.5 detailing available classes, methods, and options. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 | |
| Łukasz Langa | 8e90f15 | 2021-08-22 21:04:57 +0200 | [diff] [blame] | 48 | Tcl/Tk Resources: |
| Andrew Svetlov | e708a8a | 2012-07-26 17:02:57 +0300 | [diff] [blame] | 49 | |
| Łukasz Langa | 8e90f15 | 2021-08-22 21:04:57 +0200 | [diff] [blame] | 50 | * `Tk commands <https://www.tcl.tk/man/tcl8.6/TkCmd/contents.htm>`_ |
| 51 | Comprehensive reference to each of the underlying Tcl/Tk commands used by Tkinter. |
| Andrew Svetlov | e708a8a | 2012-07-26 17:02:57 +0300 | [diff] [blame] | 52 | |
| Łukasz Langa | 8e90f15 | 2021-08-22 21:04:57 +0200 | [diff] [blame] | 53 | * `Tcl/Tk Home Page <https://www.tcl.tk>`_ |
| 54 | Additional documentation, and links to Tcl/Tk core development. |
| Andrew Svetlov | e708a8a | 2012-07-26 17:02:57 +0300 | [diff] [blame] | 55 | |
| Łukasz Langa | 8e90f15 | 2021-08-22 21:04:57 +0200 | [diff] [blame] | 56 | Books: |
| Andrew Svetlov | e708a8a | 2012-07-26 17:02:57 +0300 | [diff] [blame] | 57 | |
| Łukasz Langa | 8e90f15 | 2021-08-22 21:04:57 +0200 | [diff] [blame] | 58 | * `Modern Tkinter for Busy Python Developers <https://tkdocs.com/book.html>`_ |
| 59 | By Mark Roseman. (ISBN 978-1999149567) |
| Andrew Svetlov | e708a8a | 2012-07-26 17:02:57 +0300 | [diff] [blame] | 60 | |
| Łukasz Langa | 8e90f15 | 2021-08-22 21:04:57 +0200 | [diff] [blame] | 61 | * `Python and Tkinter Programming <https://www.packtpub.com/product/python-gui-programming-with-tkinter/9781788835886>`_ |
| 62 | By Alan Moore. (ISBN 978-1788835886) |
| Andrés Delfino | 67a8f4f | 2018-04-25 14:53:58 -0300 | [diff] [blame] | 63 | |
| Łukasz Langa | 8e90f15 | 2021-08-22 21:04:57 +0200 | [diff] [blame] | 64 | * `Programming Python <http://learning-python.com/about-pp4e.html>`_ |
| 65 | By Mark Lutz; has excellent coverage of Tkinter. (ISBN 978-0596158101) |
| Andrés Delfino | 67a8f4f | 2018-04-25 14:53:58 -0300 | [diff] [blame] | 66 | |
| Łukasz Langa | 8e90f15 | 2021-08-22 21:04:57 +0200 | [diff] [blame] | 67 | * `Tcl and the Tk Toolkit (2nd edition) <https://www.amazon.com/exec/obidos/ASIN/032133633X>`_ |
| 68 | By John Ousterhout, inventor of Tcl/Tk, and Ken Jones; does not cover Tkinter. (ISBN 978-0321336330) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | |
| 70 | |
| Miss Islington (bot) | 2666d70 | 2021-08-11 10:16:10 -0700 | [diff] [blame] | 71 | Architecture |
| 72 | ------------ |
| 73 | |
| 74 | Tcl/Tk is not a single library but rather consists of a few distinct |
| 75 | modules, each with a separate functionality and its own official |
| 76 | documentation. Python's binary releases also ship an add-on module |
| 77 | together with it. |
| 78 | |
| 79 | Tcl |
| 80 | Tcl is a dynamic interpreted programming language, just like Python. Though |
| 81 | it can be used on its own as a general-purpose programming language, it is |
| 82 | most commonly embedded into C applications as a scripting engine or an |
| 83 | interface to the Tk toolkit. The Tcl library has a C interface to |
| 84 | create and manage one or more instances of a Tcl interpreter, run Tcl |
| 85 | commands and scripts in those instances, and add custom commands |
| 86 | implemented in either Tcl or C. Each interpreter has an event queue, |
| 87 | and there are facilities to send events to it and process them. |
| 88 | Unlike Python, Tcl's execution model is designed around cooperative |
| 89 | multitasking, and Tkinter bridges this difference |
| 90 | (see `Threading model`_ for details). |
| 91 | |
| 92 | Tk |
| 93 | Tk is a `Tcl package <http://wiki.tcl.tk/37432>`_ implemented in C |
| 94 | that adds custom commands to create and manipulate GUI widgets. Each |
| 95 | :class:`Tk` object embeds its own Tcl interpreter instance with Tk loaded into |
| 96 | it. Tk's widgets are very customizable, though at the cost of a dated appearance. |
| 97 | Tk uses Tcl's event queue to generate and process GUI events. |
| 98 | |
| 99 | Ttk |
| 100 | Themed Tk (Ttk) is a newer family of Tk widgets that provide a much better |
| 101 | appearance on different platforms than many of the classic Tk widgets. |
| 102 | Ttk is distributed as part of Tk, starting with Tk version 8.5. Python |
| 103 | bindings are provided in a separate module, :mod:`tkinter.ttk`. |
| 104 | |
| 105 | Tix |
| 106 | `Tix <https://core.tcl.tk/jenglish/gutter/packages/tix.html>`_ is an older |
| 107 | third-party Tcl package, an add-on for Tk that adds several new widgets. |
| 108 | Python bindings are found in the :mod:`tkinter.tix` module. |
| 109 | It's deprecated in favor of Ttk. |
| 110 | |
| 111 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | Tkinter Modules |
| 113 | --------------- |
| 114 | |
| Miss Islington (bot) | 4d1e74f | 2021-08-22 12:04:24 -0700 | [diff] [blame] | 115 | Support for Tkinter is spread across several modules. Most applications will need the |
| 116 | main :mod:`tkinter` module, as well as the :mod:`tkinter.ttk` module, which provides |
| 117 | the modern themed widget set and API:: |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 118 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 | |
| Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 120 | from tkinter import * |
| Miss Islington (bot) | 4d1e74f | 2021-08-22 12:04:24 -0700 | [diff] [blame] | 121 | from tkinter import ttk |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 122 | |
| 123 | |
| 124 | .. class:: Tk(screenName=None, baseName=None, className='Tk', useTk=1) |
| 125 | |
| 126 | The :class:`Tk` class is instantiated without arguments. This creates a toplevel |
| 127 | widget of Tk which usually is the main window of an application. Each instance |
| 128 | has its own associated Tcl interpreter. |
| 129 | |
| Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 130 | .. FIXME: The following keyword arguments are currently recognized: |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 131 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 132 | |
| 133 | .. function:: Tcl(screenName=None, baseName=None, className='Tk', useTk=0) |
| 134 | |
| 135 | The :func:`Tcl` function is a factory function which creates an object much like |
| 136 | that created by the :class:`Tk` class, except that it does not initialize the Tk |
| 137 | subsystem. This is most often useful when driving the Tcl interpreter in an |
| 138 | environment where one doesn't want to create extraneous toplevel windows, or |
| 139 | where one cannot (such as Unix/Linux systems without an X server). An object |
| 140 | created by the :func:`Tcl` object can have a Toplevel window created (and the Tk |
| 141 | subsystem initialized) by calling its :meth:`loadtk` method. |
| 142 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 | |
| Miss Islington (bot) | 4d1e74f | 2021-08-22 12:04:24 -0700 | [diff] [blame] | 144 | The modules that provide Tk support include: |
| 145 | |
| 146 | :mod:`tkinter` |
| 147 | Main Tkinter module. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 148 | |
| Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 149 | :mod:`tkinter.colorchooser` |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 150 | Dialog to let the user choose a color. |
| 151 | |
| Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 152 | :mod:`tkinter.commondialog` |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 153 | Base class for the dialogs defined in the other modules listed here. |
| 154 | |
| Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 155 | :mod:`tkinter.filedialog` |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 156 | Common dialogs to allow the user to specify a file to open or save. |
| 157 | |
| Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 158 | :mod:`tkinter.font` |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 159 | Utilities to help work with fonts. |
| 160 | |
| Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 161 | :mod:`tkinter.messagebox` |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | Access to standard Tk dialog boxes. |
| 163 | |
| Nikhil | 80428ed | 2019-09-10 01:55:34 -0700 | [diff] [blame] | 164 | :mod:`tkinter.scrolledtext` |
| 165 | Text widget with a vertical scroll bar built in. |
| 166 | |
| Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 167 | :mod:`tkinter.simpledialog` |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 168 | Basic dialogs and convenience functions. |
| 169 | |
| Miss Islington (bot) | 4d1e74f | 2021-08-22 12:04:24 -0700 | [diff] [blame] | 170 | :mod:`tkinter.ttk` |
| 171 | Themed widget set introduced in Tk 8.5, providing modern alternatives |
| 172 | for many of the classic widgets in the main :mod:`tkinter` module. |
| 173 | |
| 174 | Additional modules: |
| 175 | |
| 176 | :mod:`_tkinter` |
| 177 | A binary module that contains the low-level interface to Tcl/Tk. |
| 178 | It is automatically imported by the main :mod:`tkinter` module, |
| 179 | and should never be used directly by application programmers. |
| 180 | It is usually a shared library (or DLL), but might in some cases be |
| 181 | statically linked with the Python interpreter. |
| 182 | |
| 183 | :mod:`idlelib` |
| 184 | Python's Integrated Development and Learning Environment (IDLE). Based |
| 185 | on :mod:`tkinter`. |
| 186 | |
| 187 | :mod:`tkinter.constants` |
| 188 | Symbolic constants that can be used in place of strings when passing |
| 189 | various parameters to Tkinter calls. Automatically imported by the |
| 190 | main :mod:`tkinter` module. |
| 191 | |
| Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 192 | :mod:`tkinter.dnd` |
| Miss Islington (bot) | 4d1e74f | 2021-08-22 12:04:24 -0700 | [diff] [blame] | 193 | (experimental) Drag-and-drop support for :mod:`tkinter`. This will |
| 194 | become deprecated when it is replaced with the Tk DND. |
| 195 | |
| 196 | :mod:`tkinter.tix` |
| 197 | (deprecated) An older third-party Tcl/Tk package that adds several new |
| 198 | widgets. Better alternatives for most can be found in :mod:`tkinter.ttk`. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 199 | |
| Georg Brandl | 23d11d3 | 2008-09-21 07:50:52 +0000 | [diff] [blame] | 200 | :mod:`turtle` |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 201 | Turtle graphics in a Tk window. |
| 202 | |
| 203 | |
| 204 | Tkinter Life Preserver |
| 205 | ---------------------- |
| 206 | |
| 207 | .. sectionauthor:: Matt Conway |
| 208 | |
| 209 | |
| 210 | This section is not designed to be an exhaustive tutorial on either Tk or |
| 211 | Tkinter. Rather, it is intended as a stop gap, providing some introductory |
| 212 | orientation on the system. |
| 213 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 214 | Credits: |
| 215 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 216 | * Tk was written by John Ousterhout while at Berkeley. |
| 217 | |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 218 | * Tkinter was written by Steen Lumholt and Guido van Rossum. |
| 219 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 220 | * This Life Preserver was written by Matt Conway at the University of Virginia. |
| 221 | |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 222 | * The HTML rendering, and some liberal editing, was produced from a FrameMaker |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | version by Ken Manheimer. |
| 224 | |
| 225 | * Fredrik Lundh elaborated and revised the class interface descriptions, to get |
| 226 | them current with Tk 4.2. |
| 227 | |
| 228 | * Mike Clarkson converted the documentation to LaTeX, and compiled the User |
| 229 | Interface chapter of the reference manual. |
| 230 | |
| 231 | |
| 232 | How To Use This Section |
| 233 | ^^^^^^^^^^^^^^^^^^^^^^^ |
| 234 | |
| 235 | This section is designed in two parts: the first half (roughly) covers |
| 236 | background material, while the second half can be taken to the keyboard as a |
| 237 | handy reference. |
| 238 | |
| 239 | When trying to answer questions of the form "how do I do blah", it is often best |
| Julien Palard | ae342cf | 2017-12-05 06:05:33 +0100 | [diff] [blame] | 240 | 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] | 241 | corresponding :mod:`tkinter` call. Python programmers can often guess at the |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 242 | correct Python command by looking at the Tk documentation. This means that in |
| 243 | order to use Tkinter, you will have to know a little bit about Tk. This document |
| 244 | can't fulfill that role, so the best we can do is point you to the best |
| 245 | documentation that exists. Here are some hints: |
| 246 | |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 247 | * The authors strongly suggest getting a copy of the Tk man pages. |
| 248 | Specifically, the man pages in the ``manN`` directory are most useful. |
| 249 | The ``man3`` man pages describe the C interface to the Tk library and thus |
| 250 | are not especially helpful for script writers. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 251 | |
| 252 | * Addison-Wesley publishes a book called Tcl and the Tk Toolkit by John |
| 253 | Ousterhout (ISBN 0-201-63337-X) which is a good introduction to Tcl and Tk for |
| 254 | the novice. The book is not exhaustive, and for many details it defers to the |
| 255 | man pages. |
| 256 | |
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 257 | * :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] | 258 | place to go when nothing else makes sense. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 259 | |
| 260 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 261 | A Simple Hello World Program |
| 262 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 263 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 264 | :: |
| 265 | |
| Andrew Svetlov | d3d7c90 | 2012-03-14 21:41:23 -0700 | [diff] [blame] | 266 | import tkinter as tk |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 267 | |
| Andrew Svetlov | d3d7c90 | 2012-03-14 21:41:23 -0700 | [diff] [blame] | 268 | class Application(tk.Frame): |
| 269 | def __init__(self, master=None): |
| Berker Peksag | 3093bf1 | 2016-07-14 07:32:43 +0300 | [diff] [blame] | 270 | super().__init__(master) |
| Daniel Lovell | a80af77 | 2018-10-30 07:56:07 -0700 | [diff] [blame] | 271 | self.master = master |
| Andrew Svetlov | d3d7c90 | 2012-03-14 21:41:23 -0700 | [diff] [blame] | 272 | self.pack() |
| Berker Peksag | 3093bf1 | 2016-07-14 07:32:43 +0300 | [diff] [blame] | 273 | self.create_widgets() |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 274 | |
| Berker Peksag | 3093bf1 | 2016-07-14 07:32:43 +0300 | [diff] [blame] | 275 | def create_widgets(self): |
| Andrew Svetlov | d3d7c90 | 2012-03-14 21:41:23 -0700 | [diff] [blame] | 276 | self.hi_there = tk.Button(self) |
| 277 | self.hi_there["text"] = "Hello World\n(click me)" |
| 278 | self.hi_there["command"] = self.say_hi |
| 279 | self.hi_there.pack(side="top") |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 280 | |
| Berker Peksag | 3093bf1 | 2016-07-14 07:32:43 +0300 | [diff] [blame] | 281 | self.quit = tk.Button(self, text="QUIT", fg="red", |
| Daniel Lovell | a80af77 | 2018-10-30 07:56:07 -0700 | [diff] [blame] | 282 | command=self.master.destroy) |
| Berker Peksag | 3093bf1 | 2016-07-14 07:32:43 +0300 | [diff] [blame] | 283 | self.quit.pack(side="bottom") |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 284 | |
| Andrew Svetlov | d3d7c90 | 2012-03-14 21:41:23 -0700 | [diff] [blame] | 285 | def say_hi(self): |
| 286 | print("hi there, everyone!") |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 287 | |
| Andrew Svetlov | d3d7c90 | 2012-03-14 21:41:23 -0700 | [diff] [blame] | 288 | root = tk.Tk() |
| 289 | app = Application(master=root) |
| 290 | app.mainloop() |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 291 | |
| 292 | |
| 293 | A (Very) Quick Look at Tcl/Tk |
| 294 | ----------------------------- |
| 295 | |
| 296 | The class hierarchy looks complicated, but in actual practice, application |
| 297 | programmers almost always refer to the classes at the very bottom of the |
| 298 | hierarchy. |
| 299 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 300 | Notes: |
| 301 | |
| 302 | * These classes are provided for the purposes of organizing certain functions |
| 303 | under one namespace. They aren't meant to be instantiated independently. |
| 304 | |
| 305 | * The :class:`Tk` class is meant to be instantiated only once in an application. |
| 306 | Application programmers need not instantiate one explicitly, the system creates |
| 307 | one whenever any of the other classes are instantiated. |
| 308 | |
| 309 | * The :class:`Widget` class is not meant to be instantiated, it is meant only |
| 310 | for subclassing to make "real" widgets (in C++, this is called an 'abstract |
| 311 | class'). |
| 312 | |
| 313 | To make use of this reference material, there will be times when you will need |
| 314 | to know how to read short passages of Tk and how to identify the various parts |
| 315 | of a Tk command. (See section :ref:`tkinter-basic-mapping` for the |
| Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 316 | :mod:`tkinter` equivalents of what's below.) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 317 | |
| 318 | Tk scripts are Tcl programs. Like all Tcl programs, Tk scripts are just lists |
| 319 | of tokens separated by spaces. A Tk widget is just its *class*, the *options* |
| 320 | that help configure it, and the *actions* that make it do useful things. |
| 321 | |
| 322 | To make a widget in Tk, the command is always of the form:: |
| 323 | |
| 324 | classCommand newPathname options |
| 325 | |
| 326 | *classCommand* |
| 327 | denotes which kind of widget to make (a button, a label, a menu...) |
| 328 | |
| Serhiy Storchaka | 913876d | 2018-10-28 13:41:26 +0200 | [diff] [blame] | 329 | .. index:: single: . (dot); in Tkinter |
| 330 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 331 | *newPathname* |
| 332 | is the new name for this widget. All names in Tk must be unique. To help |
| 333 | enforce this, widgets in Tk are named with *pathnames*, just like files in a |
| 334 | file system. The top level widget, the *root*, is called ``.`` (period) and |
| 335 | children are delimited by more periods. For example, |
| 336 | ``.myApp.controlPanel.okButton`` might be the name of a widget. |
| 337 | |
| 338 | *options* |
| 339 | configure the widget's appearance and in some cases, its behavior. The options |
| 340 | come in the form of a list of flags and values. Flags are preceded by a '-', |
| 341 | like Unix shell command flags, and values are put in quotes if they are more |
| 342 | than one word. |
| 343 | |
| 344 | For example:: |
| 345 | |
| 346 | button .fred -fg red -text "hi there" |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 347 | ^ ^ \______________________/ |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 348 | | | | |
| 349 | class new options |
| 350 | command widget (-opt val -opt val ...) |
| 351 | |
| 352 | Once created, the pathname to the widget becomes a new command. This new |
| 353 | *widget command* is the programmer's handle for getting the new widget to |
| 354 | perform some *action*. In C, you'd express this as someAction(fred, |
| 355 | someOptions), in C++, you would express this as fred.someAction(someOptions), |
| 356 | and in Tk, you say:: |
| 357 | |
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 358 | .fred someAction someOptions |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 359 | |
| 360 | Note that the object name, ``.fred``, starts with a dot. |
| 361 | |
| 362 | As you'd expect, the legal values for *someAction* will depend on the widget's |
| 363 | class: ``.fred disable`` works if fred is a button (fred gets greyed out), but |
| 364 | does not work if fred is a label (disabling of labels is not supported in Tk). |
| 365 | |
| 366 | The legal values of *someOptions* is action dependent. Some actions, like |
| 367 | ``disable``, require no arguments, others, like a text-entry box's ``delete`` |
| 368 | command, would need arguments to specify what range of text to delete. |
| 369 | |
| 370 | |
| 371 | .. _tkinter-basic-mapping: |
| 372 | |
| 373 | Mapping Basic Tk into Tkinter |
| 374 | ----------------------------- |
| 375 | |
| 376 | Class commands in Tk correspond to class constructors in Tkinter. :: |
| 377 | |
| 378 | button .fred =====> fred = Button() |
| 379 | |
| 380 | The master of an object is implicit in the new name given to it at creation |
| 381 | time. In Tkinter, masters are specified explicitly. :: |
| 382 | |
| 383 | button .panel.fred =====> fred = Button(panel) |
| 384 | |
| 385 | The configuration options in Tk are given in lists of hyphened tags followed by |
| 386 | values. In Tkinter, options are specified as keyword-arguments in the instance |
| 387 | constructor, and keyword-args for configure calls or as instance indices, in |
| 388 | dictionary style, for established instances. See section |
| 389 | :ref:`tkinter-setting-options` on setting options. :: |
| 390 | |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 391 | button .fred -fg red =====> fred = Button(panel, fg="red") |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 392 | .fred configure -fg red =====> fred["fg"] = red |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 393 | OR ==> fred.config(fg="red") |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 394 | |
| 395 | In Tk, to perform an action on a widget, use the widget name as a command, and |
| 396 | follow it with an action name, possibly with arguments (options). In Tkinter, |
| 397 | 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] | 398 | actions (methods) that a given widget can perform are listed in |
| 399 | :file:`tkinter/__init__.py`. :: |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 400 | |
| 401 | .fred invoke =====> fred.invoke() |
| 402 | |
| 403 | To give a widget to the packer (geometry manager), you call pack with optional |
| 404 | arguments. In Tkinter, the Pack class holds all this functionality, and the |
| 405 | 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] | 406 | :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] | 407 | methods. See the :mod:`tkinter.tix` module documentation for additional |
| Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 408 | information on the Form geometry manager. :: |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 409 | |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 410 | pack .fred -side left =====> fred.pack(side="left") |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 411 | |
| 412 | |
| 413 | How Tk and Tkinter are Related |
| 414 | ------------------------------ |
| 415 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 416 | From the top down: |
| 417 | |
| 418 | Your App Here (Python) |
| Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 419 | A Python application makes a :mod:`tkinter` call. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 420 | |
| Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 421 | tkinter (Python Package) |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 422 | This call (say, for example, creating a button widget), is implemented in |
| 423 | the :mod:`tkinter` package, which is written in Python. This Python |
| 424 | function will parse the commands and the arguments and convert them into a |
| 425 | form that makes them look as if they had come from a Tk script instead of |
| 426 | a Python script. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 427 | |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 428 | _tkinter (C) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 429 | 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] | 430 | :mod:`_tkinter` - note the underscore - extension module. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 431 | |
| 432 | Tk Widgets (C and Tcl) |
| 433 | This C function is able to make calls into other C modules, including the C |
| 434 | functions that make up the Tk library. Tk is implemented in C and some Tcl. |
| 435 | 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] | 436 | widgets, and is executed once at the point where the Python :mod:`tkinter` |
| 437 | package is imported. (The user never sees this stage). |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 438 | |
| 439 | Tk (C) |
| 440 | The Tk part of the Tk Widgets implement the final mapping to ... |
| 441 | |
| 442 | Xlib (C) |
| 443 | the Xlib library to draw graphics on the screen. |
| 444 | |
| 445 | |
| Miss Islington (bot) | 2666d70 | 2021-08-11 10:16:10 -0700 | [diff] [blame] | 446 | Threading model |
| 447 | --------------- |
| 448 | |
| 449 | Python and Tcl/Tk have very different threading models, which :mod:`tkinter` |
| 450 | tries to bridge. If you use threads, you may need to be aware of this. |
| 451 | |
| 452 | A Python interpreter may have many threads associated with it. In Tcl, multiple |
| 453 | threads can be created, but each thread has a separate Tcl interpreter instance |
| 454 | associated with it. Threads can also create more than one interpreter instance, |
| 455 | though each interpreter instance can be used only by the one thread that created it. |
| 456 | |
| 457 | Each :class:`Tk` object created by :mod:`tkinter` contains a Tcl interpreter. |
| 458 | It also keeps track of which thread created that interpreter. Calls to |
| 459 | :mod:`tkinter` can be made from any Python thread. Internally, if a call comes |
| 460 | from a thread other than the one that created the :class:`Tk` object, an event |
| 461 | is posted to the interpreter's event queue, and when executed, the result is |
| 462 | returned to the calling Python thread. |
| 463 | |
| 464 | Tcl/Tk applications are normally event-driven, meaning that after initialization, |
| 465 | the interpreter runs an event loop (i.e. :func:`Tk.mainloop`) and responds to events. |
| 466 | Because it is single-threaded, event handlers must respond quickly, otherwise they |
| 467 | will block other events from being processed. To avoid this, any long-running |
| 468 | computations should not run in an event handler, but are either broken into smaller |
| 469 | pieces using timers, or run in another thread. This is different from many GUI |
| 470 | toolkits where the GUI runs in a completely separate thread from all application |
| 471 | code including event handlers. |
| 472 | |
| 473 | If the Tcl interpreter is not running the event loop and processing events, any |
| 474 | :mod:`tkinter` calls made from threads other than the one running the Tcl |
| 475 | interpreter will fail. |
| 476 | |
| 477 | A number of special cases exist: |
| 478 | |
| 479 | * Tcl/Tk libraries can be built so they are not thread-aware. In this case, |
| 480 | :mod:`tkinter` calls the library from the originating Python thread, even |
| 481 | if this is different than the thread that created the Tcl interpreter. A global |
| 482 | lock ensures only one call occurs at a time. |
| 483 | |
| 484 | * While :mod:`tkinter` allows you to create more than one instance of a :class:`Tk` |
| 485 | object (with its own interpreter), all interpreters that are part of the same |
| 486 | thread share a common event queue, which gets ugly fast. In practice, don't create |
| 487 | more than one instance of :class:`Tk` at a time. Otherwise, it's best to create |
| 488 | them in separate threads and ensure you're running a thread-aware Tcl/Tk build. |
| 489 | |
| 490 | * Blocking event handlers are not the only way to prevent the Tcl interpreter from |
| 491 | reentering the event loop. It is even possible to run multiple nested event loops |
| 492 | or abandon the event loop entirely. If you're doing anything tricky when it comes |
| 493 | to events or threads, be aware of these possibilities. |
| 494 | |
| 495 | * There are a few select :mod:`tkinter` functions that presently work only when |
| 496 | called from the thread that created the Tcl interpreter. |
| 497 | |
| 498 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 499 | Handy Reference |
| 500 | --------------- |
| 501 | |
| 502 | |
| 503 | .. _tkinter-setting-options: |
| 504 | |
| 505 | Setting Options |
| 506 | ^^^^^^^^^^^^^^^ |
| 507 | |
| 508 | Options control things like the color and border width of a widget. Options can |
| 509 | be set in three ways: |
| 510 | |
| 511 | At object creation time, using keyword arguments |
| 512 | :: |
| 513 | |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 514 | fred = Button(self, fg="red", bg="blue") |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 515 | |
| 516 | After object creation, treating the option name like a dictionary index |
| 517 | :: |
| 518 | |
| 519 | fred["fg"] = "red" |
| 520 | fred["bg"] = "blue" |
| 521 | |
| 522 | Use the config() method to update multiple attrs subsequent to object creation |
| 523 | :: |
| 524 | |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 525 | fred.config(fg="red", bg="blue") |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 526 | |
| 527 | For a complete explanation of a given option and its behavior, see the Tk man |
| 528 | pages for the widget in question. |
| 529 | |
| 530 | Note that the man pages list "STANDARD OPTIONS" and "WIDGET SPECIFIC OPTIONS" |
| 531 | for each widget. The former is a list of options that are common to many |
| 532 | widgets, the latter are the options that are idiosyncratic to that particular |
| 533 | widget. The Standard Options are documented on the :manpage:`options(3)` man |
| 534 | page. |
| 535 | |
| 536 | No distinction between standard and widget-specific options is made in this |
| 537 | document. Some options don't apply to some kinds of widgets. Whether a given |
| 538 | widget responds to a particular option depends on the class of the widget; |
| 539 | buttons have a ``command`` option, labels do not. |
| 540 | |
| 541 | The options supported by a given widget are listed in that widget's man page, or |
| 542 | can be queried at runtime by calling the :meth:`config` method without |
| 543 | arguments, or by calling the :meth:`keys` method on that widget. The return |
| 544 | value of these calls is a dictionary whose key is the name of the option as a |
| 545 | string (for example, ``'relief'``) and whose values are 5-tuples. |
| 546 | |
| 547 | Some options, like ``bg`` are synonyms for common options with long names |
| 548 | (``bg`` is shorthand for "background"). Passing the ``config()`` method the name |
| 549 | of a shorthand option will return a 2-tuple, not 5-tuple. The 2-tuple passed |
| 550 | back will contain the name of the synonym and the "real" option (such as |
| 551 | ``('bg', 'background')``). |
| 552 | |
| 553 | +-------+---------------------------------+--------------+ |
| 554 | | Index | Meaning | Example | |
| 555 | +=======+=================================+==============+ |
| 556 | | 0 | option name | ``'relief'`` | |
| 557 | +-------+---------------------------------+--------------+ |
| 558 | | 1 | option name for database lookup | ``'relief'`` | |
| 559 | +-------+---------------------------------+--------------+ |
| 560 | | 2 | option class for database | ``'Relief'`` | |
| 561 | | | lookup | | |
| 562 | +-------+---------------------------------+--------------+ |
| 563 | | 3 | default value | ``'raised'`` | |
| 564 | +-------+---------------------------------+--------------+ |
| 565 | | 4 | current value | ``'groove'`` | |
| 566 | +-------+---------------------------------+--------------+ |
| 567 | |
| 568 | Example:: |
| 569 | |
| Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 570 | >>> print(fred.config()) |
| Serhiy Storchaka | f47036c | 2013-12-24 11:04:36 +0200 | [diff] [blame] | 571 | {'relief': ('relief', 'relief', 'Relief', 'raised', 'groove')} |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 572 | |
| 573 | Of course, the dictionary printed will include all the options available and |
| 574 | their values. This is meant only as an example. |
| 575 | |
| 576 | |
| 577 | The Packer |
| 578 | ^^^^^^^^^^ |
| 579 | |
| 580 | .. index:: single: packing (widgets) |
| 581 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 582 | The packer is one of Tk's geometry-management mechanisms. Geometry managers |
| Nathan M | ecaf949 | 2020-08-02 22:13:03 -0400 | [diff] [blame] | 583 | are used to specify the relative positioning of widgets within their container - |
| 584 | their mutual *master*. In contrast to the more cumbersome *placer* (which is |
| 585 | used less commonly, and we do not cover here), the packer takes qualitative |
| 586 | relationship specification - *above*, *to the left of*, *filling*, etc - and |
| 587 | works everything out to determine the exact placement coordinates for you. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 588 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 589 | The size of any *master* widget is determined by the size of the "slave widgets" |
| 590 | inside. The packer is used to control where slave widgets appear inside the |
| 591 | master into which they are packed. You can pack widgets into frames, and frames |
| 592 | into other frames, in order to achieve the kind of layout you desire. |
| 593 | Additionally, the arrangement is dynamically adjusted to accommodate incremental |
| 594 | changes to the configuration, once it is packed. |
| 595 | |
| 596 | Note that widgets do not appear until they have had their geometry specified |
| 597 | with a geometry manager. It's a common early mistake to leave out the geometry |
| 598 | specification, and then be surprised when the widget is created but nothing |
| 599 | appears. A widget will appear only after it has had, for example, the packer's |
| 600 | :meth:`pack` method applied to it. |
| 601 | |
| 602 | The pack() method can be called with keyword-option/value pairs that control |
| 603 | where the widget is to appear within its container, and how it is to behave when |
| 604 | the main application window is resized. Here are some examples:: |
| 605 | |
| 606 | fred.pack() # defaults to side = "top" |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 607 | fred.pack(side="left") |
| 608 | fred.pack(expand=1) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 609 | |
| 610 | |
| 611 | Packer Options |
| 612 | ^^^^^^^^^^^^^^ |
| 613 | |
| 614 | For more extensive information on the packer and the options that it can take, |
| 615 | see the man pages and page 183 of John Ousterhout's book. |
| 616 | |
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 617 | anchor |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 618 | Anchor type. Denotes where the packer is to place each slave in its parcel. |
| 619 | |
| 620 | expand |
| 621 | Boolean, ``0`` or ``1``. |
| 622 | |
| 623 | fill |
| 624 | Legal values: ``'x'``, ``'y'``, ``'both'``, ``'none'``. |
| 625 | |
| 626 | ipadx and ipady |
| 627 | A distance - designating internal padding on each side of the slave widget. |
| 628 | |
| 629 | padx and pady |
| 630 | A distance - designating external padding on each side of the slave widget. |
| 631 | |
| 632 | side |
| 633 | Legal values are: ``'left'``, ``'right'``, ``'top'``, ``'bottom'``. |
| 634 | |
| 635 | |
| 636 | Coupling Widget Variables |
| 637 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 638 | |
| 639 | The current-value setting of some widgets (like text entry widgets) can be |
| 640 | connected directly to application variables by using special options. These |
| 641 | options are ``variable``, ``textvariable``, ``onvalue``, ``offvalue``, and |
| 642 | ``value``. This connection works both ways: if the variable changes for any |
| 643 | reason, the widget it's connected to will be updated to reflect the new value. |
| 644 | |
| Georg Brandl | ac6060c | 2008-05-17 18:44:45 +0000 | [diff] [blame] | 645 | Unfortunately, in the current implementation of :mod:`tkinter` it is not |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 646 | possible to hand over an arbitrary Python variable to a widget through a |
| 647 | ``variable`` or ``textvariable`` option. The only kinds of variables for which |
| 648 | this works are variables that are subclassed from a class called Variable, |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 649 | defined in :mod:`tkinter`. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 650 | |
| 651 | There are many useful subclasses of Variable already defined: |
| 652 | :class:`StringVar`, :class:`IntVar`, :class:`DoubleVar`, and |
| 653 | :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] | 654 | :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] | 655 | method. If you follow this protocol, the widget will always track the value of |
| 656 | the variable, with no further intervention on your part. |
| 657 | |
| 658 | For example:: |
| 659 | |
| Ankit Chandawala | c36dbac | 2020-08-03 05:03:48 +0100 | [diff] [blame] | 660 | import tkinter as tk |
| 661 | |
| 662 | class App(tk.Frame): |
| 663 | def __init__(self, master): |
| Berker Peksag | 3093bf1 | 2016-07-14 07:32:43 +0300 | [diff] [blame] | 664 | super().__init__(master) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 665 | self.pack() |
| 666 | |
| Ankit Chandawala | c36dbac | 2020-08-03 05:03:48 +0100 | [diff] [blame] | 667 | self.entrythingy = tk.Entry() |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 668 | self.entrythingy.pack() |
| 669 | |
| Ankit Chandawala | c36dbac | 2020-08-03 05:03:48 +0100 | [diff] [blame] | 670 | # Create the application variable. |
| 671 | self.contents = tk.StringVar() |
| 672 | # Set it to some value. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 673 | self.contents.set("this is a variable") |
| Ankit Chandawala | c36dbac | 2020-08-03 05:03:48 +0100 | [diff] [blame] | 674 | # Tell the entry widget to watch this variable. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 675 | self.entrythingy["textvariable"] = self.contents |
| 676 | |
| Ankit Chandawala | c36dbac | 2020-08-03 05:03:48 +0100 | [diff] [blame] | 677 | # Define a callback for when the user hits return. |
| 678 | # It prints the current value of the variable. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 679 | self.entrythingy.bind('<Key-Return>', |
| Ankit Chandawala | c36dbac | 2020-08-03 05:03:48 +0100 | [diff] [blame] | 680 | self.print_contents) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 681 | |
| 682 | def print_contents(self, event): |
| Ankit Chandawala | c36dbac | 2020-08-03 05:03:48 +0100 | [diff] [blame] | 683 | print("Hi. The current entry content is:", |
| Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 684 | self.contents.get()) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 685 | |
| Ankit Chandawala | c36dbac | 2020-08-03 05:03:48 +0100 | [diff] [blame] | 686 | root = tk.Tk() |
| 687 | myapp = App(root) |
| 688 | myapp.mainloop() |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 689 | |
| 690 | The Window Manager |
| 691 | ^^^^^^^^^^^^^^^^^^ |
| 692 | |
| 693 | .. index:: single: window manager (widgets) |
| 694 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 695 | In Tk, there is a utility command, ``wm``, for interacting with the window |
| 696 | 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] | 697 | placement, icon bitmaps, and the like. In :mod:`tkinter`, these commands have |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 698 | been implemented as methods on the :class:`Wm` class. Toplevel widgets are |
| 699 | subclassed from the :class:`Wm` class, and so can call the :class:`Wm` methods |
| 700 | directly. |
| 701 | |
| 702 | To get at the toplevel window that contains a given widget, you can often just |
| 703 | refer to the widget's master. Of course if the widget has been packed inside of |
| 704 | a frame, the master won't represent a toplevel window. To get at the toplevel |
| 705 | window that contains an arbitrary widget, you can call the :meth:`_root` method. |
| 706 | This method begins with an underscore to denote the fact that this function is |
| 707 | part of the implementation, and not an interface to Tk functionality. |
| 708 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 709 | Here are some examples of typical usage:: |
| 710 | |
| Berker Peksag | 3093bf1 | 2016-07-14 07:32:43 +0300 | [diff] [blame] | 711 | import tkinter as tk |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 712 | |
| Berker Peksag | 3093bf1 | 2016-07-14 07:32:43 +0300 | [diff] [blame] | 713 | class App(tk.Frame): |
| 714 | def __init__(self, master=None): |
| 715 | super().__init__(master) |
| 716 | self.pack() |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 717 | |
| 718 | # create the application |
| 719 | myapp = App() |
| 720 | |
| 721 | # |
| 722 | # here are method calls to the window manager class |
| 723 | # |
| 724 | myapp.master.title("My Do-Nothing Application") |
| 725 | myapp.master.maxsize(1000, 400) |
| 726 | |
| 727 | # start the program |
| 728 | myapp.mainloop() |
| 729 | |
| 730 | |
| 731 | Tk Option Data Types |
| 732 | ^^^^^^^^^^^^^^^^^^^^ |
| 733 | |
| 734 | .. index:: single: Tk Option Data Types |
| 735 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 736 | anchor |
| 737 | Legal values are points of the compass: ``"n"``, ``"ne"``, ``"e"``, ``"se"``, |
| 738 | ``"s"``, ``"sw"``, ``"w"``, ``"nw"``, and also ``"center"``. |
| 739 | |
| 740 | bitmap |
| 741 | There are eight built-in, named bitmaps: ``'error'``, ``'gray25'``, |
| 742 | ``'gray50'``, ``'hourglass'``, ``'info'``, ``'questhead'``, ``'question'``, |
| 743 | ``'warning'``. To specify an X bitmap filename, give the full path to the file, |
| 744 | preceded with an ``@``, as in ``"@/usr/contrib/bitmap/gumby.bit"``. |
| 745 | |
| 746 | boolean |
| Serhiy Storchaka | a4d170d | 2013-12-23 18:20:51 +0200 | [diff] [blame] | 747 | 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] | 748 | |
| 749 | callback |
| 750 | This is any Python function that takes no arguments. For example:: |
| 751 | |
| 752 | def print_it(): |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 753 | print("hi there") |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 754 | fred["command"] = print_it |
| 755 | |
| 756 | color |
| 757 | Colors can be given as the names of X colors in the rgb.txt file, or as strings |
| 758 | representing RGB values in 4 bit: ``"#RGB"``, 8 bit: ``"#RRGGBB"``, 12 bit" |
| 759 | ``"#RRRGGGBBB"``, or 16 bit ``"#RRRRGGGGBBBB"`` ranges, where R,G,B here |
| 760 | represent any legal hex digit. See page 160 of Ousterhout's book for details. |
| 761 | |
| 762 | cursor |
| 763 | The standard X cursor names from :file:`cursorfont.h` can be used, without the |
| 764 | ``XC_`` prefix. For example to get a hand cursor (:const:`XC_hand2`), use the |
| 765 | string ``"hand2"``. You can also specify a bitmap and mask file of your own. |
| 766 | See page 179 of Ousterhout's book. |
| 767 | |
| 768 | distance |
| 769 | Screen distances can be specified in either pixels or absolute distances. |
| 770 | Pixels are given as numbers and absolute distances as strings, with the trailing |
| 771 | character denoting units: ``c`` for centimetres, ``i`` for inches, ``m`` for |
| 772 | millimetres, ``p`` for printer's points. For example, 3.5 inches is expressed |
| 773 | as ``"3.5i"``. |
| 774 | |
| 775 | font |
| 776 | Tk uses a list font name format, such as ``{courier 10 bold}``. Font sizes with |
| 777 | positive numbers are measured in points; sizes with negative numbers are |
| 778 | measured in pixels. |
| 779 | |
| 780 | geometry |
| 781 | This is a string of the form ``widthxheight``, where width and height are |
| 782 | measured in pixels for most widgets (in characters for widgets displaying text). |
| 783 | For example: ``fred["geometry"] = "200x100"``. |
| 784 | |
| 785 | justify |
| 786 | Legal values are the strings: ``"left"``, ``"center"``, ``"right"``, and |
| 787 | ``"fill"``. |
| 788 | |
| 789 | region |
| 790 | This is a string with four space-delimited elements, each of which is a legal |
| 791 | distance (see above). For example: ``"2 3 4 5"`` and ``"3i 2i 4.5i 2i"`` and |
| 792 | ``"3c 2c 4c 10.43c"`` are all legal regions. |
| 793 | |
| 794 | relief |
| 795 | Determines what the border style of a widget will be. Legal values are: |
| 796 | ``"raised"``, ``"sunken"``, ``"flat"``, ``"groove"``, and ``"ridge"``. |
| 797 | |
| 798 | scrollcommand |
| Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 799 | 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] | 800 | be any widget method that takes a single argument. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 801 | |
| Nikhil | 80428ed | 2019-09-10 01:55:34 -0700 | [diff] [blame] | 802 | wrap |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 803 | Must be one of: ``"none"``, ``"char"``, or ``"word"``. |
| 804 | |
| Nikhil | 80428ed | 2019-09-10 01:55:34 -0700 | [diff] [blame] | 805 | .. _Bindings-and-Events: |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 806 | |
| 807 | Bindings and Events |
| 808 | ^^^^^^^^^^^^^^^^^^^ |
| 809 | |
| 810 | .. index:: |
| 811 | single: bind (widgets) |
| 812 | single: events (widgets) |
| 813 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 814 | The bind method from the widget command allows you to watch for certain events |
| 815 | and to have a callback function trigger when that event type occurs. The form |
| 816 | of the bind method is:: |
| 817 | |
| 818 | def bind(self, sequence, func, add=''): |
| 819 | |
| 820 | where: |
| 821 | |
| 822 | sequence |
| 823 | is a string that denotes the target kind of event. (See the bind man page and |
| 824 | page 201 of John Ousterhout's book for details). |
| 825 | |
| 826 | func |
| 827 | is a Python function, taking one argument, to be invoked when the event occurs. |
| 828 | An Event instance will be passed as the argument. (Functions deployed this way |
| 829 | are commonly known as *callbacks*.) |
| 830 | |
| 831 | add |
| 832 | is optional, either ``''`` or ``'+'``. Passing an empty string denotes that |
| 833 | this binding is to replace any other bindings that this event is associated |
| 834 | with. Passing a ``'+'`` means that this function is to be added to the list |
| 835 | of functions bound to this event type. |
| 836 | |
| 837 | For example:: |
| 838 | |
| Berker Peksag | 3093bf1 | 2016-07-14 07:32:43 +0300 | [diff] [blame] | 839 | def turn_red(self, event): |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 840 | event.widget["activeforeground"] = "red" |
| 841 | |
| Berker Peksag | 3093bf1 | 2016-07-14 07:32:43 +0300 | [diff] [blame] | 842 | self.button.bind("<Enter>", self.turn_red) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 843 | |
| 844 | Notice how the widget field of the event is being accessed in the |
| Berker Peksag | 3093bf1 | 2016-07-14 07:32:43 +0300 | [diff] [blame] | 845 | ``turn_red()`` callback. This field contains the widget that caught the X |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 846 | event. The following table lists the other event fields you can access, and how |
| 847 | 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] | 848 | |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 849 | +----+---------------------+----+---------------------+ |
| 850 | | Tk | Tkinter Event Field | Tk | Tkinter Event Field | |
| 851 | +====+=====================+====+=====================+ |
| 852 | | %f | focus | %A | char | |
| 853 | +----+---------------------+----+---------------------+ |
| 854 | | %h | height | %E | send_event | |
| 855 | +----+---------------------+----+---------------------+ |
| 856 | | %k | keycode | %K | keysym | |
| 857 | +----+---------------------+----+---------------------+ |
| 858 | | %s | state | %N | keysym_num | |
| 859 | +----+---------------------+----+---------------------+ |
| 860 | | %t | time | %T | type | |
| 861 | +----+---------------------+----+---------------------+ |
| 862 | | %w | width | %W | widget | |
| 863 | +----+---------------------+----+---------------------+ |
| 864 | | %x | x | %X | x_root | |
| 865 | +----+---------------------+----+---------------------+ |
| 866 | | %y | y | %Y | y_root | |
| 867 | +----+---------------------+----+---------------------+ |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 868 | |
| 869 | |
| 870 | The index Parameter |
| 871 | ^^^^^^^^^^^^^^^^^^^ |
| 872 | |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 873 | 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] | 874 | point at a specific place in a Text widget, or to particular characters in an |
| 875 | Entry widget, or to particular menu items in a Menu widget. |
| 876 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 877 | Entry widget indexes (index, view index, etc.) |
| 878 | 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] | 879 | displayed. You can use these :mod:`tkinter` functions to access these special |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 880 | points in text widgets: |
| 881 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 882 | Text widget indexes |
| 883 | The index notation for Text widgets is very rich and is best described in the Tk |
| 884 | man pages. |
| 885 | |
| 886 | Menu indexes (menu.invoke(), menu.entryconfig(), etc.) |
| 887 | Some options and methods for menus manipulate specific menu entries. Anytime a |
| 888 | menu index is needed for an option or a parameter, you may pass in: |
| 889 | |
| 890 | * an integer which refers to the numeric position of the entry in the widget, |
| 891 | counted from the top, starting with 0; |
| 892 | |
| Ezio Melotti | 1a263ad | 2010-03-14 09:51:37 +0000 | [diff] [blame] | 893 | * the string ``"active"``, which refers to the menu position that is currently |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 894 | under the cursor; |
| 895 | |
| 896 | * the string ``"last"`` which refers to the last menu item; |
| 897 | |
| 898 | * An integer preceded by ``@``, as in ``@6``, where the integer is interpreted |
| 899 | as a y pixel coordinate in the menu's coordinate system; |
| 900 | |
| 901 | * the string ``"none"``, which indicates no menu entry at all, most often used |
| 902 | with menu.activate() to deactivate all entries, and finally, |
| 903 | |
| 904 | * a text string that is pattern matched against the label of the menu entry, as |
| 905 | scanned from the top of the menu to the bottom. Note that this index type is |
| 906 | considered after all the others, which means that matches for menu items |
| 907 | labelled ``last``, ``active``, or ``none`` may be interpreted as the above |
| 908 | literals, instead. |
| 909 | |
| 910 | |
| 911 | Images |
| 912 | ^^^^^^ |
| 913 | |
| Andrés Delfino | 4b685bf | 2018-04-17 02:34:35 -0300 | [diff] [blame] | 914 | Images of different formats can be created through the corresponding subclass |
| 915 | of :class:`tkinter.Image`: |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 916 | |
| Andrés Delfino | 4b685bf | 2018-04-17 02:34:35 -0300 | [diff] [blame] | 917 | * :class:`BitmapImage` for images in XBM format. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 918 | |
| Andrés Delfino | 4b685bf | 2018-04-17 02:34:35 -0300 | [diff] [blame] | 919 | * :class:`PhotoImage` for images in PGM, PPM, GIF and PNG formats. The latter |
| 920 | is supported starting with Tk 8.6. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 921 | |
| 922 | Either type of image is created through either the ``file`` or the ``data`` |
| 923 | option (other options are available as well). |
| 924 | |
| 925 | The image object can then be used wherever an ``image`` option is supported by |
| 926 | some widget (e.g. labels, buttons, menus). In these cases, Tk will not keep a |
| 927 | reference to the image. When the last Python reference to the image object is |
| 928 | deleted, the image data is deleted as well, and Tk will display an empty box |
| 929 | wherever the image was used. |
| Terry Jan Reedy | d986563 | 2015-05-17 14:49:26 -0400 | [diff] [blame] | 930 | |
| Andrés Delfino | b81ca28 | 2018-04-21 09:17:26 -0300 | [diff] [blame] | 931 | .. seealso:: |
| 932 | |
| 933 | The `Pillow <http://python-pillow.org/>`_ package adds support for |
| 934 | formats such as BMP, JPEG, TIFF, and WebP, among others. |
| Terry Jan Reedy | d986563 | 2015-05-17 14:49:26 -0400 | [diff] [blame] | 935 | |
| 936 | .. _tkinter-file-handlers: |
| 937 | |
| 938 | File Handlers |
| 939 | ------------- |
| 940 | |
| 941 | Tk allows you to register and unregister a callback function which will be |
| 942 | called from the Tk mainloop when I/O is possible on a file descriptor. |
| 943 | Only one handler may be registered per file descriptor. Example code:: |
| 944 | |
| 945 | import tkinter |
| 946 | widget = tkinter.Tk() |
| 947 | mask = tkinter.READABLE | tkinter.WRITABLE |
| 948 | widget.tk.createfilehandler(file, mask, callback) |
| 949 | ... |
| 950 | widget.tk.deletefilehandler(file) |
| 951 | |
| 952 | This feature is not available on Windows. |
| 953 | |
| 954 | Since you don't know how many bytes are available for reading, you may not |
| 955 | want to use the :class:`~io.BufferedIOBase` or :class:`~io.TextIOBase` |
| 956 | :meth:`~io.BufferedIOBase.read` or :meth:`~io.IOBase.readline` methods, |
| 957 | since these will insist on reading a predefined number of bytes. |
| 958 | For sockets, the :meth:`~socket.socket.recv` or |
| 959 | :meth:`~socket.socket.recvfrom` methods will work fine; for other files, |
| 960 | use raw reads or ``os.read(file.fileno(), maxbytecount)``. |
| 961 | |
| 962 | |
| 963 | .. method:: Widget.tk.createfilehandler(file, mask, func) |
| 964 | |
| 965 | Registers the file handler callback function *func*. The *file* argument |
| 966 | may either be an object with a :meth:`~io.IOBase.fileno` method (such as |
| 967 | a file or socket object), or an integer file descriptor. The *mask* |
| 968 | argument is an ORed combination of any of the three constants below. |
| 969 | The callback is called as follows:: |
| 970 | |
| 971 | callback(file, mask) |
| 972 | |
| 973 | |
| 974 | .. method:: Widget.tk.deletefilehandler(file) |
| 975 | |
| 976 | Unregisters a file handler. |
| 977 | |
| 978 | |
| 979 | .. data:: READABLE |
| 980 | WRITABLE |
| 981 | EXCEPTION |
| 982 | |
| Ankit Chandawala | c36dbac | 2020-08-03 05:03:48 +0100 | [diff] [blame] | 983 | Constants used in the *mask* arguments. |