~ubuntu-branches/ubuntu/natty/python3.1/natty-security

« back to all changes in this revision

Viewing changes to Doc/library/tkinter.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-21 17:27:57 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20100321172757-v29qykjsa8u718lo
Tags: 3.1.2-0ubuntu1
* Python 3.1.2 release.
* Fix issue #4961: Inconsistent/wrong result of askyesno function in
  tkMessageBox with Tcl8.5. LP: #462950.
* Don't complain when /usr/local is not writable on installation.
* Apply proposed patch for issue #8032, gdb7 hooks for debugging.
* Backport issue #8140: Extend compileall to compile single files.
  Add -i option.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
Tkinter Modules
31
31
---------------
32
32
 
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
112
112
 
113
113
Credits:
114
114
 
 
115
* Tk was written by John Ousterhout while at Berkeley.
 
116
 
115
117
* Tkinter was written by Steen Lumholt and Guido van Rossum.
116
118
 
117
 
* Tk was written by John Ousterhout while at Berkeley.
118
 
 
119
119
* This Life Preserver was written by Matt Conway at the University of Virginia.
120
120
 
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.
123
123
 
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:
145
145
 
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
149
 
  for script writers.
 
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.
150
150
 
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
159
159
 
160
160
.. seealso::
161
161
 
 
162
   `Tcl/Tk 8.6 man pages <http://www.tcl.tk/man/tcl8.6/>`_
 
163
      The Tcl/Tk manual on www.tcl.tk.
 
164
 
162
165
   `ActiveState Tcl Home Page <http://tcl.activestate.com/>`_
163
166
      The Tk/Tcl development is largely taking place at ActiveState.
164
167
 
183
186
       def createWidgets(self):
184
187
           self.QUIT = Button(self)
185
188
           self.QUIT["text"] = "QUIT"
186
 
           self.QUIT["fg"]   = "red"
187
 
           self.QUIT["command"] =  self.quit
 
189
           self.QUIT["fg"] = "red"
 
190
           self.QUIT["command"] = self.quit
188
191
 
189
192
           self.QUIT.pack({"side": "left"})
190
193
 
257
260
For example::
258
261
 
259
262
   button   .fred   -fg red -text "hi there"
260
 
      ^       ^     \_____________________/
 
263
      ^       ^     \______________________/
261
264
      |       |                |
262
265
    class    new            options
263
266
   command  widget  (-opt val -opt val ...)
301
304
dictionary style, for established instances.  See section
302
305
:ref:`tkinter-setting-options` on setting options. ::
303
306
 
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")
307
310
 
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
312
 
module. ::
 
314
actions (methods) that a given widget can perform are listed in
 
315
:file:`tkinter/__init__.py`. ::
313
316
 
314
317
   .fred invoke                =====>  fred.invoke()
315
318
 
320
323
methods. See the :mod:`tkinter.tix` module documentation for additional
321
324
information on the Form geometry manager. ::
322
325
 
323
 
   pack .fred -side left       =====>  fred.pack(side = "left")
 
326
   pack .fred -side left       =====>  fred.pack(side="left")
324
327
 
325
328
 
326
329
How Tk and Tkinter are Related
332
335
   A Python application makes a :mod:`tkinter` call.
333
336
 
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
 
342
   a Python script.
339
343
 
340
 
tkinter (C)
 
344
_tkinter (C)
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.
343
347
 
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
371
375
   ::
372
376
 
373
 
      fred = Button(self, fg = "red", bg = "blue")
 
377
      fred = Button(self, fg="red", bg="blue")
374
378
 
375
379
After object creation, treating the option name like a dictionary index
376
380
   ::
381
385
Use the config() method to update multiple attrs subsequent to object creation
382
386
   ::
383
387
 
384
 
      fred.config(fg = "red", bg = "blue")
 
388
      fred.config(fg="red", bg="blue")
385
389
 
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.
464
468
the main application window is resized.  Here are some examples::
465
469
 
466
470
   fred.pack()                     # defaults to side = "top"
467
 
   fred.pack(side = "left")
468
 
   fred.pack(expand = 1)
 
471
   fred.pack(side="left")
 
472
   fred.pack(expand=1)
469
473
 
470
474
 
471
475
Packer Options
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`.
510
514
 
511
515
There are many useful subclasses of Variable already defined:
512
516
:class:`StringVar`, :class:`IntVar`, :class:`DoubleVar`, and
606
610
   This is any Python function that takes no arguments.  For example::
607
611
 
608
612
      def print_it():
609
 
              print("hi there")
 
613
          print("hi there")
610
614
      fred["command"] = print_it
611
615
 
612
616
color
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.
705
 
::
706
709
 
707
 
   Tk      Tkinter Event Field             Tk      Tkinter Event Field
708
 
   --      -------------------             --      -------------------
709
 
   %f      focus                           %A      char
710
 
   %h      height                          %E      send_event
711
 
   %k      keycode                         %K      keysym
712
 
   %s      state                           %N      keysym_num
713
 
   %t      time                            %T      type
714
 
   %w      width                           %W      widget
715
 
   %x      x                               %X      x_root
716
 
   %y      y                               %Y      y_root
 
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
+----+---------------------+----+---------------------+
717
729
 
718
730
 
719
731
The index Parameter
720
732
^^^^^^^^^^^^^^^^^^^
721
733
 
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.
725
737
 
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;
757
769
 
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;
760
772
 
761
773
   * the string ``"last"`` which refers to the last menu item;