33
Most of the time, the :mod:`tkinter` is all you really need, but a number
34
of additional modules are available as well. The Tk interface is located in a
33
Most of the time, :mod:`tkinter` is all you really need, but a number of
34
additional modules are available as well. The Tk interface is located in a
35
35
binary module named :mod:`_tkinter`. This module contains the low-level
36
36
interface to Tk, and should never be used directly by application programmers.
37
37
It is usually a shared library (or DLL), but might in some cases be statically
115
* Tk was written by John Ousterhout while at Berkeley.
115
117
* Tkinter was written by Steen Lumholt and Guido van Rossum.
117
* Tk was written by John Ousterhout while at Berkeley.
119
119
* This Life Preserver was written by Matt Conway at the University of Virginia.
121
* The html rendering, and some liberal editing, was produced from a FrameMaker
121
* The HTML rendering, and some liberal editing, was produced from a FrameMaker
122
122
version by Ken Manheimer.
124
124
* Fredrik Lundh elaborated and revised the class interface descriptions, to get
143
143
can't fulfill that role, so the best we can do is point you to the best
144
144
documentation that exists. Here are some hints:
146
* The authors strongly suggest getting a copy of the Tk man pages. Specifically,
147
the man pages in the ``mann`` directory are most useful. The ``man3`` man pages
148
describe the C interface to the Tk library and thus are not especially helpful
146
* The authors strongly suggest getting a copy of the Tk man pages.
147
Specifically, the man pages in the ``manN`` directory are most useful.
148
The ``man3`` man pages describe the C interface to the Tk library and thus
149
are not especially helpful for script writers.
151
151
* Addison-Wesley publishes a book called Tcl and the Tk Toolkit by John
152
152
Ousterhout (ISBN 0-201-63337-X) which is a good introduction to Tcl and Tk for
162
`Tcl/Tk 8.6 man pages <http://www.tcl.tk/man/tcl8.6/>`_
163
The Tcl/Tk manual on www.tcl.tk.
162
165
`ActiveState Tcl Home Page <http://tcl.activestate.com/>`_
163
166
The Tk/Tcl development is largely taking place at ActiveState.
301
304
dictionary style, for established instances. See section
302
305
:ref:`tkinter-setting-options` on setting options. ::
304
button .fred -fg red =====> fred = Button(panel, fg = "red")
307
button .fred -fg red =====> fred = Button(panel, fg="red")
305
308
.fred configure -fg red =====> fred["fg"] = red
306
OR ==> fred.config(fg = "red")
309
OR ==> fred.config(fg="red")
308
311
In Tk, to perform an action on a widget, use the widget name as a command, and
309
312
follow it with an action name, possibly with arguments (options). In Tkinter,
310
313
you call methods on the class instance to invoke actions on the widget. The
311
actions (methods) that a given widget can perform are listed in the Tkinter.py
314
actions (methods) that a given widget can perform are listed in
315
:file:`tkinter/__init__.py`. ::
314
317
.fred invoke =====> fred.invoke()
320
323
methods. See the :mod:`tkinter.tix` module documentation for additional
321
324
information on the Form geometry manager. ::
323
pack .fred -side left =====> fred.pack(side = "left")
326
pack .fred -side left =====> fred.pack(side="left")
326
329
How Tk and Tkinter are Related
332
335
A Python application makes a :mod:`tkinter` call.
334
337
tkinter (Python Package)
335
This call (say, for example, creating a button widget), is implemented in the
336
*tkinter* package, which is written in Python. This Python function will parse
337
the commands and the arguments and convert them into a form that makes them look
338
as if they had come from a Tk script instead of a Python script.
338
This call (say, for example, creating a button widget), is implemented in
339
the :mod:`tkinter` package, which is written in Python. This Python
340
function will parse the commands and the arguments and convert them into a
341
form that makes them look as if they had come from a Tk script instead of
341
345
These commands and their arguments will be passed to a C function in the
342
*tkinter* - note the lowercase - extension module.
346
:mod:`_tkinter` - note the underscore - extension module.
344
348
Tk Widgets (C and Tcl)
345
349
This C function is able to make calls into other C modules, including the C
370
374
At object creation time, using keyword arguments
373
fred = Button(self, fg = "red", bg = "blue")
377
fred = Button(self, fg="red", bg="blue")
375
379
After object creation, treating the option name like a dictionary index
381
385
Use the config() method to update multiple attrs subsequent to object creation
384
fred.config(fg = "red", bg = "blue")
388
fred.config(fg="red", bg="blue")
386
390
For a complete explanation of a given option and its behavior, see the Tk man
387
391
pages for the widget in question.
506
510
possible to hand over an arbitrary Python variable to a widget through a
507
511
``variable`` or ``textvariable`` option. The only kinds of variables for which
508
512
this works are variables that are subclassed from a class called Variable,
509
defined in the :mod:`tkinter`.
513
defined in :mod:`tkinter`.
511
515
There are many useful subclasses of Variable already defined:
512
516
:class:`StringVar`, :class:`IntVar`, :class:`DoubleVar`, and
702
706
:meth:`turnRed` callback. This field contains the widget that caught the X
703
707
event. The following table lists the other event fields you can access, and how
704
708
they are denoted in Tk, which can be useful when referring to the Tk man pages.
707
Tk Tkinter Event Field Tk Tkinter Event Field
708
-- ------------------- -- -------------------
710
%h height %E send_event
712
%s state %N keysym_num
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
+----+---------------------+----+---------------------+
719
731
The index Parameter
720
732
^^^^^^^^^^^^^^^^^^^
722
A number of widgets require"index" parameters to be passed. These are used to
734
A number of widgets require "index" parameters to be passed. These are used to
723
735
point at a specific place in a Text widget, or to particular characters in an
724
736
Entry widget, or to particular menu items in a Menu widget.
755
767
* an integer which refers to the numeric position of the entry in the widget,
756
768
counted from the top, starting with 0;
758
* the string ``'active'``, which refers to the menu position that is currently
770
* the string ``"active"``, which refers to the menu position that is currently
759
771
under the cursor;
761
773
* the string ``"last"`` which refers to the last menu item;