Tkinter 8.4 reference - a GUI for Python (2010).pdf
(
1436 KB
)
Pobierz
Tkinter 8.4 reference: a
GUI for Python
John W. Shipman
2010-03-27 12:18
Abstract
Describes the
Tkinter
widget set for constructing graphical user interfaces (GUIs) in the Python
programming language.
This publication is available in Web form
1
and also as a PDF document
2
. Please forward any
comments to
tcc-doc@nmt.edu.
Table of Contents
1. What is Tkinter? ....................................................................................................................... 3
2. A minimal application .............................................................................................................. 3
3. Definitions .............................................................................................................................. 4
4. Layout management ................................................................................................................. 5
4.1. The
.grid()
method .................................................................................................... 5
4.2. Other grid management methods ................................................................................... 6
4.3. Configuring column and row sizes ................................................................................. 7
4.4. Making the root window resizeable ................................................................................ 8
5. Standard attributes ................................................................................................................... 8
5.1. Dimensions ................................................................................................................... 9
5.2. The coordinate system ................................................................................................... 9
5.3. Colors ........................................................................................................................... 9
5.4. Type fonts ................................................................................................................... 10
5.5. Anchors ...................................................................................................................... 11
5.6. Relief styles ................................................................................................................. 12
5.7. Bitmaps ....................................................................................................................... 12
5.8. Cursors ....................................................................................................................... 12
5.9. Images ........................................................................................................................ 14
5.10. Geometry strings ........................................................................................................ 14
5.11. Window names ........................................................................................................... 15
5.12. Cap and join styles ..................................................................................................... 15
5.13. Dash patterns ............................................................................................................. 16
5.14. Matching stipple patterns ............................................................................................ 16
6. The
Button
widget ................................................................................................................ 17
7. The
Canvas
widget ................................................................................................................ 19
7.1.
Canvas
coordinates ...................................................................................................... 20
7.2. The
Canvas
display list ................................................................................................ 20
7.3.
Canvas
object IDs ........................................................................................................ 21
7.4.
Canvas
tags ................................................................................................................ 21
1
2
http://www.nmt.edu/tcc/help/pubs/tkinter/
http://www.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf
New Mexico Tech Computer Center
Tkinter reference
1
7.5.
Canvas
tagOrId
arguments ...................................................................................... 21
7.6. Methods on
Canvas
widgets ........................................................................................ 21
7.7.
Canvas
arc objects ....................................................................................................... 26
7.8.
Canvas
bitmap objects ................................................................................................. 28
7.9.
Canvas
image objects .................................................................................................. 29
7.10.
Canvas
line objects ..................................................................................................... 29
7.11.
Canvas
oval objects .................................................................................................... 31
7.12.
Canvas
polygon objects .............................................................................................. 32
7.13.
Canvas
rectangle objects ............................................................................................. 34
7.14.
Canvas
text objects ..................................................................................................... 35
7.15.
Canvas
window objects .............................................................................................. 36
8. The
Checkbutton
widget ...................................................................................................... 37
9. The
Entry
widget .................................................................................................................. 40
9.1. Scrolling an
Entry
widget ............................................................................................ 43
10. The
Frame
widget ................................................................................................................ 43
11. The
Label
widget ................................................................................................................ 44
12. The
LabelFrame
widget ...................................................................................................... 46
13. The
Listbox
widget ............................................................................................................ 48
13.1. Scrolling a
Listbox
widget ........................................................................................ 52
14. The
Menu
widget .................................................................................................................. 52
14.1.
Menu
item creation (coption) options ......................................................................... 55
15. The
Menubutton
widget ...................................................................................................... 56
16. The
Message
widget ............................................................................................................ 58
17. The
OptionMenu
widget ....................................................................................................... 59
18. The
PanedWindow
widget .................................................................................................... 60
18.1.
PanedWindow
child configuration options ................................................................... 63
19. The
Radiobutton
widget .................................................................................................... 63
20. The
Scale
widget ................................................................................................................ 66
21. The
Scrollbar
widget ........................................................................................................ 69
21.1. The
Scrollbar
command
callback ............................................................................ 72
21.2. Connecting a
Scrollbar
to another widget ................................................................ 73
22. The
Spinbox
widget ............................................................................................................ 73
23. The
Text
widget .................................................................................................................. 77
23.1.
Text
widget indices ................................................................................................... 80
23.2.
Text
widget marks .................................................................................................... 81
23.3.
Text
widget images ................................................................................................... 82
23.4.
Text
widget windows ............................................................................................... 82
23.5.
Text
widget tags ....................................................................................................... 82
23.6. Setting tabs in a
Text
widget ...................................................................................... 82
23.7. The
Text
widget undo/redo stack .............................................................................. 83
23.8. Methods on
Text
widgets .......................................................................................... 83
24.
Toplevel:
Top-level window methods .................................................................................. 90
25. Universal widget methods ..................................................................................................... 93
26. Standardizing appearance ................................................................................................... 100
26.1. How to name a widget class ...................................................................................... 101
26.2. How to name a widget instance ................................................................................. 101
26.3. Resource specification lines ....................................................................................... 102
26.4. Rules for resource matching ...................................................................................... 103
27. Connecting your application logic to the widgets ................................................................... 103
28. Control variables: the values behind the widgets ................................................................... 104
29. Focus: routing keyboard input ............................................................................................. 106
30. Events ................................................................................................................................ 107
2
Tkinter reference
New Mexico Tech Computer Center
30.1. Levels of binding ......................................................................................................
30.2. Event sequences .......................................................................................................
30.3. Event types ..............................................................................................................
30.4. Event modifiers ........................................................................................................
30.5. Key names ...............................................................................................................
30.6. Writing your handler: The
Event
class ......................................................................
30.7. The extra arguments trick ..........................................................................................
30.8. Virtual events ...........................................................................................................
31. Pop-up dialogs ....................................................................................................................
31.1. The
tkMessageBox
dialogs module ..........................................................................
31.2. The
tkFileDialog
module .....................................................................................
31.3. The
tkColorChooser
module .................................................................................
107
108
109
110
110
113
114
115
116
116
117
118
1. What is Tkinter?
Tkinter is a GUI (graphical user interface) widget set for Python. This document contains only the
commoner features.
This document applies to Python 2.5 and Tkinter 8.4 running in the X Window system under Linux.
Your version may vary.
Pertinent references:
• Fredrik Lundh, who wrote Tkinter, has two versions of his
An Introduction to Tkinter:
a more complete
1999 version
3
and a 2005 version
4
that presents a few newer features.
•
Python and Tkinter Programming
by John Grayson (Manning, 2000, ISBN 1-884777-81-3) is out of print,
but has many useful examples and also discusses an extension package called
Pmw:
Python megaw-
idgets
5
.
•
Python 2.5 quick reference
6
: general information about the Python language.
• For an example of a sizeable working application (around 1000 lines of code), see
huey:
A color and
font selection tool
7
.
We'll start by looking at the visible part of Tkinter: creating the widgets and arranging them on the
screen. Later we will talk about how to connect the face—the “front panel”—of the application to the
logic behind it.
2. A minimal application
Here is a trivial Tkinter program containing only a Quit button:
#!/usr/local/bin/python
from Tkinter import *
1
2
3
4
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
3
4
http://www.pythonware.com/library/tkinter/introduction/
http://effbot.org/tkinterbook/
5
http://pmw.sourceforge.net/
6
http://www.nmt.edu/tcc/help/pubs/python/web/
7
http://www.nmt.edu/tcc/help/lang/python/examples/huey/
New Mexico Tech Computer Center
Tkinter reference
3
self.grid()
self.createWidgets()
5
def createWidgets(self):
self.quitButton = Button ( self, text='Quit',
6
command=self.quit )
7
self.quitButton.grid()
8
app = Application()
app.master.title("Sample application")
9
10
app.mainloop()
1
2
3
4
5
6
7
8
9
10
This line makes the script self-executing, assuming that your system has the Python interpreter at
path
/usr/local/bin/python.
This line imports the entire Tkinter package into your program's namespace.
Your application class must inherit from Tkinter's
Frame
class.
Calls the constructor for the parent class,
Frame.
Necessary to make the application actually appear on the screen.
Creates a button labeled “Quit”.
Places the button on the application.
The main program starts here by instantiating the
Application
class.
This method call sets the title of the window to “Sample application”.
Starts the application's main loop, waiting for mouse and keyboard events.
3. Definitions
Before we proceed, let's define some of the common terms.
window
This term has different meanings in different contexts, but in general it refers to a rectangular area
somewhere on your display screen.
top-level window
A window that exists independently on your screen. It will be decorated with the standard frame
and controls for your system's desktop manager. You can move it around on your desktop. You
can generally resize it, although your application can prevent this
widget
The generic term for any of the building blocks that make up an application in a graphical user in-
terface. Examples of widgets: buttons, radiobuttons, text fields, frames, and text labels.
frame
In Tkinter, the
Frame
widget is the basic unit of organization for complex layouts. A frame is a
rectangular area that can contain other widgets.
child, parent
When any widget is created, a
parent-child
relationship is created. For example, if you place a text
label inside a frame, the frame is the parent of the label.
4
Tkinter reference
New Mexico Tech Computer Center
4. Layout management
Later we will discuss the widgets, the building blocks of your GUI application. How do widgets get
arranged in a window?
Although there are three different “geometry managers” in Tkinter, the author strongly prefers the
.grid()
geometry manager for pretty much everything. This manager treats every window or frame
as a table—a gridwork of rows and columns.
• A
cell
is the area at the intersection of one row and one column.
• The width of each column is the width of the widest cell in that column.
• The height of each row is the height of the largest cell in that row.
• For widgets that do not fill the entire cell, you can specify what happens to the extra space. You can
either leave the extra space outside the widget, or stretch the widget to fit it, in either the horizontal
or vertical dimension.
• You can combine multiple cells into one larger area, a process called
spanning.
When you create a widget, it does not appear until you register it with a geometry manager. Hence,
construction and placing of a widget is a two-step process that goes something like this:
self.thing =
Constructor(parent,
...)
self.thing.grid(...)
where
Constructor
is one of the widget classes like
Button, Frame,
and so on, and
parent
is the
parent widget in which this child widget is being constructed. All widgets have a
.grid()
method
that you can use to tell the geometry manager where to put it.
4.1. The
.grid()
method
To display a widget
w
on your application screen:
w.grid(option=value,
...)
This method registers a widget
w
with the grid geometry manager—if you don't do this, the widget will
exist internally, but it will not be visible on the screen.
Here are the options to the
.grid()
geometry management method:
column
The column number where you want the widget gridded, counting from zero. The default
value is zero.
columnspan
Normally a widget occupies only one cell in the grid. However, you can grab multiple
cells of a row and merge them into one larger cell by setting the
columnspan
option to
the number of cells. For example,
w.grid(row=0,
column=2, columnspan=3)
would place widget
w
in a cell that spans columns 2, 3, and 4 of row 0.
in_
ipadx
ipady
padx
To register
w
as a child of some widget
w
2
, use
in_=w
2
. The new parent
w
2
must be a
descendant of the
parent
widget used when
w
was created.
Internal x padding. This dimension is added inside the widget inside its left and right
sides.
Internal y padding. This dimension is added inside the widget inside its top and bottom
borders.
External x padding. This dimension is added to the left and right outside the widget.
New Mexico Tech Computer Center
Tkinter reference
5
Plik z chomika:
Pinus000
Inne pliki z tego folderu:
A Byte of Python, v1.20 (for Python 2.x) (2005).pdf
(337 KB)
A Byte of Python, v1.92 (for Python 3.0) (2009).pdf
(608 KB)
A Learner's Guide to Programming Using the Python Language (2009).pdf
(17233 KB)
A Primer on Scientific Programming with Python (2009).pdf
(6983 KB)
Beginning Game Development with Python and Pygame - From Novice to Professional (2007).pdf
(7814 KB)
Inne foldery tego chomika:
-- ▣▣▣ ELEKTRONIKA ▣▣ ▬▬▬▬▬▬▬▬▬▬▬▬▬
Pliki dostępne do 01.06.2025
Pliki dostępne do 08.07.2024
Pliki dostępne do 19.01.2025
►Kurs Java
Zgłoś jeśli
naruszono regulamin