An Introduction To Tkinter (Fredrik Lundh)
An Introduction To Tkinter (Fredrik Lundh)
An Introduction To Tkinter (Fredrik Lundh)
1999-12-01 22:15
An Introduction to Tkinter
by Fredrik Lundh
Copyright © 1999 by Fredrik Lundh
Fredrik Lundh
Copyright (c) 1999 by Fredrik Lundh
Review Copy. Do not redistribute! 1999-12-01 22:15
Toolbars
.........................................................................................................................
26
Table of Contents
Status
Bars..................................................................................................................
... 27
9. Dialog Windows
................................................................................................................. 29
Grid Layouts
.................................................................................................................. 34
Preface
....................................................................................................................i
Validating
Data..............................................................................................................
36
I. Introducing Tkinter
............................................................................................2
II. Tkinter
Reference............................................................................................ 37
1. What's
Tkinter?............................................................................................................
..........1
10. The BitmapImage Class
................................................................................................... 38
2. Hello, Tkinter
....................................................................................................................... 2
When to use the BitmapImage Class
...........................................................................38
Running the Example
..................................................................................................... 2
Patterns.....................................................................................................
..................... 38
Details
..........................................................................................................................
.... 2
Methods
.........................................................................................................................
38
3. Hello, Again
..........................................................................................................................
4
Options
..........................................................................................................................
38
Running the Example
..................................................................................................... 4
11. The Button
Widget............................................................................................................4
0
Details
..........................................................................................................................
.... 5
When to use the Button Widget
...................................................................................40
More on widget references
............................................................................................. 6
Patterns.....................................................................................................
.....................40
More on widget
names.................................................................................................... 6
Methods
.........................................................................................................................
41
4. Tkinter Classes
..................................................................................................................... 8
Helpers.....................................................................................................
...................... 41
Widget classes
.................................................................................................................8
Options
..........................................................................................................................
41
Mixins
..........................................................................................................................
.... 9
12. The Canvas
Widget...........................................................................................................4
4
Implementation mixins
......................................................................................... 9
When to use the Canvas
Widget................................................................................... 44
Geometry
mixins.................................................................................................... 9
Concepts
........................................................................................................................4
4
Widget configuration management
...................................................................... 9
Items
..................................................................................................................... 44
5. Widget
Configuration...................................................................................................
.......11
Coordinate Systems
............................................................................................. 45
Configuration Interface
.................................................................................................11
Item Specifiers
..................................................................................................... 45
Backwards
Compatibility...............................................................................................12
Printing.....................................................................................................
............ 46
6. Widget Styling
.....................................................................................................................13
Patterns.....................................................................................................
..................... 46
Colors
..........................................................................................................................
....13
Methods
.........................................................................................................................
46
Color Names
..........................................................................................................13
Printing.....................................................................................................
............48
RGB
Specifications................................................................................................1
3
Searching for
Items.............................................................................................. 49
Fonts
..........................................................................................................................
.... 14
Manipulating Tags
...............................................................................................50
Font descriptors
................................................................................................... 14
Special Methods for Text
Items............................................................................51
Font
names............................................................................................................1
5
Scrolling...................................................................................................
..............51
System fonts
......................................................................................................... 16
Options
..........................................................................................................................
52
X Font
Descriptors............................................................................................... 16
13. The Canvas Arc Item
........................................................................................................54
Text Formatting
.............................................................................................................17
Methods
.........................................................................................................................
54
Borders
..........................................................................................................................
.17
Options
..........................................................................................................................
55
Relief........................................................................................................
..............17
14. The Canvas Bitmap
Item.................................................................................................. 57
Focus Highlights
.................................................................................................. 18
Bitmaps....................................................................................................
...................... 57
Cursors.....................................................................................................
...................... 18
Methods
.........................................................................................................................
58
7. Events and
Bindings...........................................................................................................
19
Options
..........................................................................................................................
58
Events
..........................................................................................................................
.. 19
15. The Canvas Image
Item.................................................................................................... 59
The Event
Object...................................................................................................21
Methods
.........................................................................................................................
59
Instance and Class
Bindings.................................................................................21
coords
................................................................................................................... 59
Protocols...................................................................................................
............ 23
itemconfigure
....................................................................................................... 59
Other Protocols
.................................................................................................... 24
Options
..........................................................................................................................
59
8. Application
Windows.........................................................................................................
25
16. The Canvas Line
Item....................................................................................................... 61
Base Windows
............................................................................................................... 25
Methods
.........................................................................................................................
61
Menus
..........................................................................................................................
.. 25
Options
..........................................................................................................................
61
i
ii
Copyright (c) 1999 by Fredrik Lundh
Review Copy. Do not redistribute! 1999-12-01 22:15
17. The Canvas Oval
Item.......................................................................................................63
Manager Methods
................................................................................................ 87
Methods
.........................................................................................................................
63
Options
..........................................................................................................................
87
Options
..........................................................................................................................
63
28. The IntVar
Class...............................................................................................................8
9
18. The Canvas Polygon Item
................................................................................................64
When to use the IntVar Class
.......................................................................................89
Methods
.........................................................................................................................
64
Patterns.....................................................................................................
.....................89
Options
..........................................................................................................................
64
Methods
.........................................................................................................................
89
19. The Canvas Rectangle
Item.............................................................................................. 66
29. The Label Widget
.............................................................................................................90
Methods
.........................................................................................................................
66
When to use the Label
Widget......................................................................................90
Options
..........................................................................................................................
66
Patterns.....................................................................................................
.....................90
20. The Canvas Text
Item...................................................................................................... 67
Methods
.........................................................................................................................
91
Methods
.........................................................................................................................
67
Options
..........................................................................................................................
91
Options
..........................................................................................................................
67
30. The Listbox
Widget..........................................................................................................
93
21. The Canvas Window Item
................................................................................................69
When to use the Listbox Widget
.................................................................................. 93
Methods
.........................................................................................................................
69
Patterns.....................................................................................................
..................... 93
Options
..........................................................................................................................
69
Methods
.........................................................................................................................
96
22. The Checkbutton Widget
................................................................................................. 70
Selection
Methods................................................................................................ 97
When to use the Checkbutton Widget
......................................................................... 70
Scrolling Methods
................................................................................................ 97
Patterns.....................................................................................................
..................... 70
Options
..........................................................................................................................
98
Methods
..........................................................................................................................
71
31. The Menu Widget
........................................................................................................... 100
Options
..........................................................................................................................
.71
When to use the Menu Widget
................................................................................... 100
23. The DoubleVar
Class........................................................................................................ 75
Patterns.....................................................................................................
................... 100
When to use the DoubleVar Class
................................................................................ 75
Methods
.......................................................................................................................
102
Patterns.....................................................................................................
..................... 75
Displaying Menus
.............................................................................................. 104
Methods
.........................................................................................................................
75
Options
........................................................................................................................1
04
24. The Entry
Widget.............................................................................................................
76
32. The Menubutton Widget
............................................................................................... 107
When to use the Entry Widget
..................................................................................... 76
When to use the Menubutton
Widget........................................................................ 107
Concepts
........................................................................................................................
76
Patterns.....................................................................................................
................... 107
Indexes.....................................................................................................
............. 76
Methods
.......................................................................................................................
107
Patterns.....................................................................................................
..................... 76
Options
........................................................................................................................
107
Methods
.........................................................................................................................
77
33. The Message Widget
...................................................................................................... 108
Selection
Methods................................................................................................ 77
When to use the Message
Widget............................................................................... 108
Scrolling Methods
................................................................................................ 78
Patterns.....................................................................................................
................... 108
Options
..........................................................................................................................
78
Methods
.......................................................................................................................
108
25. The Font Class
..................................................................................................................80
Options
........................................................................................................................1
08
Patterns.....................................................................................................
.....................80
34. The Pack Geometry Manager
.........................................................................................110
Methods
.........................................................................................................................
80
When to use the Pack Manager
...................................................................................110
Functions
.......................................................................................................................8
0
Patterns.....................................................................................................
....................110
Options
..........................................................................................................................
81
Methods
........................................................................................................................1
10
26. The Frame Widget
...........................................................................................................82
Widget Methods
..................................................................................................110
When to use the Frame
Widget.................................................................................... 82
Manager Methods
...............................................................................................110
Patterns.....................................................................................................
..................... 82
Options
.........................................................................................................................
111
Methods
.........................................................................................................................
82
35. The PhotoImage
Class.....................................................................................................112
Options
..........................................................................................................................
82
When to use the PhotoImage
Class.............................................................................112
27. The Grid Geometry
Manager...........................................................................................84
Patterns.....................................................................................................
....................112
When to use the Grid Manager
....................................................................................84
Methods
........................................................................................................................1
12
Patterns.....................................................................................................
.....................84
Options
.........................................................................................................................
113
Methods
.........................................................................................................................
86
36. The Place Geometry Manager
........................................................................................115
Widget Methods
...................................................................................................86
When to use the Place Manager
.................................................................................. 115
iii
iv
Copyright (c) 1999 by Fredrik Lundh
Review Copy. Do not redistribute! 1999-12-01 22:15
Patterns.....................................................................................................
.................... 115
tag_delete
.................................................................................................. 142
Methods
........................................................................................................................1
16
tag_config.................................................................................................
. 142
Options
.........................................................................................................................
117
tag_cget
..................................................................................................... 142
37. The Radiobutton
Widget.................................................................................................118
tag_bind
.................................................................................................... 142
When to use the Radiobutton
Widget.........................................................................118
tag_unbind
................................................................................................ 142
Patterns.....................................................................................................
....................118
tag_names
................................................................................................. 142
Methods
........................................................................................................................1
19
tag_nextrange
........................................................................................... 143
Options
........................................................................................................................
120
tag_prevrange
........................................................................................... 143
38. The Scale
Widget............................................................................................................
123
tag_lower..................................................................................................
. 143
When to use the Scale Widget
.................................................................................... 123
tag_raise...................................................................................................
. 143
Patterns.....................................................................................................
................... 123
tag_ranges................................................................................................
. 143
Methods
.......................................................................................................................
123
Methods for Selections
...................................................................................... 143
Options
........................................................................................................................
123
Methods for
Rendering...................................................................................... 144
39. The Scrollbar Widget
......................................................................................................125
bbox
........................................................................................................... 144
When to use the Scrollbar
Widget...............................................................................125
dlineinfo...................................................................................................
.. 144
Patterns.....................................................................................................
....................125
Methods for
Printing.......................................................................................... 144
Methods
.......................................................................................................................
126
Methods for
Searching....................................................................................... 144
Options
........................................................................................................................
126
search.......................................................................................................
.. 144
40. The StringVar Class
....................................................................................................... 129
Methods for Scrolling
........................................................................................ 145
When to use the StringVar
Class................................................................................ 129
scan_mark, scan_dragto ..........................................................................
145
Patterns.....................................................................................................
................... 129
xview,
yview............................................................................................... 145
Methods
.......................................................................................................................
129
xview,
yview............................................................................................... 145
41. The Text Widget
............................................................................................................. 130
xview,
yview............................................................................................... 145
When to use the Text Widget
..................................................................................... 130
yview_pickplace.......................................................................................
. 146
Concepts
......................................................................................................................
130
Options
........................................................................................................................
146
Indexes.....................................................................................................
........... 130
42. The Toplevel Widget
...................................................................................................... 149
Lines and columns
.....................................................................................131
When to use the Toplevel
Widget............................................................................... 149
Line endings
...............................................................................................131
Methods
.......................................................................................................................
149
Named
indexes...........................................................................................131
Options
........................................................................................................................
149
Coordinates
............................................................................................... 132
43. Basic Widget Methods
.................................................................................................... 151
Embedded
objects..................................................................................... 132
Configuration
............................................................................................................... 151
Expressions
............................................................................................... 132
config
................................................................................................................... 151
Marks
.................................................................................................................. 132
config
................................................................................................................... 151
Tags..........................................................................................................
........... 133
cget...........................................................................................................
............151
Patterns.....................................................................................................
....................135
keys
......................................................................................................................15
1
Methods
........................................................................................................................1
37
Event processing
..........................................................................................................152
Methods for
Marks............................................................................................. 138
mainloop...................................................................................................
...........152
Methods for Embedded
Windows..................................................................... 139
quit...........................................................................................................
............152
Methods for Embedded Images
........................................................................ 140
update
..................................................................................................................152
image_create............................................................................................
. 140
update_idletasks
.................................................................................................152
index
...........................................................................................................141
focus_set...................................................................................................
...........152
delete
..........................................................................................................141
focus_displayof
...................................................................................................152
image_cget
.................................................................................................141
focus_force
..........................................................................................................152
image_config............................................................................................
..141
focus_get
.............................................................................................................152
image_names
.............................................................................................141
focus_lastfor.............................................................................................
...........152
Methods for Tags
................................................................................................141
tk_focusNext
.......................................................................................................153
tag_add.....................................................................................................
. 142
tk_focusPrev.............................................................................................
...........153
tag_remove...............................................................................................
. 142
grab_current
.......................................................................................................153
v
vi
Copyright (c) 1999 by Fredrik Lundh
Review Copy. Do not redistribute! 1999-12-01 22:15
grab_release
........................................................................................................153
winfo_screenvisual
............................................................................................ 159
grab_set....................................................................................................
...........153
winfo_toplevel.........................................................................................
........... 159
grab_set_global
..................................................................................................153
winfo_visual.............................................................................................
.......... 159
grab_status...............................................................................................
...........153
winfo_x, winfo_y
............................................................................................... 159
wait_variable............................................................................................
...........153
Miscellaneous
.............................................................................................................. 159
wait_visibility...........................................................................................
...........153
bell............................................................................................................
........... 159
wait_window............................................................................................
.......... 154
clipboard_append.....................................................................................
......... 159
Event
callbacks...........................................................................................................
. 154
clipboard_clear
.................................................................................................. 159
bind
.....................................................................................................................
154
selection_clear
................................................................................................... 160
unbind.......................................................................................................
.......... 154
selection_get
...................................................................................................... 160
bind_all
.............................................................................................................. 154
selection_handle
................................................................................................ 160
unbind_all
.......................................................................................................... 154
selection_own
.................................................................................................... 160
bind_class.................................................................................................
.......... 154
selection_own_get....................................................................................
......... 160
unbind_class
...................................................................................................... 154
tk_focusFollowsMouse............................................................................
.......... 160
bindtags
...............................................................................................................155
tk_strictMotif
..................................................................................................... 160
bindtags
...............................................................................................................155
winfo_rgb
........................................................................................................... 160
Alarm handlers and other non-event
callbacks..........................................................155
Tkinter Interface
Methods.......................................................................................... 160
after...........................................................................................................
...........155
getboolean
.......................................................................................................... 160
after_cancel..............................................................................................
...........155
getdouble
............................................................................................................ 160
after...........................................................................................................
...........155
getint.........................................................................................................
...........161
after_idle
.............................................................................................................155
register
.................................................................................................................161
Window
management.................................................................................................
156
winfo_atom
.........................................................................................................161
lift.............................................................................................................
........... 156
winfo_atomname......................................................................................
..........161
lower
................................................................................................................... 156
Option
Database...........................................................................................................
161
Window Related
Information..................................................................................... 156
option_add
..........................................................................................................161
winfo_cells
......................................................................................................... 156
option_clear
........................................................................................................161
winfo_children.........................................................................................
.......... 156
option_get
...........................................................................................................161
winfo_class...............................................................................................
.......... 156
option_readfile.........................................................................................
...........161
winfo_colormapfull
........................................................................................... 156
44. Toplevel Window
Methods............................................................................................ 162
winfo_containing
............................................................................................... 156
Visibility Methods
....................................................................................................... 162
winfo_depth
........................................................................................................157
deiconify
............................................................................................................. 162
winfo_exists
........................................................................................................157
iconify
................................................................................................................. 162
winfo_pixels
........................................................................................................157
withdraw...................................................................................................
.......... 162
winfo_geometry
..................................................................................................157
state..........................................................................................................
........... 162
winfo_width,
winfo_height................................................................................157
Style
Methods............................................................................................................
.. 162
winfo_id...................................................................................................
............157
title
......................................................................................................................
162
winfo_ismapped
.................................................................................................157
group........................................................................................................
........... 162
winfo_manager
...................................................................................................157
transient....................................................................................................
.......... 163
winfo_name
....................................................................................................... 158
overrideredirect........................................................................................
.......... 163
winfo_parent............................................................................................
.......... 158
Window Geometry
Methods....................................................................................... 163
winfo_pathname.......................................................................................
......... 158
geometry
............................................................................................................. 163
winfo_reqheight,
winfo_reqwidth.................................................................... 158
geometry
............................................................................................................. 163
winfo_rootx, winfo_rooty
................................................................................. 158
aspect
.................................................................................................................. 163
winfo_screen............................................................................................
.......... 158
maxsize
............................................................................................................... 163
winfo_screencells.....................................................................................
.......... 158
minsize.....................................................................................................
........... 163
winfo_screendepth
............................................................................................ 158
resizable...................................................................................................
........... 163
winfo_screenwidth, winfo_screenheight
......................................................... 159
Icon
Methods............................................................................................................
... 164
winfo_screenmmwidth,
winfo_screenmmheight............................................ 159
iconbitmap................................................................................................
.......... 164
vii
viii
Copyright (c) 1999 by Fredrik Lundh
Review Copy. Do not redistribute! 1999-12-01 22:15
iconmask..................................................................................................
........... 164
iconname
............................................................................................................ 164
iconposition..............................................................................................
.......... 164
iconwindow
........................................................................................................ 164
Property Access Methods
........................................................................................... 164
client
................................................................................................................... 164
colormapwindows
.............................................................................................. 164
command..................................................................................................
.......... 164
focusmodel
......................................................................................................... 165
frame........................................................................................................
........... 165
positionfrom.............................................................................................
.......... 165
protocol....................................................................................................
........... 165
sizefrom
.............................................................................................................. 165
Index
.................................................................................................................. 166
ix
Copyright (c) 1999 by Fredrik Lundh
Review Copy. Do not redistribute! 1999-12-01 22:15
Preface
I. Introducing Tkinter
This is yet another snapshot of my continously growing Tkinter
documentation.
If you like this book, you might be interested in hearing that O'Reilly
& Associates The first few chapters in this book provide a brief
introduction to Tkinter. After reading this, (http://www.ora.com) will be
publishing a Tkinter book (tentatively called Programming you should have
a fair grasp of the Tkinter fundamentals.
Tkinter in Python) in a not too distant future. This book features lots of
brand new material written by yours truly, giving you a more thorough
description of Tkinter (and many other things) than you can find anywhere
else.
</F>
(last update: Oct 05, 1999)
i
Copyright (c) 1999 by Fredrik Lundh