blob: 78c359d116178a0c33ab6f4c9112802f6be76cf3 [file] [log] [blame]
Fred Drake7cf4e5b2001-11-15 17:22:04 +00001\chapter{Graphical User Interface Modules \label{gui}}
2
3\index{GUI}
4\index{Graphical User Interface}
5\index{Tkinter}
6\index{Tk}
7
8Tk/Tcl has long been an integral part of Python. It provides a robust
9and platform independent windowing toolkit, that is available to
10Python programmers using the \refmodule{Tkinter} module, and its
11extension, the \refmodule{Tix} module.
12
13\refmodule{Tkinter} is a thin object--oriented layer on top of
14Tcl/Tk. To use \refmodule{Tkinter}, you don't need to write Tcl code,
15but you will need to consult the Tk documentation, and occasionally
16the Tcl documentation. \refmodule{Tkinter} is a set of wrappers that
17implement the Tk widgets as Python classes. In addition, the internal
18module \module{\_tkinter} provides a threadsafe mechanism which allows
19Python and Tcl to interact.
20
21\refmodule{Tkinter} is not the only GUI for Python, but is however the
22most commonly used one; see section~\ref{other-gui-modules},
23``Other User Interface Modules and Packages'' for more information on
24other GUI toolkits for Python.
25
26% Other sections I have in mind are
27% Tkinter internals
28% Freezing Tkinter applications
29
30\localmoduletable
31
32
33\section{Tkinter \label{tkinter}}
34\index{Tkinter}
35\sectionauthor{Fredrik Lundh}{fredrik@effbot.org}
36
37\index{Tkinter}
38
39\declaremodule{standard}{Tkinter}
40\modulesynopsis{Interface to Tcl/Tk for graphical user interfaces}
41\moduleauthor{Guido van Rossum}{guido@Python.org}
42
43The \module{Tkinter} module (``Tk interface'') is the standard Python
44interface to the Tk GUI toolkit, now maintained at ActiveState. Both
45Tk and \module{Tkinter} are available on most Unix platforms, as well
46as on Windows and Macintosh systems.
47
48\begin{seealso}
49\seetitle[http://www.python.org/topics/tkinter/]
50 {Python Tkinter Resources}
51 {The Python Tkinter Topic Guide provides a great
52 deal of information on using Tk from Python and links to
53 other sources of information on Tk.}
54
55\seetitle[http://www.pythonware.com/library/an-introduction-to-tkinter.htm]
56 {An Introduction to Tkinter}
57 {Fredrik Lundh's on-line reference material.}
58
59\seetitle[http://www.nmt.edu/tcc/help/pubs/lang.html]
60 {Tkinter reference: a GUI for Python}
61 {On-line reference material.}
62
63\seetitle[http://jtkinter.sourceforge.net]
64 {Tkinter for JPython}
65 {The Jython interface to Tkinter.}
66
67\seetitle[http://www.amazon.com/exec/obidos/ASIN/1884777813]
68 {Python and Tkinter Programming}
69 {The book by John Grayson (ISBN 1-884777-81-3).}
70\end{seealso}
71
72
73\subsection{Tkinter Modules}
74
75\refmodule{Tkinter} consists of a number of modules. The Tk interface
76is located in a binary module named \module{_tkinter}. This module
77contains the low-level interface to Tk, and should never be used
78directly by application programmers. It is usually a shared library
79(or DLL), but might in some cases be statically linked with the Python
80interpreter.
81
82In addition to the Tk interface module, \refmodule{Tkinter} includes a
83number of Python modules. The two most important modules are the
84\refmodule{Tkinter} module itself, and a module called
85\module{Tkconstants}. The former automatically imports the latter, so
86to use Tkinter, all you need to do is to import one module:
87
88\begin{verbatim}
89import Tkinter
90\end{verbatim}
91
92Or, more often:
93
94\begin{verbatim}
95from Tkinter import *
96\end{verbatim}
97
98\begin{classdesc}{Tk}{screenName=None, baseName=None, className='Tk'}
99The \class{Tk} class is instantiated without arguments.
100This creates a toplevel widget of Tk which usually is the main window
101of an appliation. Each instance has its own associated Tcl interpreter.
102% FIXME: The following keyword arguments are currently recognized:
103\end{classdesc}
104
105Other modules that provide Tk support include:
106
107\begin{description}
108% \declaremodule{standard}{Tkconstants}
109% \modulesynopsis{Constants used by Tkinter}
110% FIXME
111
112\item[\module{Tkdnd}]
113Drag-and-drop support for \refmodule{Tkinter}.
114This is experimental and should become deprecated when it is replaced
115with the Tk DND.
116
117\item[\refmodule{turtle}]
118Turtle graphics in a Tk window.
119
120\end{description}
121
122\subsection{Tkinter Life Preserver}
123\index{Tkinter}
124
125This section is not designed to be an exhaustive tutorial on either
126Tk or Tkinter. Rather, it is intended as a stop gap, providing some
127introductory orientation on the system.
128
129Credits:
130\begin{itemize}
131\item Tkinter was written by Steen Lumholt and Guido van Rossum.
132\item Tk was written by John Ousterhout while at Berkeley.
133\item This Life Preserver was written by Matt Conway at
134the University of Virginia.
135\item The html rendering, and some liberal editing, was
136produced from a FrameMaker version by Ken Manheimer.
137\item Fredrik Lundh elaborated and revised the class interface descriptions,
138to get them current with Tk 4.2.
139\item Mike Clarkson converted the documentation to \LaTeX, and compiled the
140User Interface chapter of the reference manual.
141\end{itemize}
142
143
144\subsubsection{How To Use This Section}
145
146This section is designed in two parts: the first half (roughly) covers
147background material, while the second half can be taken to the
148keyboard as a handy reference.
149
150When trying to answer questions of the form ``how do I do blah'', it
151is often best to find out how to do``blah'' in straight Tk, and then
152convert this back into the corresponding \refmodule{Tkinter} call.
153Python programmers can often guess at the correct Python command by
154looking at the Tk documentation. This means that in order to use
155Tkinter, you will have to know a little bit about Tk. This document
156can't fulfill that role, so the best we can do is point you to the
157best documentation that exists. Here are some hints:
158
159\begin{itemize}
160\item The authors strongly suggest getting a copy of the Tk man
161pages. Specifically, the man pages in the \code{mann} directory are most
162useful. The \code{man3} man pages describe the C interface to the Tk
163library and thus are not especially helpful for script writers.
164
165\item Addison-Wesley publishes a book called \citetitle{Tcl and the
166Tk Toolkit} by John Ousterhout (ISBN 0-201-63337-X) which is a good
167introduction to Tcl and Tk for the novice. The book is not
168exhaustive, and for many details it defers to the man pages.
169
170\item \file{Tkinter.py} is a last resort for most, but can be a good
171place to go when nothing else makes sense.
172\end{itemize}
173
174\begin{seealso}
175\seetitle[http://tcl.activestate.com/]
176 {ActiveState Tcl Home Page}
177 {The Tk/Tcl development is largely taking place at
178 ActiveState.}
179\seetitle[http://www.amazon.com/exec/obidos/ASIN/020163337X]
180 {Tcl and the Tk Toolkit}
181 {The book by John Ousterhout, the inventor of Tcl .}
182\seetitle[http://www.amazon.com/exec/obidos/ASIN/0130220280]
183 {Practical Programming in Tcl and Tk}
184 {Brent Welch's encyclopedic book.}
185\end{seealso}
186
187
188\subsubsection{A Simple Hello World Program} % HelloWorld.html
189
190%begin{latexonly}
191%\begin{figure}[hbtp]
192%\centerline{\epsfig{file=HelloWorld.gif,width=.9\textwidth}}
193%\vspace{.5cm}
194%\caption{HelloWorld gadget image}
195%\end{figure}
196%See also the hello-world \ulink{notes}{classes/HelloWorld-notes.html} and
197%\ulink{summary}{classes/HelloWorld-summary.html}.
198%end{latexonly}
199
200
201\begin{verbatim}
202from Tkinter import * 1
203 2
204class Application(Frame): 3
205 def say_hi(self): 4
206 print "hi there, everyone!" 5
207 6
208 def createWidgets(self): 7
209 self.QUIT = Button(self) 8
210 self.QUIT["text"] = "QUIT" 9
211 self.QUIT["fg"] = "red" 10
212 self.QUIT["command"] = self.quit 11
213 12
214 self.QUIT.pack({"side": "left"}) 13
215 14
216 self.hi_there = Button(self) 15
217 self.hi_there["text"] = "Hello", 16
218 self.hi_there["command"] = self.say_hi 17
219 18
220 self.hi_there.pack({"side": "left"}) 19
221 20
222 21
223 def __init__(self, master=None): 22
224 Frame.__init__(self, master) 23
225 self.pack() 24
226 self.createWidgets() 25
227 26
228app = Application() 27
229app.mainloop() 28
230\end{verbatim}
231
232\ifhtml
233\subsection{An Overview of The Tkinter Classes} % TkClassHier.html
234
235%begin{latexonly}
236%\begin{figure}[hbtp]
237%\centerline{\epsfig{file=TkClassHier.gif,width=.9\textwidth}}
238%\caption{Class Hierarchy Image}
239%\end{figure}
240%end{latexonly}
241
242The class hierarchy looks complicated, but in actual practice,
243application programmers almost always refer to the classes at the very
244bottom of the hierarchy.
245
246Here are links to the interfaces for each of the concrete widgets:
247
248\begin{itemize}
249\item \citetitle[classes/ClassButton.html]{Button}
250\item \citetitle[classes/ClassCanvas.html]{Canvas}
251\item \citetitle[classes/ClassCheckbutton.html]{Checkbutton}
252\item \citetitle[classes/ClassEntry.html]{Entry}
253\item \citetitle[classes/ClassFrame.html]{Frame}
254\item \citetitle[classes/ClassLabel.html]{Label}
255\item \citetitle[classes/ClassListbox.html]{Listbox}
256\item \citetitle[classes/ClassMenu.html]{Menu}
257\item \citetitle[classes/ClassMenubutton.html]{Menubutton}
258\item \citetitle[classes/ClassMessage.html]{Message}
259\item \citetitle[classes/ClassMisc.html]{*Misc}
260\item \citetitle[classes/ClassPacker.html]{*Pack}
261\item \citetitle[classes/ClassPlacer.html]{*Place}
262\item \citetitle[classes/ClassRadiobutton.html]{Radiobutton}
263\item \citetitle[classes/ClassScale.html]{Scale}
264\item \citetitle[classes/ClassScrollbar.html]{Scrollbar}
265\item \citetitle[classes/ClassText.html]{Text}
266\item \citetitle[classes/ClassTk.html]{**Tk}
267\item \citetitle[classes/ClassToplevel.html]{Toplevel}
268\item \citetitle[classes/ClassWidget.html]{***Widget}
269\item \citetitle[classes/ClassWm.html]{*Wm}
270\end{itemize}
271
272
273Notes:
274\begin{itemize}
275\item These classes are provided for the purposes of
276organizing certain functions under one namespace. They aren't meant to
277be instantiated independently.
278\item The Tk class is meant to be instantiated only once in
279an application. Application programmers need not instantiate one
280explicitly, the system creates one whenever any of the other classes
281are instantiated.
282\item The Widget class is not meant to be instantiated, it
283is meant only for subclassing to make ``real'' widgets. (in C++, this
284is called an `abstract class')
285\end{itemize}
286\fi
287
288
289\subsection{A (Very) Quick Look at Tcl/Tk} % BriefTclTk.html
290
291To make use of this reference material, there will be times when you
292will need to know how to read short passages of Tk and how to identify
293the various parts of a Tk command.
294(See \ref{tkinter-basic-mapping} for the
295\refmodule{Tkinter} equivalents of what's below.)
296
297Tk scripts are Tcl programs. Like all Tcl programs, Tk scripts are
298just lists of tokens separated by spaces. A Tk widget is just its
299\emph{class}, the \emph{options} that help configure it, and the
300\emph{actions} that make it do useful things.
301
302To make a widget in Tk, the command is always of the form:
303
304\begin{verbatim}
305 classCommand newPathname options
306\end{verbatim}
307
308\begin{description}
309\item[\var{classCommand}]
310denotes which kind of widget to make (a button, a label, a menu...)
311
312\item[\var{newPathname}]
313is the new name for this widget. All names in Tk must be unique. To
314help enforce this, widgets in Tk are named with \emph{pathnames}, just
315like files in a file system. The top level widget, the \emph{root},
316is called \code{.} (period) and children are delimited by more
317periods. For example, \code{.myApp.controlPanel.okButton} might be
318the name of a widget.
319
320\item[\var{options} ]
321configure the widget's appearance and in some cases, its
322behavior. The options come in the form of a list of flags and values.
323Flags are proceeded by a `-', like unix shell command flags, and
324values are put in quotes if they are more than one word.
325\end{description}
326
327For example:
328
329\begin{verbatim}
330 button .fred -fg red -text "hi there"
331 ^ ^ \_____________________/
332 | | |
333 class new options
334 command widget (-opt val -opt val ...)
335\end{verbatim}
336
337Once created, the pathname to the widget becomes a new command. This
338new \var{widget command} is the programmer's handle for getting the new
339widget to perform some \var{action}. In C, you'd express this as
340someAction(fred, someOptions), in C++, you would express this as
341fred.someAction(someOptions), and in Tk, you say:
342
343\begin{verbatim}
344 .fred someAction someOptions
345\end{verbatim}
346
347Note that the object name, \code{.fred}, starts with a dot.
348
349As you'd expect, the legal values for \var{someAction} will depend on
350the widget's class: \code{.fred disable} works if fred is a
351button (fred gets greyed out), but does not work if fred is a label
352(disabling of labels is not supported in Tk).
353
354The legal values of \var{someOptions} is action dependent. Some
355actions, like \code{disable}, require no arguments, others, like
356a text-entry box's \code{delete} command, would need arguments
357to specify what range of text to delete.
358
359
360\subsection{Mapping Basic Tk into Tkinter
361 \label{tkinter-basic-mapping}}
362
363Class commands in Tk correspond to class constructors in Tkinter.
364
365\begin{verbatim}
366 button .fred =====> fred = Button()
367\end{verbatim}
368
369The master of an object is implicit in the new name given to it at
370creation time. In Tkinter, masters are specified explicitly.
371
372\begin{verbatim}
373 button .panel.fred =====> fred = Button(panel)
374\end{verbatim}
375
376The configuration options in Tk are given in lists of hyphened tags
377followed by values. In Tkinter, options are specified as
378keyword-arguments in the instance constructor, and keyword-args for
379configure calls or as instance indices, in dictionary style, for
380established instances. See \ref{tkinter-setting-options}
381on setting options.
382
383\begin{verbatim}
384 button .fred -fg red =====> fred = Button(panel, fg = "red")
385 .fred configure -fg red =====> fred["fg"] = red
386 OR ==> fred.config(fg = "red")
387\end{verbatim}
388
389In Tk, to perform an action on a widget, use the widget name as a
390command, and follow it with an action name, possibly with arguments
391(options). In Tkinter, you call methods on the class instance to
392invoke actions on the widget. The actions (methods) that a given
393widget can perform are listed in the Tkinter.py module.
394
395\begin{verbatim}
396 .fred invoke =====> fred.invoke()
397\end{verbatim}
398
399To give a widget to the packer (geometry manager), you call pack with
400optional arguments. In Tkinter, the Pack class holds all this
401functionality, and the various forms of the pack command are
402implemented as methods. All widgets in \refmodule{Tkinter} are
403subclassed from the Packer, and so inherit all the packing
404methods. See the \refmodule{Tix} module documentation for additional
405information on the Form geometry manager.
406
407\begin{verbatim}
408 pack .fred -side left =====> fred.pack(side = "left")
409\end{verbatim}
410
411
412\subsection{How Tk and Tkinter are Related} % Relationship.html
413
414From the top down:
415\begin{description}
416\item[\b{Your App Here (Python)}]
417A Python application makes a \refmodule{Tkinter} call.
418
419\item[\b{Tkinter (Python Module)}]
420This call (say, for example, creating a button widget), is
421implemented in the \emph{Tkinter} module, which is written in
422Python. This Python function will parse the commands and the
423arguments and convert them into a form that makes them look as if they
424had come from a Tk script instead of a Python script.
425
426\item[\b{tkinter (C)}]
427These commands and their arguments will be passed to a C function
428in the \emph{tkinter} - note the lowercase - extension module.
429
430\item[\b{Tk Widgets} (C and Tcl)]
431This C function is able to make calls into other C modules,
432including the C functions that make up the Tk library. Tk is
433implemented in C and some Tcl. The Tcl part of the Tk widgets is used
434to bind certain default behaviors to widgets, and is executed once at
435the point where the Python \refmodule{Tkinter} module is
436imported. (The user never sees this stage).
437
438\item[\b{Tk (C)}]
439The Tk part of the Tk Widgets implement the final mapping to ...
440
441\item[\b{Xlib (C)}]
442the Xlib library to draw graphics on the screen.
443\end{description}
444
445
446\subsection{Handy Reference}
447
448\subsubsection{Setting Options
449 \label{tkinter-setting-options}}
450
451Options control things like the color and border width of a widget.
452Options can be set in three ways:
453
454\begin{description}
455\item[At object creation time, using keyword arguments]:
456\begin{verbatim}
457fred = Button(self, fg = "red", bg = "blue")
458\end{verbatim}
459\item[After object creation, treating the option name like a dictionary index]:
460\begin{verbatim}
461fred["fg"] = "red"
462fred["bg"] = "blue"
463\end{verbatim}
464\item[Use the config() method to update multiple attrs subesequent to
465object creation]:
466\begin{verbatim}
467fred.config(fg = "red", bg = "blue")
468\end{verbatim}
469\end{description}
470
471For a complete explanation of a given option and its behavior, see the
472Tk man pages for the widget in question.
473
474Note that the man pages list "STANDARD OPTIONS" and "WIDGET SPECIFIC
475OPTIONS" for each widget. The former is a list of options that are
476common to many widgets, the latter are the options that are
477ideosyncratic to that particular widget. The Standard Options are
478documented on the \manpage{options}{3} man page.
479
480No distinction between standard and widget-specific options is made in
481this document. Some options don't apply to some kinds of widgets.
482Whether a given widget responds to a particular option depends on the
483class of the widget; buttons have a \code{command} option, labels do not.
484
485The options supported by a given widget are listed in that widget's
486man page, or can be queried at runtime by calling the
487\kbd{config()} method with arguments, or by calling the keys()
488method on that widget. The return value of these calls is a dictionary
489whose key is the name of the option (e.g. \kbd{relief}) and whose
490values are 5 tuples.
491
492(Some options, like \kbd{bg} are synonyms for common options with
493hard-to-type names (\kbd{bg} is shorthand for "background").
494Passing the \kbd{config()} method the name of a
495shorthand option will return a 2-tuple, not 5-tuple. The 2-tuple
496passed back will contain the name of the synonym ``real''
497option. (\kbd{bg}, \kbd{background}))
498
499\begin{tableiii}{c|l|l}{textrm}{Index}{Meaning}{Example}
500 \lineiii{0}{option name} {\code{'relief'}}
501 \lineiii{1}{option name for database lookup} {\code{'relief'}}
502 \lineiii{2}{option class for database lookup} {\code{'Relief'}}
503 \lineiii{3}{default value} {\code{'raised'}}
504 \lineiii{4}{current value} {\code{'groove'}}
505\end{tableiii}
506
507
508Example:
509
510\begin{verbatim}
511>>> print fred.config()
512{'relief' : ('relief', 'relief', 'Relief', 'raised', 'groove')}
513\end{verbatim}
514
515Of course, the dictionary printed will include all the options
516available and their values. This is meant only as an example.
517
518
519\subsubsection{The Packer} % Packer.html
520\index{packing (widgets)}
521
522The packer is one of Tk's geometry-management mechanisms. See also
523\citetitle[classes/ClassPacker.html]{the Packer class interface}.
524
525Geometry managers are used to specify the relative positioning of the
526positioning of widgets within their container - their mutual
527\emph{master}. In contrast to the more cumbersome \emph{placer}
528(which is used less commonly, and we do not cover here), the packer
529takes qualitative relationship specification - \emph{above}, \emph{to
530the left of}, \emph{filling}, etc - and works everything out to
531determine the exact placement coordinates for you.
532
533The size of any \emph{master} widget is determined by the size of
534the "slave widgets" inside. The packer is used to control where slave
535widgets appear inside the master into which they are packed. You can
536pack widgets into frames, and frames into other frames, in order to
537achieve the kind of layout you desire. Additionally, the arrangement
538is dynamically adjusted to accomodate incremental changes to the
539configuration, once it is packed.
540
541Note that widgets do not appear until they have had their geometry
542specified with a geometry manager. It's a common early mistake to
543leave out the geometry specification, and then be surprised when the
544widget is created but nothing appears. A widget will appear only
545after it has had, for example, the packer's \method{pack()} method
546applied to it.
547
548The pack() method can be called with keyword-option/value pairs that
549control where the widget is to appear within its container, and how it
550is to behave when the main application window is resized. Here are
551some examples:
552
553\begin{verbatim}
554 fred.pack() # defaults to side = "top"
555 fred.pack(side = "left")
556 fred.pack(expand = 1)
557\end{verbatim}
558
559
560\subsubsection{Packer Options}
561
562For more extensive information on the packer and the options that it
563can take, see the man pages and page 183 of John Ousterhout's book.
564
565\begin{description}
566\item[\b{anchor }]
567Anchor type. Denotes where the packer is to place each slave in its
568parcel.
569
570\item[\b{expand}]
571Boolean, \code{0} or \code{1}.
572
573\item[\b{fill}]
574Legal values: \code{'x'}, \code{'y'}, \code{'both'}, \code{'none'}.
575
576\item[\b{ipadx} and \b{ipady}]
577A distance - designating internal padding on each side of the slave
578widget.
579
580\item[\b{padx} and \b{pady}]
581A distance - designating external padding on each side of the slave
582widget.
583
584\item[\b{side}]
585Legal values are: \code{'left'}, \code{'right'}, \code{'top'},
586\code{'bottom'}.
587\end{description}
588
589
590\subsubsection{Coupling Widget Variables} % VarCouplings.html
591
592The current-value setting of some widgets (like text entry widgets)
593can be connected directly to application variables by using special
594options. These options are \code{variable}, \code{textvariable},
595\code{onvalue}, \code{offvalue}, and \code{value}. This
596connection works both ways: if the variable changes for any reason,
597the widget it's connected to will be updated to reflect the new value.
598
599Unfortunately, in the current implementation of \refmodule{Tkinter} it is
600not possible to hand over an arbitrary Python variable to a widget
601through a \code{variable} or \code{textvariable} option. The only
602kinds of variables for which this works are variables that are
603subclassed from a class called Variable, defined in the
604\refmodule{Tkinter} module.
605
606There are many useful subclasses of Variable already defined:
607\class{StringVar}, \class{IntVar}, \class{DoubleVar}, and
608\class{BooleanVar}. To read the current value of such a variable,
609call the \method{get()} method on
610it, and to change its value you call the \method{set()} method. If
611you follow this protocol, the widget will always track the value of
612the variable, with no further intervention on your part.
613
614For example:
615\begin{verbatim}
616class App(Frame):
617 def __init__(self, master=None):
618 Frame.__init__(self, master)
619 self.pack()
620
621 self.entrythingy = Entry()
622 self.entrythingy.pack()
623
624 self.button.pack()
625 # here is the application variable
626 self.contents = StringVar()
627 # set it to some value
628 self.contents.set("this is a variable")
629 # tell the entry widget to watch this variable
630 self.entrythingy["textvariable"] = self.contents
631
632 # and here we get a callback when the user hits return.
633 # we will have the program print out the value of the
634 # application variable when the user hits return
635 self.entrythingy.bind('<Key-Return>',
636 self.print_contents)
637
638 def print_contents(self, event):
639 print "hi. contents of entry is now ---->", \
640 self.contents.get()
641\end{verbatim}
642
643
644\subsubsection{The Window Manager} % WindowMgr.html
645\index{window manager (widgets)}
646
647In Tk, there is a utility command, \code{wm}, for interacting with the
648window manager. Options to the \code{wm} command allow you to control
649things like titles, placement, icon bitmaps, and the like. In
650\refmodule{Tkinter}, these commands have been implemented as methods
651on the \class{Wm} class. Toplevel widgets are subclassed from the
652\class{Wm} class, and so can call the \class{Wm} methods directly.
653
654%See also \citetitle[classes/ClassWm.html]{the Wm class interface}.
655
656To get at the toplevel window that contains a given widget, you can
657often just refer to the widget's master. Of course if the widget has
658been packed inside of a frame, the master won't represent a toplevel
659window. To get at the toplevel window that contains an arbitrary
660widget, you can call the \method{_root()} method. This
661method begins with an underscore to denote the fact that this function
662is part of the implementation, and not an interface to Tk functionality.
663
664Here are some examples of typical usage:
665
666\begin{verbatim}
667import Tkinter
668class App(Frame):
669 def __init__(self, master=None):
670 Frame.__init__(self, master)
671 self.pack()
672
673
674# create the application
675myapp = App()
676
677#
678# here are method calls to the window manager class
679#
680myapp.master.title("My Do-Nothing Application")
681myapp.master.maxsize(1000, 400)
682
683# start the program
684myapp.mainloop()
685\end{verbatim}
686
687
688\subsubsection{Tk Option Data Types} % OptionTypes.html
689
690\index{Tk Option Data Types}
691
692\begin{description}
693\item[anchor]
694Legal values are points of the compass: \code{"n"},
695\code{"ne"}, \code{"e"}, \code{"se"}, \code{"s"},
696\code{"sw"}, \code{"w"}, \code{"nw"}, and also
697\code{"center"}.
698
699\item[bitmap]
700There are eight built-in, named bitmaps: \code{'error'}, \code{'gray25'},
701\code{'gray50'}, \code{'hourglass'}, \code{'info'}, \code{'questhead'},
702\code{'question'}, \code{'warning'}. To specify an X bitmap
703filename, give the full path to the file, preceded with an \code{@},
704as in \code{"@/usr/contrib/bitmap/gumby.bit"}.
705
706\item[boolean]
707You can pass integers 0 or 1 or the stings \code{"yes"} or \code{"no"} .
708
709\item[callback]
710This is any Python function that takes no arguments. For example:
711\begin{verbatim}
712 def print_it():
713 print "hi there"
714 fred["command"] = print_it
715\end{verbatim}
716
717\item[color]
718Colors can be given as the names of X colors in the rgb.txt file,
719or as strings representing RGB values in 4 bit: \code{"\#RGB"}, 8
720bit: \code{"\#RRGGBB"}, 12 bit" \code{"\#RRRGGGBBB"}, or 16 bit
721\code{"\#RRRRGGGGBBBB"} ranges, where R,G,B here represent any
722legal hex digit. See page 160 of Ousterhout's book for details.
723
724\item[cursor]
725The standard X cursor names from \file{cursorfont.h} can be used,
726without the \code{XC_} prefix. For example to get a hand cursor
727(\constant{XC_hand2}), use the string \code{"hand2"}. You can also
728specify a bitmap and mask file of your own. See page 179 of
729Ousterhout's book.
730
731\item[distance]
732Screen distances can be specified in either pixels or absolute
733distances. Pixels are given as numbers and absolute distances as
734strings, with the trailing character denoting units: \code{c}
735for centimeters, \code{i} for inches, \code{m} for millimeters,
736\code{p} for printer's points. For example, 3.5 inches is expressed
737as \code{"3.5i"}.
738
739\item[font]
740Tk uses a list font name format, such as \code{\{courier 10 bold\}}.
741Font sizes with positive numbers are measured in points;
742sizes with negative numbers are measured in pixels.
743
744\item[geometry]
745This is a string of the form \samp{\var{width}x\var{height}}, where
746width and height are measured in pixels for most widgets (in
747characters for widgets displaying text). For example:
748\code{fred["geometry"] = "200x100"}.
749
750\item[justify]
751Legal values are the strings: \code{"left"},
752\code{"center"}, \code{"right"}, and \code{"fill"}.
753
754\item[region]
755This is a string with four space-delimited elements, each of
756which is a legal distance (see above). For example: \code{"2 3 4
7575"} and \code{"3i 2i 4.5i 2i"} and \code{"3c 2c 4c 10.43c"}
758are all legal regions.
759
760\item[relief]
761Determines what the border style of a widget will be. Legal
762values are: \code{"raised"}, \code{"sunken"},
763\code{"flat"}, \code{"groove"}, and \code{"ridge"}.
764
765\item[scrollcommand]
766This is almost always the \method{set()} method of some scrollbar
767widget, but can be any widget method that takes a single argument.
768Refer to the file \file{Demo/tkinter/matt/canvas-with-scrollbars.py}
769in the Python source distribution for an example.
770
771\item[wrap:]
772Must be one of: \code{"none"}, \code{"char"}, or \code{"word"}.
773\end{description}
774
775
776\subsubsection{Bindings and Events} % Bindings.html
777
778\index{bind (widgets)}
779\index{events (widgets)}
780
781The bind method from the widget command allows you to watch for
782certain events and to have a callback function trigger when that event
783type occurs. The form of the bind method is:
784
785\begin{verbatim}
786 def bind(self, sequence, func, add=''):
787\end{verbatim}
788where:
789
790\begin{description}
791\item[sequence]
792is a string that denotes the target kind of event. (See the bind
793man page and page 201 of John Ousterhout's book for details).
794
795\item[func]
796is a Python function, taking one argument, to be invoked when the
797event occurs. An Event instance will be passed as the argument.
798(Functions deployed this way are commonly known as \var{callbacks}.)
799
800\item[add]
801is optional, either \samp{} or \samp{+}. Passing an empty string
802denotes that this binding is to replace any other bindings that this
803event is associated with. Preceeding with a \samp{+} means that this
804function is to be added to the list of functions bound to this event type.
805\end{description}
806
807For example:
808\begin{verbatim}
809 def turnRed(self, event):
810 event.widget["activeforeground"] = "red"
811
812 self.button.bind("<Enter>", self.turnRed)
813\end{verbatim}
814
815Notice how the widget field of the event is being accesed in the
816\method{turnRed()} callback. This field contains the widget that
817caught the X event. The following table lists the other event fields
818you can access, and how they are denoted in Tk, which can be useful
819when referring to the Tk man pages.
820
821\begin{verbatim}
822Tk Tkinter Event Field Tk Tkinter Event Field
823-- ------------------- -- -------------------
824%f focus %A char
825%h height %E send_event
826%k keycode %K keysym
827%s state %N keysym_num
828%t time %T type
829%w width %W widget
830%x x %X x_root
831%y y %Y y_root
832\end{verbatim}
833
834
835\subsubsection{The index Parameter} % Index.html
836
837A number of widgets require``index'' parameters to be passed. These
838are used to point at a specific place in a Text widget, or to
839particular characters in an Entry widget, or to particular menu items
840in a Menu widget.
841
842\begin{description}
843\item[\b{Entry widget indexes (index, view index, etc.)}]
844Entry widgets have options that refer to character positions in the
845text being displayed. You can use these \refmodule{Tkinter} functions
846to access these special points in text widgets:
847\begin{description}
848\item[AtEnd()]
849refers to the last position in the text
850\item[AtInsert()]
851refers to the point where the text cursor is
852\item[AtSelFirst()]
853indicates the beginning point of the selected text
854\item[AtSelLast()]
855denotes the last point of the selected text and finally
856\item[At(x, y=None)]
857refers to the character at pixel location x, y (with y not used
858in the case of a text entry widget, which is one line of text).
859\end{description}
860
861\item[\b{Text widget indexes}]
862The index notation for Text widgets is very rich and is best described
863in the Tk man pages.
864
865\item[\b{Menu indexes (menu.invoke(), menu.entryconfig(), etc.)}]
866
867Some options and methods for menus manipulate specific menu entries.
868Anytime a menu index is needed for an option or a parameter, you may
869pass in:
870\begin{itemize}
871\item an integer which refers to the numeric position of the entry in
872the widget, counted from the top, starting with 0;
873\item the string \code{'active'}, which refers to the menu position that is
874currently under the cursor;
875\item the string \code{"last"} which refers to the last menu
876item;
877\item An integer preceded by \code{@}, as in \code{@6}, where the integer is
878interpreted as a y pixel coordinate in the menu's coordinate system;
879\item the string \code{"none"}, which indicates no menu entry at all, most
880often used with menu.activate() to deactivate all entries, and
881finally,
882\item a text string that is pattern matched against the label of the
883menu entry, as scanned from the top of the menu to the bottom. Note
884that this index type is considered after all the others, which means
885that matches for menu items labelled \code{last}, \code{active}, or
886\code{none} may be interpreted as the above literals, instead.
887\end{itemize}
888\end{description}
889
890
891\section{Tix \label{tix-widgets}}
892
893\index{Tix}
894
895\declaremodule{standard}{Tix}
896\modulesynopsis{Tk Extension Widgets for Tkinter}
897\sectionauthor{Mike Clarkson}{mikeclarkson@users.sourceforge.net}
898
899The \module{Tix} (Tk Interface Extension) module provides an
900additional rich set of widgets. Although the standard Tk library has
901many useful widgets, they are far from complete. The \module{Tix}
902library provides most of the commonly needed widgets that are missing
903from standard Tk: \class{HList}, \class{ComboBox}, \class{Control}
904(a.k.a. SpinBox) and an assortment of scrollable widgets. \module{Tix}
905also includes many more widgets that are generally useful in a wide
906range of applications: \class{NoteBook}, \class{FileEntry},
907\class{PanedWindow}, etc; there are more than 40 of them.
908
909With all these new widgets, you can introduce new interaction
910techniques into applications, creating more useful and more intuitive
911user interfaces. You can design your application by choosing the most
912appropriate widgets to match the special needs of your application and
913users.
914
915\begin{seealso}
916\seetitle[http://tix.sourceforge.net/]
917 {Tix Homepage}
918 {See the home page for \module{Tix}.}
919\seetitle[http://tix.sourceforge.net/dist/current/man/]
920 {Tix Man Pages}
921 {On-line version of the man pages and reference material.}
922\seetitle[http://tix.sourceforge.net/dist/current/docs/tix-book/tix.book.html]
923 {Tix Programming Guide}
924 {On-line version of the programmer's reference material.}
925\seetitle[http://tix.sourceforge.net/Tide/]
926 {Tix Development Applications}
927 {Tix applications for development of Tix and Tkinter programs.
928 Tide applications work under Tk or Tkinter, and include
929 \program{TixInspect}, an inspector to remotely modify and
930 debug Tix/Tk/Tkinter applications.}
931\end{seealso}
932
933
934\subsection{Using Tix}
935
936\begin{classdesc}{Tix}{screenName=None, baseName=None, className='Tix'}
937 Toplevel widget of Tix which represents mostly the main window
938 of an application. It has an associated Tcl interpreter.
939
940The \refmodule{Tix} interface module subclasses the \refmodule{Tkinter}
941module. The former imports the latter, so to use \refmodule{Tix} with
942Tkinter, all you need to do is to import one module. In general, you
943can just import Tix, and replace the toplevel call
944to \class{Tkinter.Tk} with \class{Tix.Tk}:
945\begin{verbatim}
946import Tix
947from Tkconstants import *
948root = Tix.Tk()
949\end{verbatim}
950\end{classdesc}
951
952To use \refmodule{Tix}, you must have the \refmodule{Tix} widgets installed,
953usually alongside your installation of the Tk widgets.
954To test your installation, try the following:
955\begin{verbatim}
956import Tix
957root = Tix.Tk()
958root.tk.eval('package require Tix')
959\end{verbatim}
960
961If this fails, you have a Tk installation problem which must be
962resolved before proceeding. Use the environment variable \envvar{TIX_LIBRARY}
963to point to the installed \refmodule{Tix} library directory, and
964make sure you have the dynamic object library (\file{tix8183.dll} or
965\file{libtix8183.so}) in the same directory that contains your Tk
966dynamic object library (\file{tk8183.dll} or \file{libtk8183.so}). The
967directory with the dynamic object library should also have a file
968called \file{pkgIndex.tcl} (case sensitive), which contains the line:
969
970\begin{verbatim}
971package ifneeded Tix 8.1 [list load "[file join $dir tix8183.dll]" Tix]
972\end{verbatim} % $ <-- bow to font-lock
973
974
975\subsection{Tix Widgets}
976
977\ulink{Tix}
978{http://tix.sourceforge.net/dist/current/man/html/TixCmd/TixIntro.htm}
979introduces over 40 widget classes to the \refmodule{Tkinter}
980repertoire. There is a demo of all the \refmodule{Tix} widgets in the
981\file{Demo/tix} directory of the standard distribution.
982
983
984% The Python sample code is still being added to Python, hence commented out
985
986
987\subsubsection{Basic Widgets}
988
989\index{Balloon widget}
990\begin{classdesc}{Balloon}{}
991A \ulink{Balloon}
992{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixBalloon.htm} that pops
993up over a widget to provide help. When the user moves the cursor
994inside a widget to which a Balloon widget has been bound, a small
995pop-up window with a descriptive message will be shown on the screen.
996\end{classdesc}
997
998% Python Demo of: \ulink{ Balloon}{http://tix.sourceforge.net/dist/current/demos/samples/Balloon.tcl}
999
1000\index{ButtonBox widget}
1001\begin{classdesc}{ButtonBox}{}
1002The \ulink{ButtonBox}
1003{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixButtonBox.htm} widget
1004creates a box of buttons, such as is commonly used for \code{Ok Cancel}.
1005\end{classdesc}
1006
1007% Python Demo of: \ulink{ ButtonBox}{http://tix.sourceforge.net/dist/current/demos/samples/BtnBox.tcl}
1008
1009\index{ComboBox widget}
1010\begin{classdesc}{ComboBox}{}
1011The \ulink{ComboBox}
1012{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixComboBox.htm} widget is
1013similar to the combo box control in MS Windows. The user can select a
1014choice by either typing in the entry subwdget or selecting from the
1015listbox subwidget.
1016\end{classdesc}
1017
1018% Python Demo of: \ulink{ ComboBox}{http://tix.sourceforge.net/dist/current/demos/samples/ComboBox.tcl}
1019
1020\index{Control widget}
1021\begin{classdesc}{Control}{}
1022The \ulink{Control}
1023{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixControl.htm} widget is
1024also known as the \class{SpinBox} widget. The user can adjust the value
1025by pressing the two arrow buttons or by entering the value directly
1026into the entry. The new value will be checked against the user-defined
1027upper and lower limits.
1028\end{classdesc}
1029
1030% Python Demo of: \ulink{ Control}{http://tix.sourceforge.net/dist/current/demos/samples/Control.tcl}
1031
1032\index{LabelEntry widget}
1033\begin{classdesc}{LabelEntry}{}
1034The \ulink{LabelEntry}
1035{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixLabelEntry.htm} widget
1036packages an entry widget and a label into one mega widget. It can be
1037used be used to simplify the creation of ``entry-form'' type of interface.
1038\end{classdesc}
1039
1040% Python Demo of:
1041% \ulink{LabelEntry}{http://tix.sourceforge.net/dist/current/demos/samples/LabEntry.tcl}
1042
1043\index{LabelFrame widget}
1044\begin{classdesc}{LabelFrame}{}
1045The \ulink{LabelFrame}
1046{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixLabelFrame.htm} widget
1047packages a frame widget and a label into one mega widget. To create
1048widgets inside a LabelFrame widget, one creates the new widgets
1049relative to the \member{frame} subwidget and manage them inside the
1050\member{frame} subwidget.
1051\end{classdesc}
1052
1053% Python Demo of:
1054% \ulink{LabelFrame}{http://tix.sourceforge.net/dist/current/demos/samples/LabFrame.tcl}
1055
1056\index{Meter widget}
1057\begin{classdesc}{Meter}{}
1058The \ulink{Meter}
1059{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixMeter.htm} widget can
1060be used to show the progress of a background job which may take a long
1061time to execute.
1062\end{classdesc}
1063
1064% Python Demo of:
1065% \ulink{Meter}{http://tix.sourceforge.net/dist/current/demos/samples/Meter.tcl}
1066
1067\index{OptionMenu widget}
1068\begin{classdesc}{OptionMenu}{}
1069The \ulink{OptionMenu}
1070{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixOptionMenu.htm} creates a
1071menu button of options.
1072\end{classdesc}
1073
1074% Python Demo of: \ulink{ OptionMenu}{http://tix.sourceforge.net/dist/current/demos/samples/OptMenu.tcl}
1075
1076\index{PopupMenu widget}
1077\begin{classdesc}{PopupMenu}{}
1078The \ulink{PopupMenu}
1079{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixPopupMenu.htm} widget can
1080be used as a replacement of the \code{tk_popup} command. The advantage
1081of the \refmodule{Tix} PopupMenu widget is it requires less application
1082code to manipulate.
1083\end{classdesc}
1084
1085% Python Demo of: \ulink{ PopupMenu}{http://tix.sourceforge.net/dist/current/demos/samples/PopMenu.tcl}
1086
1087\index{Select widget}
1088\begin{classdesc}{Select}{}
1089The \ulink{Select}
1090{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixSelect.htm} widget is a
1091container of button subwidgets. It can be used to provide radio-box or
1092check-box style of selection options for the user.
1093\end{classdesc}
1094
1095% Python Demo of: \ulink{ Select}{http://tix.sourceforge.net/dist/current/demos/samples/Select.tcl}
1096
1097\index{StdButtonBox widget}
1098\begin{classdesc}{StdButtonBox}{}
1099The \ulink{StdButtonBox}
1100{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixStdButtonBox.htm} widget is a
1101group of Standard buttons for Motif-like dialog boxes.
1102\end{classdesc}
1103
1104% Python Demo of: \ulink{ StdButtonBox}{http://tix.sourceforge.net/dist/current/demos/samples/StdBBox.tcl}
1105
1106
1107\subsubsection{File Selectors}
1108
1109\index{DirList widget}
1110\begin{classdesc}{DirList}{}
1111The \ulink{DirList}
1112{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixDirList.htm} widget
1113displays a list view of a directory, its previous directories and its
1114sub-directories. The user can choose one of the directories displayed
1115in the list or change to another directory.
1116\end{classdesc}
1117
1118% Python Demo of: \ulink{ DirList}{http://tix.sourceforge.net/dist/current/demos/samples/DirList.tcl}
1119
1120\index{DirTree widget}
1121\begin{classdesc}{DirTree}{}
1122The \ulink{DirTree}
1123{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixDirTree.htm} widget
1124displays a tree view of a directory, its previous directories and its
1125sub-directories. The user can choose one of the directories displayed
1126in the list or change to another directory.
1127\end{classdesc}
1128
1129% Python Demo of: \ulink{ DirTree}{http://tix.sourceforge.net/dist/current/demos/samples/DirTree.tcl}
1130
1131\index{DirSelectDialog widget}
1132\begin{classdesc}{DirSelectDialog}{}
1133The \ulink{DirSelectDialog}
1134{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixDirSelectDialog.htm} widget
1135presents the directories in the file system in a dialog window. The
1136user can use this dialog window to navigate through the file system to
1137select the desired directory.
1138\end{classdesc}
1139
1140% Python Demo of: \ulink{ DirSelectDialog}{http://tix.sourceforge.net/dist/current/demos/samples/DirDlg.tcl}
1141
1142\index{DirSelectBox widget}
1143\begin{classdesc}{DirSelectBox}{}
1144The \class{DirSelectBox} is similar
1145to the standard Motif(TM) directory-selection box. It is generally used for
1146the user to choose a directory. DirSelectBox stores the directories mostly
1147recently selected into a ComboBox widget so that they can be quickly
1148selected again.
1149\end{classdesc}
1150
1151\index{ExFileSelectBox widget}
1152\begin{classdesc}{ExFileSelectBox}{}
1153The \ulink{ExFileSelectBox}
1154{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixExFileSelectBox.htm} widget is
1155usually embedded in a tixExFileSelectDialog widget. It provides an
1156convenient method for the user to select files. The style of the
1157ExFileSelectBox widget is very similar to the standard file dialog in
1158MS Windows 3.1.
1159\end{classdesc}
1160
1161% Python Demo of: \ulink{ ExFileSelectDialog}{http://tix.sourceforge.net/dist/current/demos/samples/EFileDlg.tcl}
1162
1163\index{FileSelectBox widget}
1164\begin{classdesc}{FileSelectBox}{}
1165The \ulink{FileSelectBox}
1166{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixFileSelectBox.htm} is similar
1167to the standard Motif(TM) file-selection box. It is generally used for
1168the user to choose a file. FileSelectBox stores the files mostly
1169recently selected into a ComboBox widget so that they can be quickly
1170selected again.
1171\end{classdesc}
1172
1173% Python Demo of: \ulink{ FileSelectDialog}{http://tix.sourceforge.net/dist/current/demos/samples/FileDlg.tcl}
1174
1175\index{FileEntry widget}
1176\begin{classdesc}{FileEntry}{}
1177The \ulink{FileEntry}
1178{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixFileEntry.htm} widget can
1179be used to input a filename. The user can type in the filename
1180manually. Alternatively, the user can press the button widget that
1181sits next to the entry, which will bring up a file selection dialog.
1182\end{classdesc}
1183
1184% Python Demo of: \ulink{ FileEntry}{http://tix.sourceforge.net/dist/current/demos/samples/FileEnt.tcl}
1185
1186
1187\subsubsection{Hierachical ListBox}
1188
1189\index{HList widget}
1190\begin{classdesc}{HList}{}
1191The \ulink{HList}
1192{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixHList.htm} widget can be
1193used to display any data that have a hierarchical structure, for
1194example, file system directory trees. The list entries are indented
1195and connected by branch lines according to their places in the hierachy.
1196\end{classdesc}
1197
1198% Python Demo of: \ulink{ HList}{http://tix.sourceforge.net/dist/current/demos/samples/HList1.tcl}
1199
1200\index{CheckList widget}
1201\begin{classdesc}{CheckList}{}
1202The \ulink{CheckList}
1203{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixCheckList.htm} widget
1204displays a list of items to be selected by the user. CheckList acts
1205similarly to the Tk checkbutton or radiobutton widgets, except it is
1206capable of handling many more items than checkbuttons or radiobuttons.
1207\end{classdesc}
1208
1209% Python Demo of: \ulink{ CheckList}{http://tix.sourceforge.net/dist/current/demos/samples/ChkList.tcl}
1210% Python Demo of: \ulink{ScrolledHList (1)}{http://tix.sourceforge.net/dist/current/demos/samples/SHList.tcl}
1211% Python Demo of: \ulink{ScrolledHList (2)}{http://tix.sourceforge.net/dist/current/demos/samples/SHList2.tcl}
1212
1213
1214\index{Tree widget}
1215\begin{classdesc}{Tree}{}
1216The \ulink{Tree}
1217{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixTree.htm} widget can be
1218used to display hierachical data in a tree form. The user can adjust
1219the view of the tree by opening or closing parts of the tree.
1220\end{classdesc}
1221
1222% Python Demo of: \ulink{ Tree}{http://tix.sourceforge.net/dist/current/demos/samples/Tree.tcl}
1223
1224% Python Demo of: \ulink{Tree (Dynamic)}{http://tix.sourceforge.net/dist/current/demos/samples/DynTree.tcl}
1225
1226
1227\subsubsection{Tabular ListBox}
1228
1229\index{TList widget}
1230\begin{classdesc}{TList}{}
1231The \ulink{TList}
1232{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixTList.htm} widget can be
1233used to display data in a tabular format. The list entries of a TList
1234widget are similar to the entries in the Tk listbox widget. The main
1235differences are (1) the TList widget can display the list entries in a
1236two dimensional format and (2) you can use graphical images as well as
1237multiple colors and fonts for the list entries.
1238\end{classdesc}
1239
1240% Python Demo of: \ulink{ScrolledTList (1)}{http://tix.sourceforge.net/dist/current/demos/samples/STList1.tcl}
1241% Python Demo of: \ulink{ScrolledTList (2)}{http://tix.sourceforge.net/dist/current/demos/samples/STList2.tcl}
1242
1243% Grid has yet to be added to Python
1244% \subsubsection{Grid Widget}
1245% % Python Demo of: \ulink{Simple Grid}{http://tix.sourceforge.net/dist/current/demos/samples/SGrid0.tcl}
1246% % Python Demo of: \ulink{ScrolledGrid}{http://tix.sourceforge.net/dist/current/demos/samples/SGrid1.tcl}
1247% % Python Demo of: \ulink{Editable Grid}{http://tix.sourceforge.net/dist/current/demos/samples/EditGrid.tcl}
1248
1249
1250\subsubsection{Manager Widgets}
1251
1252\index{PanedWindow widget}
1253\begin{classdesc}{PanedWindow}{}
1254The \ulink{PanedWindow}
1255{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixPanedWindow.htm} widget
1256allows the user to interactively manipulate the sizes of several
1257panes. The panes can be arranged either vertically or horizontally.The
1258user changes the sizes of the panes by dragging the resize handle
1259between two panes.
1260\end{classdesc}
1261
1262% Python Demo of: \ulink{ PanedWindow}{http://tix.sourceforge.net/dist/current/demos/samples/PanedWin.tcl}
1263
1264\index{ListNoteBook widget}
1265\begin{classdesc}{ListNoteBook}{}
1266The \ulink{ListNoteBook}
1267{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixListNoteBook.htm} widget is
1268very similar to the TixNoteBook widget: it can be used to display many
1269windows in a limited space using a notebook metaphor. The notebook is
1270divided into a stack of pages (windows). At one time only one of these
1271pages can be shown. The user can navigate through these pages by
1272choosing the name of the desired page in the \member{hlist} subwidget.
1273\end{classdesc}
1274
1275% Python Demo of: \ulink{ ListNoteBook}{http://tix.sourceforge.net/dist/current/demos/samples/ListNBK.tcl}
1276
1277
1278\index{NoteBook widget}
1279\begin{classdesc}{NoteBook}{}
1280The \ulink{NoteBook}
1281{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixNoteBook.htm} widget can
1282be used to display many windows in a limited space using a notebook
1283metaphor. The notebook is divided into a stack of pages. At one time
1284only one of these pages can be shown. The user can navigate through
1285these pages by choosing the visual ``tabs'' at the top of the NoteBook widget.
1286\end{classdesc}
1287
1288% Python Demo of: \ulink{ NoteBook}{http://tix.sourceforge.net/dist/current/demos/samples/NoteBook.tcl}
1289
1290
1291% \subsubsection{Scrolled Widgets}
1292% Python Demo of: \ulink{ ScrolledListBox}{http://tix.sourceforge.net/dist/current/demos/samples/SListBox.tcl}
1293% Python Demo of: \ulink{ ScrolledText}{http://tix.sourceforge.net/dist/current/demos/samples/SText.tcl}
1294% Python Demo of: \ulink{ ScrolledWindow}{http://tix.sourceforge.net/dist/current/demos/samples/SWindow.tcl}
1295% Python Demo of: \ulink{Canvas Object View}{http://tix.sourceforge.net/dist/current/demos/samples/CObjView.tcl}
1296
1297
1298\subsubsection{Image Types}
1299
1300The \refmodule{Tix} module adds:
1301\begin{itemize}
1302\item
1303\ulink{pixmap}
1304{http://tix.sourceforge.net/dist/current/man/html/TixCmd/pixmap.htm} capabilities
1305to all \refmodule{Tix} and \refmodule{Tkinter} widgets to create color
1306images from XPM files.
1307
1308% Python Demo of: \ulink{XPM Image In Button}{http://tix.sourceforge.net/dist/current/demos/samples/Xpm.tcl}
1309
1310% Python Demo of: \ulink{XPM Image In Menu}{http://tix.sourceforge.net/dist/current/demos/samples/Xpm1.tcl}
1311
1312\item
1313\ulink{Compound}
1314{http://tix.sourceforge.net/dist/current/man/html/TixCmd/compound.html} image
1315types can be used to create images that consists of multiple
1316horizontal lines; each line is composed of a series of items (texts,
1317bitmaps, images or spaces) arranged from left to right. For example, a
1318compound image can be used to display a bitmap and a text string
1319simutaneously in a Tk \class{Button} widget.
1320
1321% Python Demo of: \ulink{Compound Image In Buttons}{http://tix.sourceforge.net/dist/current/demos/samples/CmpImg.tcl}
1322
1323% Python Demo of: \ulink{Compound Image In NoteBook}{http://tix.sourceforge.net/dist/current/demos/samples/CmpImg2.tcl}
1324
1325% Python Demo of: \ulink{Compound Image Notebook Color Tabs}{http://tix.sourceforge.net/dist/current/demos/samples/CmpImg4.tcl}
1326
1327% Python Demo of: \ulink{Compound Image Icons}{http://tix.sourceforge.net/dist/current/demos/samples/CmpImg3.tcl}
1328\end{itemize}
1329
1330
1331\subsubsection{Miscellaneous Widgets}
1332
1333\index{InputOnly widget}
1334\begin{classdesc}{InputOnly}{}
1335The \ulink{InputOnly}
1336{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixInputOnly.htm} widgets are
1337to accept inputs from the user, which can be done with the \code{bind}
1338command (\UNIX{} only).
1339\end{classdesc}
1340
1341\subsubsection{Form Geometry Manager}
1342
1343In addition, \refmodule{Tix} augments \refmodule{Tkinter} by providing:
1344\index{Form widget class}
1345\begin{classdesc}{Form}{}
1346The \ulink{Form}
1347{http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixForm.htm} geometry
1348manager based on attachment rules for all Tk widgets.
1349\end{classdesc}
1350
1351
1352%begin{latexonly}
1353%\subsection{Tix Class Structure}
1354%
1355%\begin{figure}[hbtp]
1356%\centerline{\epsfig{file=hierarchy.png,width=.9\textwidth}}
1357%\vspace{.5cm}
1358%\caption{The Class Hierarchy of Tix Widgets}
1359%\end{figure}
1360%end{latexonly}
1361
1362
1363\section{Other User Interface Modules and Packages
1364 \label{other-gui-modules}}
1365
1366
1367There are an number of extension widget sets to \refmodule{Tkinter}.
1368
1369\begin{seealso}
1370\seetitle[http://pmw.sourceforge.net/]{Python megawidgets}{is a
1371toolkit for building high-level compound widgets in Python using the
1372\refmodule{Tkinter} module. It consists of a set of base classes and
1373a library of flexible and extensible megawidgets built on this
1374foundation. These megawidgets include notebooks, comboboxes, selection
1375widgets, paned widgets, scrolled widgets, dialog windows, etc. Also,
1376with the Pmw.Blt interface to BLT, the busy, graph, stripchart, tabset
1377and vector commands are be available.
1378
1379The initial ideas for Pmw were taken from the Tk \code{itcl}
1380extensions \code{[incr Tk]} by Michael McLennan and \code{[incr
1381Widgets]} by Mark Ulferts. Several of the megawidgets are direct
1382translations from the itcl to Python. It offers most of the range of
1383widgets that \code{[incr Widgets]} does, and is almost as complete as
1384Tix, lacking however Tix's fast \class{HList} widget for drawing trees.
1385}
1386\seetitle[http://tkinter.effbot.org]{Tkinter3000}{
1387is a Widget Construction Kit that allows you to write new Tkinter
1388widgets in Python using Mixins. It is built on top of Tkinter,
1389and does not offer the extended range of widgets that \refmodule{Tix} does,
1390but does allow a form of building mega-widgets. The project is
1391still in the early stages.
1392}
1393\end{seealso}
1394
1395
1396\refmodule{Tkinter} is not the only GUI for Python, but is however the
1397most commonly used one.
1398
1399\begin{seealso}
1400\seetitle[http://www.wxwindows.org]{wxWindows}{
1401is a GUI toolkit that combines the most attractive attributes of Qt,
1402Tk, Motif, and GTK+ in one powerful and efficient package. It is
1403implemented in C++. wxWindows supports two flavors of Unix
1404implementation: GTK+ and Motif, and under Windows, it has a standard
1405Microsoft Foundation Classes (MFC) appearance, because it uses Win32
1406widgets. There is a Python class wrapper, independent of Tkinter.
1407
1408wxWindows is much richer in widgets than \refmodule{Tkinter}, with its
1409help system, sophisticated HTML and image viewers, and other
1410specialized widgets, extensive documentation, and printing capabilities.
1411}
1412\seetitle[http://www.thekompany.com]{PyKDE}{
1413PyKDE is a SIP wrapped interface to the Qt toolkit.
1414The Qt C++ toolkit lies at the heart of the KDE desktop, and the
1415Qt toolkit allows very tight integration with KDE, and also Windows
1416portability. SIP is a tool for generating bindings for \Cpp{} libraries
1417as Python classes, and is specifically designed for Python.
1418}
1419\seetitle[http://fxpy.sourceforge.net/]{FXPy}{
1420is a Python extension module which provides an interface to the
1421\citetitle[http://www.cfdrc.com/FOX/fox.html]{FOX} GUI.
1422FOX is a C++ based Toolkit for developing Graphical User Interfaces
1423easily and effectively. It offers a wide, and growing, collection of
1424Controls, and provides state of the art facilities such as drag and
1425drop, selection, as well as OpenGL widgets for 3D graphical
1426manipulation. FOX also implements icons, images, and user-convenience
1427features such as status line help, and tooltips.
1428
1429Even though FOX offers a large collection of Controls already, FOX
1430leverages C++ to allow programmers to easily build additional Controls
1431and GUI elements, simply by taking existing controls, and creating a
1432derived class which simply adds or redefines the desired behavior.
1433}
1434\seetitle[http://www.daa.com.au/\~james/pygtk/]{PyGTK}{
1435is a set of bindings for the \ulink{GTK}{http://www.gtk.org/} widget set.
1436It provides an object oriented interface that is slightly higher
1437level than the C one. It automatically does all the type casting and
1438reference counting that you would have to do normally with the C
1439API. There are also \ulink{bindings}{http://www.daa.com.au/\~james/gnome/}
1440to \ulink{GNOME}{http://www.gnome.org}, and a
1441\ulink{tutorial}
1442{http://laguna.fmedic.unam.mx/\~daniel/pygtutorial/pygtutorial/index.html}
1443is available.
1444}
1445\end{seealso}
1446
1447% XXX Reference URLs that compare the different UI packages
1448
1449
1450\section{Idle \label{idle}}
1451
1452%\declaremodule{standard}{idle}
1453%\modulesynopsis{A Python Integrated Developement Environment}
1454\moduleauthor{Guido van Rossum}{guido@Python.org}
1455
1456Idle is the Python IDE built with the \refmodule{Tkinter} GUI toolkit.
1457\index{Idle}
1458\index{Python Editor}
1459\index{Integrated Developement Environment}
1460
1461
1462IDLE has the following features:
1463
1464\begin{itemize}
1465\item coded in 100\% pure Python, using the \refmodule{Tkinter} GUI toolkit
1466
1467\item cross-platform: works on Windows and \UNIX{} (on Mac OS, there are
1468currently problems with Tcl/Tk)
1469
1470\item multi-window text editor with multiple undo, Python colorizing
1471and many other features, e.g. smart indent and call tips
1472
1473\item Python shell window (a.k.a. interactive interpreter)
1474
1475\item debugger (not complete, but you can set breakpoints, view and step)
1476\end{itemize}
1477
1478
1479\subsection{Menus}
1480
1481\subsubsection{File menu}
1482
1483\begin{description}
1484\item[New window] create a new editing window
1485\item[Open...] open an existing file
1486\item[Open module...] open an existing module (searches sys.path)
1487\item[Class browser] show classes and methods in current file
1488\item[Path browser] show sys.path directories, modules, classes and methods
1489\end{description}
1490\index{Class browser}
1491\index{Path browser}
1492
1493\begin{description}
1494\item[Save] save current window to the associated file (unsaved
1495windows have a * before and after the window title)
1496
1497\item[Save As...] save current window to new file, which becomes
1498the associated file
1499\item[Save Copy As...] save current window to different file
1500without changing the associated file
1501\end{description}
1502
1503\begin{description}
1504\item[Close] close current window (asks to save if unsaved)
1505\item[Exit] close all windows and quit IDLE (asks to save if unsaved)
1506\end{description}
1507
1508
1509\subsubsection{Edit menu}
1510
1511\begin{description}
1512\item[Undo] Undo last change to current window (max 1000 changes)
1513\item[Redo] Redo last undone change to current window
1514\end{description}
1515
1516\begin{description}
1517\item[Cut] Copy selection into system-wide clipboard; then delete selection
1518\item[Copy] Copy selection into system-wide clipboard
1519\item[Paste] Insert system-wide clipboard into window
1520\item[Select All] Select the entire contents of the edit buffer
1521\end{description}
1522
1523\begin{description}
1524\item[Find...] Open a search dialog box with many options
1525\item[Find again] Repeat last search
1526\item[Find selection] Search for the string in the selection
1527\item[Find in Files...] Open a search dialog box for searching files
1528\item[Replace...] Open a search-and-replace dialog box
1529\item[Go to line] Ask for a line number and show that line
1530\end{description}
1531
1532\begin{description}
1533\item[Indent region] Shift selected lines right 4 spaces
1534\item[Dedent region] Shift selected lines left 4 spaces
1535\item[Comment out region] Insert \#\# in front of selected lines
1536\item[Uncomment region] Remove leading \# or \#\# from selected lines
1537\item[Tabify region] Turns \emph{leading} stretches of spaces into tabs
1538\item[Untabify region] Turn \emph{all} tabs into the right number of spaces
1539\item[Expand word] Expand the word you have typed to match another
1540 word in the same buffer; repeat to get a different expansion
1541\item[Format Paragraph] Reformat the current blank-line-separated paragraph
1542\end{description}
1543
1544\begin{description}
1545\item[Import module] Import or reload the current module
1546\item[Run script] Execute the current file in the __main__ namespace
1547\end{description}
1548
1549\index{Import module}
1550\index{Run script}
1551
1552
1553\subsubsection{Windows menu}
1554
1555\begin{description}
1556\item[Zoom Height] toggles the window between normal size (24x80)
1557 and maximum height.
1558\end{description}
1559
1560The rest of this menu lists the names of all open windows; select one
1561to bring it to the foreground (deiconifying it if necessary).
1562
1563
1564\subsubsection{Debug menu (in the Python Shell window only)}
1565
1566\begin{description}
1567\item[Go to file/line] look around the insert point for a filename
1568 and linenumber, open the file, and show the line.
1569\item[Open stack viewer] show the stack traceback of the last exception
1570\item[Debugger toggle] Run commands in the shell under the debugger
1571\item[JIT Stack viewer toggle] Open stack viewer on traceback
1572\end{description}
1573
1574\index{stack viewer}
1575\index{debugger}
1576
1577
1578\subsection{Basic editing and navigation}
1579
1580\begin{itemize}
1581\item \kbd{Backspace} deletes to the left; \kbd{Del} deletes to the right
1582\item Arrow keys and \kbd{Page Up}/\kbd{Page Down} to move around
1583\item \kbd{Home}/\kbd{End} go to begin/end of line
1584\item \kbd{C-Home}/\kbd{C-End} go to begin/end of file
1585\item Some \program{Emacs} bindings may also work, including \kbd{C-B},
1586 \kbd{C-P}, \kbd{C-A}, \kbd{C-E}, \kbd{C-D}, \kbd{C-L}
1587\end{itemize}
1588
1589
1590\subsubsection{Automatic indentation}
1591
1592After a block-opening statement, the next line is indented by 4 spaces
1593(in the Python Shell window by one tab). After certain keywords
1594(break, return etc.) the next line is dedented. In leading
1595indentation, \kbd{Backspace} deletes up to 4 spaces if they are there.
1596\kbd{Tab} inserts 1-4 spaces (in the Python Shell window one tab).
1597See also the indent/dedent region commands in the edit menu.
1598
1599
1600\subsubsection{Python Shell window}
1601
1602\begin{itemize}
1603\item \kbd{C-C} interrupts executing command
1604\item \kbd{C-D} sends end-of-file; closes window if typed at
1605a \samp{>>>~} prompt
1606\end{itemize}
1607
1608\begin{itemize}
1609\item Alt-p retrieves previous command matching what you have typed
1610\item Alt-n retrieves next
1611\item \kbd{Return} while on any previous command retrieves that command
1612\item Alt-/ (Expand word) is also useful here
1613\end{itemize}
1614
1615\index{indentation}
1616
1617
1618\subsection{Syntax colors}
1619
1620The coloring is applied in a background ``thread,'' so you may
1621occasionally see uncolorized text. To change the color
1622scheme, edit the \code{[Colors]} section in \file{config.txt}.
1623
1624\begin{description}
1625\item[Python syntax colors:]
1626
1627\begin{description}
1628\item[Keywords] orange
1629\item[Strings ] green
1630\item[Comments] red
1631\item[Definitions] blue
1632\end{description}
1633
1634\item[Shell colors:]
1635\begin{description}
1636\item[Console output] brown
1637\item[stdout] blue
1638\item[stderr] dark green
1639\item[stdin] black
1640\end{description}
1641\end{description}
1642
1643
1644\subsubsection{Command line usage}
1645
1646\begin{verbatim}
1647idle.py [-c command] [-d] [-e] [-s] [-t title] [arg] ...
1648
1649-c command run this command
1650-d enable debugger
1651-e edit mode; arguments are files to be edited
1652-s run $IDLESTARTUP or $PYTHONSTARTUP first
1653-t title set title of shell window
1654\end{verbatim}
1655
1656If there are arguments:
1657
1658\begin{enumerate}
1659\item If \programopt{-e} is used, arguments are files opened for
1660 editing and \code{sys.argv} reflects the arguments passed to
1661 IDLE itself.
1662
1663\item Otherwise, if \programopt{-c} is used, all arguments are
1664 placed in \code{sys.argv[1:...]}, with \code{sys.argv[0]} set
1665 to \code{'-c'}.
1666
1667\item Otherwise, if neither \programopt{-e} nor \programopt{-c} is
1668 used, the first argument is a script which is executed with
1669 the remaining arguments in \code{sys.argv[1:...]} and
1670 \code{sys.argv[0]} set to the script name. If the script name
1671 is '-', no script is executed but an interactive Python
1672 session is started; the arguments are still available in
1673 \code{sys.argv}.
1674\end{enumerate}