Chun_ch19 - openbookproject

jockeyropeInternet and Web Development

Feb 2, 2013 (4 years and 8 months ago)

843 views

GUI
Progr
a
mming

GUI
编程



Ch
a
pter

T
opics

本章主题




Int
r
oduction




Tkinter

and

Python

P
r
ogramming




Tkinter

Module



Tk

Widgets




Tkinter

Examples




Label,

Button

and

Scale

Widgets




An

Intermediate

Tk

Example




Brief

T
our
of

Other

GUIs

(Tix,

Pm
w
,

wxPytho
n,

PyGTK)




Related

Modules

and

Other

GUIs

介绍

Tkinter

Python
编程

Tkinter
模块

Tk

Widgets

Tkinter
举例

标签、按钮和矢量
Widgets

中等的
Tk
举例

其他
GUI
简介(
Tix

Pm
w

wxPython

PyGTK


相关模块和其他
GUI




19

n


this


chapte
r
,

we

will


give


you


a

brief


introduction


to


the


subject


of

gr
aphical

user

interface

(GUI)

programming.

If

you

are

somewhat

new

t
o

thi
s

are
a

o
r

wan
t

t
o

lear
n

mor
e

abou
t

it
,

o
r

i
f

yo
u

wan
t

t
o

se
e

ho
w

i
t

is
done

in

Python,

then

this

chapter

is

for

you.

W
e

cannot

show

you

everything
about

GUI

application

development

her
e

in

this

one

chapte
r
,

but

we

will

give
you

a

very

solid

introduction

to

it.

The

primary

GUI

toolkit

we

will

be

using

is
Tk,

Python

s

default

GUI,

and

we

will

access

Tk

from

its

Python

interface
called

Tkinter

(short

for

“Tk

interface”).

Tk

is

not

the

“lat
est

and

greatest”

nor

does

it

have

the

most

robust

set

of
GUI

building

blocks,

but

it

is

fairly

simple

to

use

and

will

allow

you

to

build
GUIs

that

run

on

most

platforms.

W
e

will

present

several

simple

and

inte
r
-

mediate

examples

using

Tkinte
r
,

followed

by

a

few

examples

using

other

tool
-

kits.

Once

you

have

completed

this

chapte
r
,

you

will

have

the

skills

to

build
more

complex

applications

and/or

move

to

a

more

modern

graphical

toolkit.
Pytho
n


ha
s


binding
s


o
r


adapter
s


t
o


mos
t


o
f


th
e


majo
r


toolkit
s

ou
t


there,
including

commercial

systems.



19.1


Int
r
oduction

介绍


19.1.1

What

A
r
e

Tcl
,
Tk,

and

Tkinter?

什么是
呣l



呫T湴nr



Tkinter

is

Python

s

default

GUI

librar
y
.

It

is

based

on

the

Tk

toolkit,

originally
designe
d

fo
r

th
e

T
oo
l

Comman
d

Languag
e

(
T
cl)
.

Du
e

t
o

Tk

s

popularit
y
,

i
t


has

been

ported

to

a

variety

of

other

scripting

languages,

including

Perl

(Perl/Tk),


Ruby


(Ruby/Tk),

and


Python


(Tkinter).


W
ith


the


GUI


develop
-

ment

portability

and

flexibility

of

Tk,

along

with

the

simplicity

of

scripting
la
nguage

integrated

with

the

power

of

systems

language,

you

are

given

the
tools

to

rapidly

design

and

implement

a

wide

variety

of

commercial
-
quality
GUI

applications.

Tkinter

Python
默认的
GUI
库,它基于
Tk
,原来是为了
T
cl
设计的(
oo
l

Comman
d

Languag
e
)。因为
Tk
的流行,它现在适用于很多其他的脚本语
言,包括
Perl
(Perl/Tk)

Ruby

(Ruby/Tk)


Python(Tkinter)
。由于
Tk


GUI
开发的适用性和灵活性,以及脚本语言与强大的系统语言整合的
简单化,为您提供了快速设计和实现各种不同的商业级质量的
GUI

用程序的工具。


If

you

are

new

to

GUI

programming,

you

will

be

pleas
antly

surprised

at
how

easy

it

is.

Y
ou

will

also

find

that

Python,

along

with

Tkinte
r
,

provides

a
fast

and

exciting

way

to

build

applications

that

are

fun

(and

perhaps

useful)
and

that

would
have

taken

much

longer

if

you

had

had

to

program

directly

in
C/C+
+

with

the

native

windowing

system

s

libraries.

Once

you

have

designed
the

application

and

the

look

and

feel

that

goes

along

with

your

program,

you
will

use

basic

building

blocks

known

as

widgets

to

piece

together

the

desired
components,

and

finall
y
,
to

at
tach

functionality

to

“make
it

real.”

如果您是一个
GUI
编程的新手,您会惊讶于它是多么的容易。

If

you

are

an

old

hand

at

using

Tk,

either

with

T
cl

or

Perl,

you

will

find
Python

a

refreshing

way

to

program

GUIs.

On

top

of

that,

it

provides

an

even
faster

rapid

prototyping

system

for

b
uilding

them.

Remember

that

you

also
I

have

Python

s

system

accessibilit
y
,

networking

functionalit
y
,

XML,

numerical
and

visual

processing,

database

access,

and

all

the

other

standard

library

and
third
-
party

extension

modules.

Once

you

get

Tkinter

up

on

your

system,

it

will

take

less

than

15

minutes
to

get

your
first

GUI

application

running.



19.1.2

Getting

Tkinter

Installed

and

W
orking

安装和使用
Tk楮ter


Like

threading,

Tkinter

is

not

necessarily

turned

on

by

default

on

your

sys
-

tem.

Y
ou

can

tell

whether

Tkinter

is

available

for

your

Python

interpreter

by
attempting

to

import

the

Tkinter

module.

If

Tkinter

is

available,

then

no
errors

occur:

和线程一样,
Tkinter
默认情况下没有必要安装到您的系统上。您可以
通过尝试导入
Tkinter
模块来判断它是否能被
Python
解释器使用。


>>>
import
Tkinter

>>>

If


your

Python


interpreter


was


not


compiled


with


Tkinter


enabled,


the
module

import

fails:

如果您的
Python
解释器不能与
Tkinter
编译,模块倒入会失败。


>>>
import
Tkinter

Trace
back (innermost last):

File "<stdin>", line 1, in ?

Fil
e

"/usr/lib/python1.5/lib
-
tk/Tkinter.py"
,

lin
e

8
,

i
n

?

import
_tkinter # If this fails your Python may not
be configured for Tk

ImportError: No module named _tkinter



You may have to recompile yo
ur Python interpreter to get access to

Tkinter. This usually involves editing the Modules/Setup file and enabling all the correct
settings to compile your Python interpreter with

hooks

to Tkinter or choosing to
have Tk installed on your sy
stem. Check the README file for your Python distribution for
specific instructions on getting Tkinter to compile on your system. Be sure, after your
compilation, that you start the new Python interpreter you just created; otherwise, it will act just
like y
our

old one without Tkinter (and in fact, it is your old one).

您可能得重新编译您的
Python
解释器来访问
Tkinter
。这通常会涉及到编辑模块
/
安装
文件,并激活所有的正确设置来与
Tkinter
编译您的
Python
解释器
,或者选择安装
Tk

您的系统上。当把
Tkinter
在您的系统上编译时,检查您的
Python
发行版的
README

件,找到具体的指导说明。请保证编译后,启动您刚刚创建的
Python
解释器;否则,它
会像原来的没有安装
Tkinter
的老编译器一样的工作(事实上,它就是您原来的那个旧编
译器)。


19.1.3

Client/Se
r
v
er

Architectu
r
e

T
a
k
e

T
w
o

Client/Se
r
v
er
架构


I
n

th
e

earlie
r

chapte
r

o
n

networ
k

programming
,

w
e

introduce
d

th
e

notio
n

of
client/serve
r

computing
.

A

windowin
g

syste
m
i
s

anothe
r

exampl
e

o
f

a
software
serve
r
.

Thes
e

ru
n

o
n

a

machin
e

wit
h

a
n

attache
d

displa
y
,

suc
h

a
s

a

monito
r

of
so
m
e

sort
.

Ther
e

ar
e

clients
,

too

program
s

tha
t

requir
e

a

windowin
g

environ
-

men
t

t
o

e
xecute
,

als
o

know
n

a
s

GU
I

applications
.

Suc
h

application
s

canno
t

run
withou
t

a

window
s

system.

在前面关于网络编程的章节中,我们介绍了
client/serve
r
计算的定义。抽
口系统是软件服务器的另外一个例子。这些运行在一个带有像显示器
一类设备的的机器上,也
有客户端
---
需要一个窗口环境来运行的程
序,也被称作
GU
I
应用程序。这样的应用程序没有窗口系统不能运
行。

Th
e

architectur
e

become
s

eve
n

mor
e

interestin
g

whe
n

networkin
g

come
s

into
pla
y
.

Usuall
y

whe
n

a

GU
I

applicatio
n

i
s

executed
,

i
t

display
s

t
o

th
e

machin
e

that

i
t

starte
d

o
n

(vi
a

the

windowing

server),

but

i
t

is

possible

in

some

networked
windowing


environments
,


suc
h


a
s


th
e


X


W
indo
w

syste
m

o
n


Unix
,


t
o


choose
anothe
r

machine

s

windo
w

serve
r

t
o

displa
y

to
.

I
n

suc
h

situations
,

yo
u

ca
n

be
runnin
g

a

GU
I

progra
m

o
n

on
e

machine
,

bu
t

hav
e

i
t

displaye
d

o
n

a
nother!


当网络可以使用的时候,这个架构变得更加有意思。通常当
GUI
应用程
序执行的时候,它会显示在启动的机器上(通过窗口服务器),但是在
某些网络窗口环境中变得可能,例如
Unix
上的
X
窗口系统,可以选择其他
机器的窗口系统来显示。在这样的情况下,您可以在一台机器上运行
GUI
程序,但是在另外一台机器上显示它。




19.2


Tkinter

and

Python

P
r
o
gramming


Tkinter



P祴桯n

编程


19.2.1

Tkinter

Module:

Adding

Tk
to

your

Applicat
ions

Tkinter
模块:添加
Tk
到您的应用程
序中


So

what

do

you

need

to

do

to

have

Tkinter

as

part

of

your

application?

W
ell,
first

of

all,

it

is

not

necessary

to

have

an

application

alread
y
.

Y
ou

can

create

a
pure

GUI

if

you

want,

but

it

probably

isn

t

too

useful

without

some

underly
-

ing
software

that

does

something

interesting.

要把
Tkinter
作为您应用程序的一部分,得做什么呢?首先,没有必要
已经有一用程序。如果您想要,可以创建一个纯粹的
GUI
,但是如果
没有可以完成一些有意义的事情的底层软件,这可能很没有用。


There

are

basically

five

main

steps

that

are

required

to

get

your

GUI

up
and

running:


要使得您的
GUI
运行,需要完成下面基
本的五步:


1
.

Impor
t

th
e

Tkinter

modul
e

(o
r

from
Tkinter
import

*
).

2.

Create

a
top
-
level

windowing

object

that

contains

your
entire

GUI

application.


3.

Build

all

your
GUI

components

(and

functionality)

on

top

(or

“inside”)

of

your
top
-
level

windowing

o
bject.

4
.

Connec
t

thes
e

GU
I

component
s

t
o

th
e

underlyin
g

applicatio
n

code.

5.

Enter

the

main

event

loop.





1
.

导入
Tkinter
模块

(
或者

from
Tkinter
import

*
)


2.

创建一个顶层的窗口对象来容纳您整个的
GUI
应用程序。


3.

建立您所有的
GUI
组件
(
和功能
)

在您的顶层窗口对象的顶部
(
或者“内部”
)


4
.

把这些
GUI
组件和地称的应用程序
代码连接

5.

进入主事件循环。



Th
e

firs
t

ste
p

i
s

trivial
:

Al
l

GUI
s

tha
t

us
e

Tkinte
r

mus
t

impor
t

th
e

Tkinter

module
.

Gettin
g

acces
s

t
o

Tkinte
r

i
s

th
e

firs
t

ste
p

(se
e

Sectio
n

19.1.2).



19.2.2

Int
r
oduction

to

GUI
P
r
o
g
r
amming

GUI
编程介绍


Before

going

to

the

examples,

we

will

giv
e

you

a

brief

introduction

to

GUI
application

development

in

general.

This

will

provide

you

with

some

of

the
background

you

need

to

move
forward.

Setting

up

a

GUI

application

is

similar

to

an

artist

s

producing

a

painting.
Conventionall
y
,

there

is

a

single

canvas

onto

which

the

artist

must

put

all

the
work.

The

way

it

works

is

like

this:

Y
ou

start

with

a

clean

slate,

a

“top
-
level”
windowing

object

on

which

you

build

the

rest

of

your

components.

Think

of

it

as

a

foundation

to

a

house

or

the

easel

for

an

arti
st.

In

other

words,

you
have

to

pour

the

concrete

or

set

up

your

easel

before

putting

together

the
actual

structure

or

canvas

on

top

of

it.

In

Tkinte
r
,
this

foundation

is

known
as
the

top
-
level

window

object.

I
n

GU
I

programming
,

a

top
-
leve
l

roo
t

windowin
g

objec
t

contain
s

al
l

of
th
e

littl
e

windowin
g

object
s

tha
t

wil
l

b
e

par
t

o
f

you
r

complet
e

GU
I

appli
-

cation
.

Thes
e

ca
n

b
e

tex
t

labels
,

buttons
,

lis
t

boxes
,

etc
.

Thes
e

individual
littl
e


GU
I


component
s


ar
e


know
n

a
s


widget
s
.

S
o


whe
n


w
e

sa
y


creat
e


a
t
op
-
leve
l


windo
w
,

w
e

jus
t


mea
n


tha
t


yo
u


nee
d


suc
h


a

thin
g


a
s


a

place
wher
e

yo
u

pu
t

al
l

you
r

widgets
.

I
n

Python
,

thi
s

woul
d

typicall
y

loo
k

like
thi
s

line:


to
p

=

Tkinter.Tk(
)

#

o
r

jus
t

Tk(
)

wit
h

"
fro
m

Tkinte
r

impor
t

*"


The

object

returned

by

Tk
inter.Tk()

is

usually

referred

to

as

the

root
windo
w
,

hence

the

reason

why

some

applications

use

root

rather

than

top

to

indicate

as

such.

T
op
-
level

windows

are

those

that

show

up

standalone

as
part

of

your

application.

Y
ou

may

have

more

than

one

top
-
level

window

for
your

GUI,

but

only

one

of

them

should

be

your

root

windo
w
.

Y
ou

may

choose

to

completely

design

all

your

widgets

first,

then

add

the

real

functionalit
y
,

or
do

a

little

of

this

and

a

little

of

that

along

the

wa
y
.

(This

means

mixing

and
matching

s
teps

3
and

4
from

our

list.)

W
idgets

may

be

standalone

or

be

containers.

If

a

widget

“contains”

other
widgets,

it

is

considered

the

parent

of

those

widgets.

Accordingl
y
,

if

a

widget



is

“contained”

in

another

widget,

it

s

considered

a

child

of

the

parent,

the

pa
r
-

ent

being

the

next

immediate

enclosing

container

widget.

Usuall
y
,

widgets

have

some

associated

behaviors,

such

as

when

a

button

is
pressed,

or

text

is

filled

into

a

text

field.

These

types

of

user

behaviors

are
called

event
s
,

and

the

actions

that

the

GUI

takes

to

respond

to

such

events
are

known
as

callback
s
.

Actions

may

include

the

actual

button

press

(and

release),

mouse

move
-

ment,

hitting

the

RETURN

or

Enter

ke
y
,

etc.

All

of

these

are

known

to

the
system

literally

as

event
s
.

The

entire

system

of

events

that

occurs

from

the
beginning

to

the

end

of

a

GUI

application

is

what

drives

it.

This

is

known

as
event
-
driven

processing.

One

example

of

an

event

with

a
callback

is

a
simple

mouse

move.
Let

s

say
the

mouse

pointer

is

sitting

somewhere

on

top

of

your

GUI

application.

If
the


mouse


is


moved


to


another


part


of


your

application,


something


has


to
cause

the

movement

of

the

mouse

on

your

screen

so

that

it

looks

as

if

it

is
moving

to

another

location.

These

are

mouse

move

events

that

the

syst
em
must

process

to

give

you

the

illusion

(and

reality)

that

your

mouse

is

moving
across

the

windo
w
.

When

you

release

the

mouse,

there

are

no

more

events

to
process,

so

everything

just

sits

there

quietly

on

the

screen

again.

The

event
-
driven

processing

natu
re

of

GUIs

fits

right

in

with

client/server
architecture.

When

you

start

a

GUI

application,

it

must

perform

some

setup
procedure
s

t
o

prepar
e

fo
r

th
e

cor
e

execution
,

jus
t

a
s

whe
n

a

networ
k

server
ha
s

t
o

allocat
e

a

socke
t

an
d

bin
d

i
t

t
o

a

loca
l

address
.

Th
e

GU
I

application
mus
t


establis
h


al
l


th
e


GU
I


components
,


the
n


dra
w


(ak
a


rende
r


o
r


paint)
the
m

t
o

th
e

screen
.

T
k

ha
s

a

coupl
e

o
f

geometr
y

manager
s

tha
t

hel
p

posi
-

tio
n

th
e

widge
t

i
n

th
e

righ
t

place
;

th
e

mai
n

on
e

tha
t

yo
u

wil
l

us
e

i
s

called
Pack
,

ak
a

th
e

packe
r
.

Anothe
r

geometr
y

manage
r

i
s

Grid

thi
s

i
s

wher
e

you specif
y

GU
I

widget
s

t
o

b
e

place
d

i
n

gri
d

coordinates
,

an
d

Gri
d

wil
l

render
eac
h

objec
t

i
n

th
e

GU
I

i
n

thei
r

gri
d

position
.

Fo
r

us
,

w
e

wil
l

stic
k

wit
h

the
packe
r
.

Once

the

packer

has

determined

the

sizes

and

alignments

of

all

your

wid
-

gets,

it

will

then

place

them

on

the

screen

for

you.

When

all

of

the

widgets,
including


the


top
-
level


windo
w
,

finally


appear


on


your

screen,


your

GUI
application

then

enters

a

“serve
r
-
like”

infinite

loop
.

This

infinite

loop

involves
waiting

for

a

GUI

event,

processing

it,

then

going

back

to

wait

for

the

next
event.

The

final

step

we

described

above

says

to

enter

the

main

loop

once

all

the
widgets

are

read
y
.

This

is

the

“server”

infinite

loop

we

have

been

referring

to.
In

Tkinte
r
,
the

code

that

does

this

is:


Tkinter.mainloop()


This


is


normally

the


last

piece


of


sequential


code


your

program


runs. When

the

main

loop

is

entered,

the

GUI

takes

over

execution

from

there.

All
other


action


is


via


callbacks,


even


exiting


your

application.


When


you


pull
down

the

File

menu

to

click

on

the

Exit

menu

option

or

close

the

window
directl
y
, a
callback

must

be

invoked
to

end

your
GUI

application.



19.2.3

T
op
-
L
e
v
el

Wind
o
w:

Tkinter.Tk()

顶级窗口:
T歩湴e爮呫⠩


W
e

mentione
d

abov
e

tha
t

al
l

mai
n

widget
s

ar
e

buil
t

int
o

th
e

top
-
leve
l

window
object.

This

object

is

created

by

the

Tk

class

in

Tkinter

and

is

created

via

the
normal

instantiation:


>>>
import
Tkinter

>>> top = Tkinter.Tk()

W
ithin

this

wi
ndo
w
,

you

place

individual

widgets

or

multiple
-
component
pieces

together

to

form

your

GUI.

So

what

kinds

of

widgets

are

there?

W
e
will

now
introduce

the

Tk widgets.



19.2.4


Tk

Widgets


There

are

currently

15

types

of

widgets

in

Tk.

W
e

describe

these

widg
ets

in

T
able

19.1.

W
e

won

t

go

over

the

Tk

widgets

in

detail

as

there

is

plenty

of

good

docu
-

mentation

available

on

them,

either

from

the

Tkinter

topics

page

at

the

main
Python


W
eb


site


or


the


abundant


number


of


T
cl/Tk


printed


and


online resour
ces


(some


of


which

are


available


in


Appendix

B).

Howeve
r
,

we

will
present

several
simple

examples
to

help

you

get

started.



CORE

N
O
TE:

De
f
ault

a
r
guments

a
r
e

y
our

friend


GUI

d
e
velopment

r
eally

ta
k
es

advanta
g
e

of

de
f
ault

arguments

in

Python

beca
use

the
r
e

a
r
e

nume
r
ous

de
f
ault

actions

in

Tkinter

wid
g
et
s
.

Unless

you
kn
o
w

e
ve
r
y

single

option

available

to

you

f
or

e
ve
r
y

single

wid
g
et

you

a
r
e using,

it

s

best

to

sta
r
t

out

b
y

setting

only

the

pa
r
amete
r
s

you

a
r
e

a
w
a
r
e

of
an
d

lettin
g

th
e

syste
m

handl
e

th
e

r
est
.

Thes
e

de
f
ault
s

we
r
e

c
hose
n

ca
r
efull
y
.

I
f

yo
u

d
o

no
t

p
r
o
vid
e

thes
e

value
s
,

d
o

no
t

wor
r
y

abou
t

you
r

applications
appearing

odd

on

the

sc
r
een
.
They

we
r
e

c
r
eated

with

an

optimi
z
ed

set

of
de
f
aul
t

argument
s

a
s

a

g
ene
r
a
l

rul
e
,

an
d

onl
y

whe
n

yo
u

kn
o
w

h
o
w

t
o

e
xactly

customi
z
e

you
r

wid
g
et
s

shoul
d

yo
u

us
e

value
s

othe
r

tha
n

th
e

de
f
ault.


W
idget

Description


Button

Similar
to

a
Label

but

provides

additional

functionality

for
mouse

overs,
presses,

and

releases

as

well

as

keyboard

activity/
events


Canvas

Provides

ab
ility

to

draw

shapes

(lines,
ovals,

polygons,
rectangles);

can

contain

images
or

bitmaps


Checkbutton

Set

of

boxes

of

which
any

number

can

be

“checked” (similar
to

HTML

checkbox

input)


Entry

Single
-
line
text

field

with

which
to

collect

keyboard

input

(sim
ilar
to

HTML

text

input)


Frame

Pure

container

for

other

widgets


Label

Used

to

contain

text

or

images


Listbox

Presents

user

list

of

choices

to

pick
from


Menu

Actual

list

of

choices

“hanging”

from

a

Menubutton

that

the
user

can

choose

from


Menubutton

Pr
ovides

infrastructure

to

contain

menus

(pulldown,

cascading,
etc.)


Message

Similar
to

a
Label
,
but

displays

multi
-
line

text


Radiobutton

Set

of

buttons

of

which
only

one

can

be

“pressed”

(similar
to

HTML

radio

input)


Scale

Linear

“slider”

widget

providin
g
an

exact

value
at

current
setting;

with

defined

starting

and

ending

values


Scrollbar

Provides

scrolling

functionality

to

supporting

widgets,
i.e.,

Text
,
Canvas
,
Listbox
,
and

Entry


Text

Multi
-
line

text

field

with

which
to

collect

(or

display)

text

from
user

(similar
to

HTML

textarea
)


Toplevel

Similar
to

a
Frame
,
but

provides

a
separate

window

container


Example

19.1


Label

Widget

Demo

(
tkhello1.py
)


Ou
r

fi
r
s
t

Tkinte
r

exampl
e

i
s

.

.

.

wha
t

else
?

“Hell
o

W
orld!


I
n

pa
r
ticula
r
,

w
e

int
r
oduce
ou
r

fi
r
s
t

wid
g
et
,

th
e

Label
.


1

#!/usr/bin/env python

2

3

import
Tkinter

4

5

top = Tkinter.Tk()

6

label = Tkinter.Label(top, text='Hello World!')

7

label.pack()

8

Tkinter.mainloop()






19.3


Tkinter

Examples


19.3.1

Label

Widget

标签


In

Example

19.1,

we

present


tkhello1.py
,

the

Tkinter

version

of

“Hello
W
orld!”

In

particula
r
,

it

shows

you

how

a

Tkinter

application

is

set

up

and
highlights
the

Label

widget.

In

the

first

line,

we

create

our

top
-
level

windo
w
.

That

is

followed

by

our

Label

widget

containing

the

all
-
too
-
famous

string.

W
e

instruct

the

packer

to
manage

and

display

our

widget,

and

finally

call

mainloop()

to

run

our

GUI
application.


Figure


19

1

shows


what


you


will


see


when


you


run


this


GUI
application.



19.3.2

Button

Widget

按钮


The

next

example

is

pretty

much

the

same

as

the

first.

Howeve
r
,

instead

of

a
simple


text


label,


we

will


create


a

button


instead.


In


Example


19.2

is


the
source

code

for

tkhello2.py
.







Unix (twm)

Wind
o
ws

Figure

19

1


Tkinter

Label

wid
get

(
tkhello1.py
)

Example

19.2


Button

Widget

Demo

(
tkhello2.py
)


Thi
s

exampl
e

i
s

exactl
y

th
e

sam
e

a
s

tkhello1.p
y

excep
t

tha
t

r
athe
r

than
usin
g

a

Labe
l

wid
g
et
,

w
e

c
r
eat
e

a

Butto
n

wid
g
et.


1

#!/usr/bin/en
v

python

2

3

impor
t

Tkinter

4

5

to
p

=

Tkinter.Tk()

6

qui
t

=

Tkinter.Button(top
,

text='Hell
o

World!',

7

command=top.quit)

8

quit.pack()

9

Tkinter.mainloop()





Th
e

firs
t

fe
w

line
s

ar
e

identical
.

Thing
s

diffe
r

onl
y

whe
n

w
e

creat
e

th
e

But
-

to
n

widget
.

Ou
r

butto
n

ha
s

on
e

additiona
l

paramete
r
,

th
e

Tkinter.quit(
)

method
.


Thi
s


install
s


a

callbac
k


t
o


ou
r


butto
n


s
o


tha
t


i
f


i
t


i
s


presse
d


(and

released)
,


th
e


entir
e


applicatio
n


wil
l


exit
.


Th
e


fina
l


tw
o

line
s


ar
e


th
e


usual
pack(
)

an
d

enterin
g

o
f

th
e

mainloop()
.

Thi
s

simpl
e

butto
n

applicatio
n

is

show
n

i
n

Figur
e

19

2.



19.3.3

Label

and

Button

Widgets

标签和按钮


W
e

combine

tkhello1.py

and

tkhello2.py

into

tkhello3.py
,

a

script
that

has

both

a

label

and

a

button.

In

addition,

we

are

providing

more

param
-

eters

now

than

before

when

we

were

comfortable

using

all

the

default

argu
-

ments

that

are

automatically

s
et

for

us.

The

source

for

tkhello3.py

is

given

in

Example

19.3.

Besides


additional


parameters


for


the


widgets,

we

also


see


some


argu
-

ments

for

the

packe
r
.

The

fill

parameter

tells

the

packer

to

let

the

QUIT
butto
n


tak
e


u
p


th
e


res
t


o
f


th
e


horizonta
l


rea
l


estate
,


an
d


th
e


expand

Unix


Wind
o
ws

Figure

19

2

Tkinter

Label

widget

(
tkhello1.py
)

Example

19.3


Label

and

Button

Widget

Demo

(
tkhello3.py
)


Thi
s

exampl
e

f
eatu
r
e
s

bot
h

a

Labe
l

an
d

a

Butto
n

wid
g
et
.

Rathe
r

than
p
r
ima
r
ily

using

de
f
ault

a
rguments

when

c
r
eating

the

wid
g
et,

we

a
r
e

able

to

specify
mo
r
e

n
o
w

tha
t

w
e

kn
o
w

mo
r
e

abou
t

Butto
n

wid
g
et
s

an
d

h
o
w

t
o

configu
r
e

them.


1

#!/usr/bin/env python

2

3

import
Tkinter

4

top = Tkinter.Tk()

5

6

hello = Tkinter.Label(top, text='Hello World!')

7

hell
o.pack()

8

9

quit = Tkinter.Button(top, text='QUIT',

10

command=top.quit, bg='red', fg='white')

11

quit.pack(fill=Tkinter.X, expand=1)

12

13

Tkinter.mainloop()





parameter


directs


the


packer


to


visually


fill


out


the


entire


horizontal


land
-

scape,

stretching

the

button

to

the

left

and

right

sides
of

the

windo
w
.

A
s


yo
u


ca
n


se
e


i
n


Figur
e


19

3
,

withou
t


an
y


othe
r


instruction
s


t
o


the
packe
r
,

th
e

widget
s

ar
e

place
d

verticall
y

(o
n

to
p

o
f

eac
h

other)
.

Horizontal
placemen
t


require
s


crea
tin
g


a

ne
w


Fram
e

objec
t


wit
h


whic
h

t
o


ad
d


the
buttons
.


Tha
t


fram
e


wil
l


tak
e


th
e


plac
e


o
f


th
e


paren
t


objec
t


a
s


a

single
chil
d

objec
t

(se
e

th
e

button
s

i
n

th
e

listdir.p
y

module
,

Exampl
e

19.
6

in Sectio
n

19.3.6).

Unix


Wind
o
ws

Figure

19

3



Tkinter

Label

and

Button

widgets

(
tkhello3.py
)

19.3.4

Labe
l
,

Butto
n
,

and

Scale

Widgets

标签、按钮和


坩dgets


Our


final


trivial


example,


tkhello4.py
,

involves


the


addition


of


a

Scale
widget.

In

particula
r
,

the

Scale

is

used

to

interact

with

the

Label

widget
.
The


Scale

slider


is


a

tool


which

controls


the


size


of


the


text


font


in


the
Label

widget.

The

greater

the

slider

position,

the

larger

the

font,

and

the
same


goes


for


a

lesser


position,


meaning


a

smaller


font.


The


code


for
tkhello
4.py

is

given

in

Example

19.4.





Exampl
e

19.
4


Label
,

Button
,

an
d

Scal
e

Demo

(
tkhello4.p
y
)


Our

final

int
r
oducto
r
y

wid
g
et

example

int
r
oduces

the

Scale

wid
g
et

and

highlights
h
o
w

wid
g
ets

can

“com
m
unicate”

with

ea
c
h

other

using

callba
c
ks

[su
c
h

as
resize()
]
.
The

text

in

the

Label

wid
g
et

is

af
f
ected

b
y

actions

ta
k
en

on

the
Scale

wid
g
et.


1

#!/usr/bin/env python

2

3

from
Tkinter
import
*

4

5

def
resize(ev=None):

6

label.config(font='Helvetica
-
%d bold' %
\

7

scale.get())

8

9

top = Tk()

10

top.geometry('250x150
')

11

12

label = Label(top, text='Hello World!',

13

font='Helvetica
-
12 bold')

14

label.pack(fill=Y, expand=1)

15

16

scale = Scale(top, from_=10, to=40,

17

orient=HORIZONTAL, command=resize)





22

command=top.quit, activeforeground='white',

23

acti
vebackground='red')

24

quit.pack()

25

26

mainloop()



Ne
w

feature
s

o
f

thi
s

scrip
t

includ
e

a

resize(
)

callbac
k

functio
n

(line
s

5

7),
w
hich

is

attached

to

the

Scale
.

This

is

the

code

that

is

activated

when

the
slider

on

the

Scale

is

moved,

resizing
the

s
ize

of

the

text

in

the

Label
.

W
e

also

define

the

size

(250



150)

of

the

top
-
level

window

(line

10).

The
final

difference

between

this

script

and

the

first

three

is

that

we

import

the
attributes


from


the


Tkinter

module


into


our


namespace


with



from
Tkinte
r

impor
t

*
.


Althoug
h

no
t

recommende
d

becaus
e

i
t

“po
llutes”
your

namespace,


we

do


it


here


mainly


because


this


application


involves


a
great

number

of

references

to

Tkinter

attributes.

This

would

require

use

of
their

fully

qualified

names

for

each

and

every

attribute

access.

By

using

the
undesired

shortcut,

we

are

able

to

access

attributes

with

less

typing

and

have
code

that

is

easier

to

read,

18

scale.set(12)


19

scale.pack(fill=X,

expand=1)

20



21

quit = Button(top,

text='QUIT',


at

some

cost.

As

you

can

see

from

Figure

19

4,

both

the

slider

mechanism

as

well

as

the
current

set

value

show

up

in

the

main

part

of

the

windo
w
.

Figure

19
-
4

shows
th
e

stat
e

o
f

th
e

GU
I

afte
r

th
e

use
r

move
s

th
e

scale/slide
r

t
o

avalu
e

o
f

36.










Unix














Wind
o
ws








Figure

19

4


Tkinter

Label
,

Button
,

and

Scale

widgets

(
tkhello4.py
)

A
s

you

can

see

from

the

code,

the

initial

setting

for

the

scal
e

when

the

appli
-

cation

starts

is

12
(line

18).



19.3.5

P
a
r
tial

Function

Application

Example

部分函数应用举例


Befor
e

lookin
g

a

longe
r

GU
I

application
,

w
e

wante
d

t
o

revie
w

th
e

Partia
l

Func
-

tio
n

Applicatio
n

(P
F
A
)

a
s

introduce
d

bac
k

i
n

Sectio
n

11.7.
3

o
f

Chapte
r

11.

P
F
As

were

added

to

Python

in

version

2.5

and

are

one

piece

in

a

series

of
significant

improv
ements

in

functional

programming.

P
F
As

allow

you

to

“cache”

function

parameters

by

effectively

“freezing”
those


predetermined


arguments,


and


then


at


runtime,


when


you


have


the
remaining


arguments


you


need,


you


can


thaw


them


out,


send


in


the


final
arguments,

and

have

that

function

called

with

all

parameters.

Best

of

all,

P
F
As

are

not

limited

to

just

functions.

They

will

work

with

any

“callable,”

any

object

that

has

a

functional

interface

just

by

using

parenthe
-

ses,

i.e.,

classes,

metho
ds,

or

callable

instances.

The

use

of

P
F
As

fits

perfectly
into

a

situation

where

there

are

many

callables

and

many

of

the

calls

feature
the

same

arguments

over

and

over

again.

GUI

programming

makes

a

great

use

case

because

there

is

good

probabil
-

ity


that


you


want


some


consistency

in


GUI


widget


look
-
and
-
feel,


and


this
consistency

comes

about

when

the

same

parameters

are

used

to

create

like
objects.

W
e

are

now

going

to

present

an

application

where

multiple

buttons
will

have

the

same

foreground

and

background

colors.

It

would

be

a

waste

of
typing

to

give

the

same

arguments

to

the

same

instantiators

every

time

we
wanted

a

slightly

different

button:

the

foreground

and

background

colors

are
the

same,

but

only

the

text

is

slightly

different.

W
e

ar
e

goin
g

t
o

us
e

traffi
c

roa
d

sign
s

a
s

ou
r

exampl
e

wit
h

ou
r

application
attempt
s

creatin
g

textua
l

version
s

o
f

roa
d

sign
s

b
y

dividin
g

the
m

u
p

int
o

vari
-

ou
s

categorie
s

o
f

sig
n

type
s

lik
e

critical
,

warning
,

o
r

informationa
l

(jus
t

like
loggin
g

levels)
.

Th
e

typ
e

o
f

th
e

sig
n

determine
s

thei
r

colo
r

layou
t

whe
n

the
sign
s

ar
e

created
.

Fo
r

example
,

critica
l

sign
s

hav
e

th
e

tex
t

i
n

brigh
t

re
d

wit
h

a
whit
e

backdrop
,

warnin
g

sign
s

ar
e

i
n

blac
k

tex
t

o
n

a

goldenro
d

background,
an
d


informationa
l


o
r


regulator
y


sign
s


featur
e


b
lac
k

tex
t


o
n


a

whit
e


back
-

ground
.

W
e

hav
e

th
e

“D
o

No
t

Enter


an
d


W
ron
g

W
ay


signs
,

whic
h

ar
e

both

“critical,


plu
s

“Mergin
g

T
raffic


an
d

“Railroa
d

Crossing,


bot
h

o
f

whic
h

are
warnings
.

Finall
y
,

w
e

hav
e

th
e

regulator
y

“Spee
d

Limit


an
d

“On
e

W
ay


sig
ns.
The


application


creates


the


“signs,”


which

are


just


buttons.


When


users
pres
s


th
e


buttons
,


the
y


jus
t


po
p


u
p


th
e


correspondin
g


T
k

dialog
,


critical/
erro
r
,

warning,

or

informational.

It

is

not

too

exciting,

but

how

the

buttons
are

bu
ilt

is.

Y
ou

will

find
our

application

featured

here

in

Example

19.5.

Example

19.5


Road

Signs

P
F
A
GUI

Application

(
pfaGUI2.py
)


C
r
eate

r
oad

signs

with

the

app
r
op
r
iate

f
o
r
eg
r
ound

and

ba
c
kg
r
ound

colo
r
s

based
on

sign

typ
e
.

Use

PFAs

to

help

“templati
z
e”

common

GUI

pa
r
amete
r
s
.


1

#!/usr/bin/env python

2

3

from
functools
import
partial
as
pto

4

from
Tkinter
import
Tk, Button, X

5

from
tkMessageBox
import
showinfo, showwarning, showerror

6

7

WARN = 'warn'

8

CRIT = 'crit'

9

REGU = 'regu'

10

11

SIGNS = {

12

'do no
t enter': CRIT,

13

'railroad crossing': WARN,

14

'55
\
nspeed limit': REGU,

15

'wrong way': CRIT,

16

'merging traffic': WARN,

17

'one way': REGU,

18

}

19

2
0

critC
B

=

lambda
:

showerror('Error'
,

'Erro
r

Butto
n

Pressed!')

21

warnCB =
lambda
: showwarning('W
arning',

22

'Warning Button Pressed!')

23

infoCB =
lambda
: showinfo('Info', 'Info Button Pressed!')

24

25

top = Tk()

26

top.title('Road Signs')

27

Button(top, text='QUIT', command=top.quit,

28

bg='red', fg='white').pack()

29

30

MyButton = pto(But
ton, top)

3
1

CritButto
n

=

pto(MyButton
,

command=critCB
,

bg='white'
,

fg='red')

32

WarnButton

= pto(MyButton,

command=warnCB,

bg='goldenrod1')

33

ReguButton

= pto(MyButton,

command=infoCB,

bg='white')

34




35

for
eachSign
in
SIGNS:

36

signType =
SIGNS[eachSign]

3
7

cm
d

=

'%sButton(text=%r%s).pack(fill=X
,

expand=True)
'

%

(

38

signType.title(), eachSign,

39

'.upper()
'

i
f

signTyp
e

=
=

CRI
T

els
e

'
.title()')

40

eval(cmd)

41

42

top.mainloop()

Figure

19

5

Road

signs

P
F
A

GUI

application

on

XDa
r
win

in

MacO
S

X

(
pfaGUI2.py
)



When

you

execute

this

application,

you

will

get

a
GUI

that

will

look

some
-

thing

like

Figure

19.5.



Line
-
b
y
-
Line

Explanation


Lines

1

18

W
e


begin


our


application


by


importing


functional.partial(
)
,

a

few
Tkinter

attributes,


and


the


Tk

dialogs


(lines

1

5).

Next,


we

define


some
signs

along
with

their

categories

(lines 7

18).


Lines

20

28

The

Tk

dialogs

are

assigned

as

button

callbacks,

which

we

will

use

for

each
button

created

(lines

20

23).

W
e

then

launch

Tk,

set

the

titl
e,

and

create

a
QUIT

button

(lines 25

28).


Lines

30

33

These

lines

represent

our

P
F
A

magic.

W
e

use

two

levels

of

P
F
A.

The

first
t
emplatize
s

th
e

Butto
n

clas
s

an
d

th
e

roo
t

windo
w

top
.

Wha
t

thi
s

doe
s

i
s

that
ever
y

tim
e


w
e

cal
l


MyButton
,

i
t


wil
l


cal
l


Butto
n

(
Tkinter.Button()
creates

a

button.)

with

top

as

its

first

argument.

W
e

have

“frozen”

this

into
MyButt
on
.

The

second

level

of

P
F
A

is

where

we

use

our

first

one,


MyButton
,

and
templatize

that
.

W
e

create

separate

button

types

for

each

of

our

sign

cat
ego
-

ries.


When


users


create


a

critical

button


CritButton

(by


calling


it,


e.g.,
CritButton()
)
,

i
t


wil
l


the
n


cal
l


MyButto
n

alon
g

wit
h


th
e


appropriate
butt
on

callback

and

background

and

foreground

colors,

which

means

calling
Button

with

top
,

callback,

and

colors.

Do

you

see

how

it

unwinds

and

goes
down

the

layers

until

at

the

very

bottom,

it

has

the

call

that

you

would

have

originally

had

to

make

if

this

feature

did

not

exist

yet?

W
e

repeat

with

Warn
-

Button

and

ReguButton
.


Lines

35

42

W
it
h

th
e

setu
p

completed
,

w
e

loo
k

a
t

ou
r

lis
t

o
f

sign
s

an
d

creat
e

them
.

W
e

put
togethe
r


a

Pytho
n


evaluatabl
e


strin
g


consistin
g

o
f


th
e


correc
t


butto
n


name,
pas
s

i
n

th
e

butto
n

labe
l

a
s

th
e

tex
t

argument
,

an
d

pack(
)

it
.

I
f

i
t

i
s

a

critical
sign
,

the
n

w
e

CAPI
T
ALIZ
E

th
e

butto
n

text
,

otherwis
e

w
e

titlecas
e

it
.

Thi
s

last
bi
t

i
s

don
e

i
n

lin
e

39
,

demonstratin
g

anothe
r

featur
e

introduce
d

i
n

Pytho
n

2.5,
th
e

temporar
y

operato
r
.
The
n

w
e
tak
e

eac
h

butto
n

creatio
n

strin
g

an
d

execute

i
t

wit
h

eval()
,
creatin
g

th
e

butt
on
s

on
e

a
t

a
tim
e

an
d

resultin
g

i
n

th
e

graphic
see
n

previousl
y
.

Finall
y
,

w
e

star
t

th
e

GU
I

b
y

enterin
g

th
e

mai
n

even
t

loop.

This

application

uses

several

Python

2.5

features,

so

you

will

not

be

able

to
run

this

with

an

older

version.


19.3.6


Intermediate

T
kinter

Example


W
e

conclud
e

thi
s

sectio
n

wit
h

a

large
r

example
,

listdir.py
.

Thi
s

application

i
s

a

director
y

tre
e

traversa
l

tool
.

I
t

start
s

i
n

th
e

curren
t

director
y

an
d

provide
s

a

fil
e

listing
.

Double
-
clickin
g

o
n

an
y

othe
r

director
y

i
n

th
e

lis
t

cause
s

th
e

t
oo
l

to
chang
e

t
o

th
e

ne
w

director
y

a
s

wel
l

a
s

replac
e

th
e

origina
l

fil
e

listin
g

wit
h

the
file
s

fro
m

th
e

ne
w

director
y
.

Th
e

sourc
e

cod
e

i
s

give
n

a
s

Exampl
e

19.6.



Example

19.6


File

System

T
r
a
v
ersal

GUI

(
listdir.py
)


This

slightly

mo
r
e

advanced

GUI

expands

on

the

use

of

wid
g
et
s
,

adding

listb
o
x
e
s
,
text

ent
r
y

field
s
,

and

sc
r
ollba
r
s

to

our

r
epe
r
toi
r
e
.
The
r
e

a
r
e

also

a

g
ood

number

of
callba
c
ks

su
c
h

as

mouse

c
li
c
k
s
,

k
ey

p
r
esse
s
,

and

sc
r
ollbar

action.


1

#!/usr/bin/env python

2

3

import
os

4

from
time
import
sleep

5

from
Tkinter
import
*

6

7

class
DirList(object):

8

9

def
__init__(self, initdir=None):

10

self.top = Tk()

11

self.label = Label(self.top,

12

text='Directory Lister v1.1')

13

self.label.pack()

14

15

self.cwd = StringVar(self.top)

16


Example

19.6

File

System

T
r
a
v
ersal

GUI

(
listdir.py
)

(conti
n
ued)


17

self.dirl = Label(self.top, fg='blue',

18

font=('Helvetica', 12, 'bold'))

19

self.dirl.pack()




23

self.dirsb.pack(side=RIGHT, fill=Y)

24

self.dir
s

=

Listbox(self.dirfm
,

height=15,

25

width=50
,

yscrollc
ommand=self.dirsb.set)

26

self.dirs.bind('<Double
-
1>'
,

self.setDirAndGo)

27

self.dirsb.config(command=self.dirs.yview)

28

self.dirs.pack(side=LEFT, fill=BOTH)

29

self.dirfm.pack()

30

31

self.dirn = Entry(self.top, width=50,

32

textvariable=self.cwd)

33

sel
f.dirn.bind('<Return>', self.doLS)

34

self.dirn.pack()

35

36

self.bfm = Frame(self.top)

37

self.cl
r

=

Button(self.bfm
,

text='Clear
'
,

38

command=self.clrDir,

39

activeforeground='white',

40

activebackground='blue')

41

self.ls = Button(self.bfm,

42

text='Lis
t Directory',

43

command=self.doLS,

44

activeforeground='white',

45

activebackground='green')

46

self.qui
t

=

Button(self.bfm
,

text='Quit',

47

command=self.top.quit,

48

activeforeground='white',

49

activebackground='red')

50

self.clr.pack(side=LEFT)

51

self
.ls.pack(side=LEFT)

52

self.quit.pack(side=LEFT)

53

self.bfm.pack()

20



21

self.dirfm

= Frame(self.top)

22

self.dirsb

= Scrollbar(self.dirfm)


54

55

if
initdir:

56

self.cwd.set(os.curdir)

57

self.doLS()

58

59

def
clrDir(self, ev=None):

60

self.cwd.set('')

61

62

def
setDirAndGo(self, ev=None):

63

self.last = self.cwd.get()

64

se
lf
.dirs.config(selectbackground='re
d'
)

65

chec
k

=

self.dirs.get(self.dirs.curselection())

66

if not
check:

Example

19.6

File

System

T
r
a
v
ersal

GUI

(
listdir.py
)

(conti
n
ued)


67

check = os.curdir

68

self.cwd.set(check)

69

self.doLS()

70



73

tdir = self.cwd.ge
t()

74

if not
tdir: tdir = os.curdir

75

76

if not
os.path.exists(tdir):

77

error = tdir + ': no such file'

78

elif not
os.path.isdir(tdir):

79

error = tdir + ': not a directory'

80

81

if
error:




85

86

and
self.last):

87

self.last = os.curdir

88

self.cwd
.set(self.last)

89

self.dirs.config(
\

90

selectbackground='LightSkyBlue')

91

self.top.update()

92

return

93

94

self.cwd.set(
\

95

'FETCHING DIRECTORY CONTENTS...')

96

self.top.update()

97

dirlist = os.listdir(tdir)

98

dirlist.sort()

99

os.chdir(tdir)

100

se
lf.dirl.config(text=os.getcwd())

101

self.dirs.delete(0, END)

102

self.dirs.insert(END, os.curdir)

103

self.dirs.insert(END, os.pardir)

104

for
eachFile
in
dirlist:

105

self.dirs.insert(END, eachFile)

106

self.cwd.set(os.curdir)

107

self.dirs.config(
\

108

selectbackground='LightSkyBlue')

109

110

def
main():

111

d = DirList(os.curdir)

112

mainloop()

113

114

if
__name__ == '__main__':

115

main()



Wind
o
ws

71

def

doLS(self, ev=None):

72


error = ''


82

self.cwd.set(error)


83

self.top.update()


84

slee
p(2)



if not
(hasattr(self, 'last')

\





Figure

19

6

List

directo
r
y

GUI

application

in

W
indows

(
listdir.py
)



I
n


Figur
e


19

6
,

w
e

presen
t


w
ha
t


thi
s


GU
I


look
s


lik
e


i
n


a

W
indows

environment.

The

Unix

version
of

this

application

is

given

in

Figure

19

7.


Line
-
b
y
-
Line

Explanation


Lines

1

5

These

first

few

lines

contain

the

usual

Unix

startup

line

and

importation

of
the


os

module,


the


t
ime.sleep()

function,


and


all


attributes


of


the
Tkinter

module.


Lines

9

13

Thes
e


line
s


defin
e


th
e


constructo
r


fo
r


th
e


DirLis
t

class
,


a
n


objec
t


that
r
e
p
resents

our

application.

The

first

Label

we

create

contains

the

main

title

of

the

applica
tion

and

the

version
numbe
r
.


Figure

19

7


List

directo
r
y

GUI

application

in

Unix

(
listdir.py
)



Unix





Lines

15

19

W
e

declar
e

a

T
k

variabl
e

name
d

cw
d

t
o

hol
d

th
e

nam
e

o
f

th
e

director
y

we
ar
e

on

w
e

wil
l

se
e

wher
e

thi
s

come
s

i
n

hand
y

late
r
.

Anothe
r

Lab
e
l

i
s

cre
-

ated

to

display

the

name

of

the

current

director
y
.


Lines

21

29

This

section

defines

the

core

part

of

our

GUI,

(the

Listbox
)

dirs,

which
contain

the

list

of

files

of

the

directory

that

is

being

listed.

A

Scrollbar

is
employed

to

allow

the

user

t
o

move

through

a

listing

if

the

number

of

files
exceeds

the

size

of

the

Listbox
.

Both

of

these

widgets

are

contained

in

a
Fram
e

widget
.


Listbo
x

entrie
s


hav
e


a

callbac
k


(
setDirAndGo
)

tie
d


to
the
m

usin
g

th
e

Listbox

bind()

method.

Bindin
g

mean
s

t
o

ti
e

a

keystroke
,

mous
e

action
,

o
r

som
e

othe
r

even
t

t
o

a
callbac
k

t
o

b
e

execute
d

whe
n

suc
h

a
n

even
t

i
s

generate
d

b
y

th
e

use
r
.

setDir
-

AndGo(
)

wil
l


b
e


calle
d


i
f


an
y


ite
m


i
n


th
e


Listbo
x

i
s


doubleclicked
.


The
Scrollba
r

i
s


tie
d


t
o


th
e


Listbo
x

b
y


call
in
g


th
e


Scrollbar.config()
method.


Lines

31

34

W
e


the
n


creat
e


a

tex
t


Entr
y

fiel
d


fo
r


th
e


use
r


t
o


ente
r


th
e


nam
e


o
f


the
director
y

h
e

o
r

sh
e

want
s

t
o

travers
e

an
d

se
e

it
s

file
s

liste
d

i
n

th
e

Listbox
.
W
e

add

a

RETURN

or

Enter

key

binding

to

t
his

text

entry

field

so

that

the
user


can


hit


RETURN


as


an


alternative


to


pressing


a

button.


The


same applies

for

the

mouse

binding

we

saw

above

in

the

Listbox
.

When

the

user
doubleclicks

on

a

Listbox

item,

it

has

the

same

effect

as

the

user

s

entering
th
e


director
y


nam
e


manuall
y

int
o


th
e


tex
t


Entr
y

fiel
d


an
d


pressin
g


the

“go


button.


Lines

36

53

W
e

then

define

a

Button

frame

(
bfm
)

to

hold

our

three

buttons,

a

“clear”
button

(
clr
),

a

“go”

button

(
ls
),

and

a

“quit”

button

(
quit
).

Each

button
has

its own
different

configuration

and

callbacks,

if

pressed.


Lines

55

57

The

final

part

of

the

constructor

initializes

the

GUI

program,

starting

with
the

current

working

director
y
.


Lines

59

60

Th
e


clrDir(
)

metho
d


clear
s


th
e


cw
d

T
k

strin
g


v
ariable
,

whic
h

contains
th
e

curren
t

director
y

tha
t

i
s

“active.


Thi
s

variabl
e

i
s

use
d

t
o

kee
p

trac
k

of
wha
t

director
y

w
e

ar
e

i
n

and
,

mor
e

important
,

help
s

kee
p

trac
k

o
f

th
e

pre
-

viou
s

director
y

i
n

cas
e

error
s

arise
.

Y
o
u

wil
l

notic
e

th
e

e
v

variable
s

i
n

th
e
callbac
k

function
s

wit
h

a

defaul
t

valu
e

o
f

None
.

An
y

suc
h

value
s

woul
d

be
passe
d

i
n

b
y

th
e

windowin
g

system
.

The
y

ma
y

o
r

ma
y

no
t

b
e

use
d

i
n

your
callback.


Lines

62

69

Th
e


setDirAndGo(
)

metho
d


set
s


th
e


director
y


t
o


travers
e


t
o


an
d


issues
the

cal
l

to

the

method

that

makes

it

all

happen,

doLS()
.


Lines

71

108

doLS()

is,

by

fa
r
,

the

key

to

this

entire

GUI

application.

It

performs

all

the
safety

checks

(e.g.,

is

the

destination

a

directory

and

does

it

exist?).

If

there

is
an

erro
r
,
the

last
directory

is

reset

to

be

the

current

director
y
.
If

all

goes

well,

i
t

call
s

os.listdir(
)

t
o

ge
t

th
e

actua
l

se
t

o
f

file
s

an
d

replace
s

th
e

listing

i
n

the

Listbox
.

While

the

background

work

is

going

on

to

pull

in

the

new

directory

s

information,

the

highlighted

blue

ba
r

becomes

bright

red.

When

the

new

directory

has

been

installed,

it

reverts

to

blue.

Lines

110

115

The

last

pieces

of

code

in

listdir.py

represent

the

main

part

of

the

code.
main()

is

executed

only

if

this

script

is

invoked

directl
y
,

and

when

main()
runs
,


i
t


create
s


th
e


GU
I


application
,


the
n


call
s


mainloop(
)

t
o


star
t


the
GUI,

which
is

passed

control

of

the

application.

W
e

leave

all

other

aspects

of

the

application

as

an

exercise

to

the

reade
r
,
recommending

that

it

is

easier

to

view

the

entire

appl
ication

as

a

combination

of

a

set

of

widgets

and

functionalit
y
.

If

you

see

the

individual

pieces

clearl
y
,
then

the

entire

script

will

not

appear

as

daunting.

W
e

hope

that

we

have

given

you

a

good

introduction

to

GUI

programming
with

Python

and

Tkinte
r
.

Rem
ember

that

the

best

way

to

get

familiar

with
Tkinter


programming


is


by


practicing


and


stealing


a

few


examples!


The
Python


distribution


comes


with


a

large


number


of


demonstration


applica
-

tions

that

you

can

stud
y
.

If

you

download

the

sour
ce

code,

you

will

find

Tkinter

demo

code

in

Lib/
lib
-
tk
,

Lib/idlelib
,

and


Demo/tkinter
.

If


you


have


installed


the
W
in32

version

of

Python

and

C:
\
Python2x
,

then

you

can

get

access

to

the
dem
o


cod
e


i
n


Lib
\
lib
-
t
k

an
d


Lib
\
idlelib
.

Th
e


latte
r


dir
ector
y


con
-

tain
s

th
e

mos
t

significan
t

sampl
e

Tkinte
r

application
:

th
e

IDL
E

ID
E

itself.
For

further

reference,

there

are

several

books

on

Tk

programming,

one

spe
-

cifically

on

Tkinte
r
.




19.4

Brief

T
our
of

Other

GUIs

其他
G啉
简介


W
e


hope


to


eventually

develop


an


independent


chapter


on


general


GUI
development

using

many

of

the

abundant

number

of

graphical

toolkits

that
exist

under

Python,

but

alas,

that

is

for

the

future.

As

a

prox
y
,

we

would

like

to


present


a

single

simple


GUI


application


written


using

four


of


the


more
popular

and

available

toolkits

out

there:

T
ix

(Tk

Interface

eXtensions),

Pmw

(Python


Mega
W
idgets


Tkinter


extension),


wxPython


(Python


binding


to
wx
W
idgets),

and

PyGTK

(Python

bind
ing

to

GT
K

).

Links

to

where

you

can
get

more

information

and/or

download

these

toolkits

can

be

found

in

the

ref
-

erence

section

at

the

end

of

this

chapte
r
.

The

Tix

module

is

already

available

in

the

Python

standard

librar
y
.

Y
o
u
mus
t

downloa
d

th
e

others
,

whic
h

ar
e

t
hir
d

part
y
.

Sinc
e

Pm
w

i
s

jus
t

a
n

extension

t
o

Tkinte
r
,

i
t

i
s

th
e

easies
t

t
o

instal
l

(jus
t

extrac
t

int
o

you
r

sit
e

packages)
.

wxPython
an
d

PyGT
K

involv
e

th
e

downloa
d

o
f

mor
e

tha
n

on
e

fil
e

an
d

buildin
g

(unless
yo
u

op
t

fo
r

th
e

W
in3
2

version
s

wher
e

binarie
s

ar
e

usuall
y

available)
.

Onc
e

the
toolkit
s

ar
e

installe
d

an
d

verified
,

w
e

ca
n

begin
.

Rathe
r

than

just

sticking

with


the

widgets

we’ve

already

seen

in

this

chapte
r
,

we’d

like

to

introduce

a

few

more

complex widgets
for

these

examples.

I
n


additio
n


t
o


th
e


La
be
l

an
d


Butto
n

widget
s

w
e

hav
e


see
n


before
,


we woul
d

lik
e


t
o


introduc
e


th
e


Contro
l

o
r


SpinButto
n

an
d


ComboBox
.

The
Contro
l


widge
t


i
s


a

combinatio
n


o
f


a

tex
t


widge
t


wit
h


a

valu
e

insid
e


being

“controlled


o
r

“spu
n

u
p

o
r

down


b
y

a

se
t

o
f

arro
w

button
s

clos
e

b
y
,

an
d

the
ComboBo
x

i
s

usuall
y

a

tex
t

widge
t

an
d

a

pulldow
n

men
u

o
f

option
s

wher
e

the
currentl
y

activ
e

o
r

selecte
d

ite
m

i
n

th
e

lis
t

i
s

displaye
d

i
n

th
e

tex
t

widget.

Our

application

is

fairly

basic:

pairs

of

animals

are

being

mov
ed

around,
and

the

number

of

total

animals

can

range

from

a

pair

to

a

dozen

max.

The
Control

is

used

to

keep

track

of

the

total

number

while

the

ComboBox

is

a
m
en
u

containin
g

th
e

variou
s

type
s

o
f

animal
s

tha
t

ca
n

b
e

selected
.

I
n

Figur
e

19

8,
eac
h

image

sho
ws

the

state

of

the

GUI

application

immediately

after

launch
-

ing.

Note

that

the

default

number

of

animals

is

two,

and

no

animal

type

has
been

selected

yet.

Things

are

different

once

we

start

to

play

around

with

the

application,

as
evidenced

in

Figure

19

9

after

we

have

modified

some

of

the

elements

in

the
T
ix

application.

Belo
w
,

you

will

find

the

code

for

all

four

versions

of

our

GUI.

Y
ou

will

note
that

although

relatively

simila
r
,

each

one

differs

in

its

own

special

wa
y
.

Also,
we

use

the

.pyw

extension

to

suppress

the

popping

up

of

the

Dos

command

or

terminal

windo
w
.





Tix





Pmw

PyGTK



wxPython

Figure

19

8


Application

using

various

GUIs

under

W
in32

(
animal*.pyw
)





Tix

Figure

19

9


Afte
r

modifyin
g

th
e

T
i
x

GUI

versio
n

o
f
ou
r

applicatio
n

(
ani
malTix.pyw
)



19.4.1


Tk

Inter
f
ace

eXtensions

(Tix)


W
e

start

with

an

example

(Example

19.7)

of

using

the

Tix

module.

T
ix

is

an
extension


library

for


Td/T


that


adds


many

new


widgets,

image


types,


and
other

commands

that

keep

Tk

a

viable

GUI

deve
lopment

todkit.

Let

s

take
look

at

how
to

use

T
ix

with

Python.



Example

19.7


Tix

GUI

Demo

(
animalTix.pyw
)


Our

fi
r
st

example

uses

the

Tix

modul
e
.

Tix

comes

with

Python!


1

#!/usr/bin/env python

2

3

from
Tkinter
import
Label, Button, END

4

from
Tix
import

Tk, Control, ComboBox

5

6

top = Tk()

7

top.tk.eval('package require Tix')

8

9

lb = Label(top,

10

text='Animals

(in

pairs;

min:

pair,

max:

dozen)')

11

lb.pack()

12

13

ct = Control(top, label='Number:',

14

integer=True, max=12, min=2, value=2, step=2)

1
5

ct.label.config(font='Helvetica
-
14 bold')

16

ct.pack()

17

18

cb = ComboBox(top, label='Type:', editable=True)

19

for
animal
in
('dog', 'cat', 'hamster', 'python'):

20

cb.insert(END, animal)

21

cb.pack()

22

23

qb = Button(top, text='QUIT',

24

command=top.quit, bg='red', fg='white')

25

qb.pack()

26

27

top.mainloop()


Line
-
b
y
-
Line

Explanation


Lines

1

7

This


is


all


the


setup


code,


module


imports,


and


basic

GUI


infrastructure. Line

7
asserts

that

the

Tix

module

is

available

to

the

application.


Lines

8

27

Thes
e


line
s


creat
e


al
l


th
e


widgets
:


Labe
l

(line
s

9

11)
,

Contro
l

(lines

13

16)
,

ComboBo
x

(line
s

18

21)
,

an
d

qui
t

Butto
n

(line
s

23

25)
.

Th
e

con
-

structors

and

arguments

for

the

widgets

are

fairly

self
-
explanatory

and

do

not

r
equire

elaboration.

Finall
y
, we
enter

the

main

GUI

event

loop

in

line

27.



19.4.2


Python

Me
g
aWidgets

(PMW)


Next

we

take

a

look

at

Python

Mega
W
idgets

as

shown

in

Example

19.8.

This
module

was

created

to

address

the

aging

Tkinte
r
.

It

basically

helps

the

e
xtend
its
longevity

by

adding

more

modern

widgets
to

the

GUI

palette.

Th
e

Pm
w

exampl
e

i
s

s
o

simila
r

t
o

ou
r

Ti
x

exampl
e

tha
t

w
e

leav
e

line
-
by
-

lin
e

analysi
s

t
o

th
e

reade
r
.

Th
e

lin
e

o
f

cod
e

tha
t

differ
s

th
e

mos
t

i
s

th
e

con
-

structo
r

fo
r

th
e

contro
l

widget
,

t
h
e

Pm
w

Counter
.

I
t

provide
s

fo
r

entr
y

val
-

idation
.


Instea
d


o
f


specifyin
g


th
e


smalles
t


an
d


larges
t


possibl
e


value
s


as
keywor
d

argument
s


t
o


th
e


widge
t


constructo
r
,

Pm
w


use
s


a

“validator



to
ensur
e

tha
t

th
e

value
s

d
o

no
t

fal
l

outsid
e

ou
r

accepte
d

range.

No
w
,

we

are

finally

going

to

leave

the

Tk

world

behind.

T
ix

and

Pmw

are
extensions

to

Tk

and

Tkinte
r
,

respectivel
y
,

but

now

we

are

going

to

change
gears

to

look

at

completely

different

toolkits,

wx
W
idgets

and

GT
K

.

Y
ou

will
notice

that

the

number

of

lines

of

code

starts

to

increase

as

we

start

program
-

ming


in


a

more


object
-
oriented


way


when


using

these


more


modern


and robust

GUI

toolkits.



19.4.3


wxWidgets

and

wxPython


wx
W
idgets

(formerly

known

as

w
x
W
indows)

is

a

cross
-
platform

toolkit

used

to

build

graphical

user

applications.

It

is

implemented

using

C++

and

is

avail
-

able

on

a

wide

number

of

platforms

to

which

wx
W
idgets

defines

a

consistent
and

common

API.

The

best

part

of

all

is

that

wx
W
idgets

use
s

the

native

GUI
on

each

platform,

so

your
program

will

have

the

same

look
-
and
-
feel

as

all

the
other


applications


on


your

desktop.


Another


featu
r
e


i
s


tha
t


yo
u


ar
e


not
r
estricted

to

developing

wx
W
idgets

applications

in

C++.

There

are

interfaces


Example

19.8


Pmw

GUI

Demo

(
animalPmw.pyw
)


Our

second

example

uses

the

Python

MegaWid
g
ets

pa
c
ka
g
e
.


1

#!/usr/bin/env python

2

3

from
Tkinter
import
Button, END, Label, W

4

from
Pmw
import
initialise, ComboBox, Counter

5

6

top = initialise()

7

8

lb = Label
(top,

9

text='Animals

(in

pairs;

min:

pair,

max:

dozen)')

10

lb.pack()

11

12

ct = Counter(top, labelpos=W, label_text='Number:',

13

datatype='integer', entryfield_value=2,

14

increment=2, entryfield_validate={'validator':

15

'integer', 'min': 2, 'max':

12})

16

ct.pack()

17

18

cb = ComboBox(top, labelpos=W, label_text='Type:')

19

for
animal
in
('dog', 'cat', 'hamster', 'python'):

20

cb.insert(end, animal)

21

cb.pack()

22

23

qb = Button(top, text='QUIT',

24

command=top.quit, bg='red', fg='white'
)

25

qb.pack()

26

27

top.mainloop()



to

both

Python

and

Perl.

Example

19.9

shows

our

animal

application

using

wxPython.


Line
-
b
y
-
Line

Explanation


Lines

5

37

Here

we

instantiate

a

Frame

class

(lines

5

8),

of

which

the

sole

member

is
the

constructo
r
.

T
his

method

s

only

purpose

in

life

is

to

create

our

widgets.
Inside

the

frame,

we

have

a

Panel
.

Inside

the

panel

we

use

a

BoxSizer

to
contain

and

layout

all

of

our

widgets

(lines

10,

36),

which

consist

of

a

Label

(lines

12

14),

SpinCtrl

(lines

16

20),

C
omboBox

(lines

22

27),

and


quit

Button

(lines 29

34).

W
e

hav
e

t
o

manuall
y

ad
d

Label
s

t
o

th
e

SpinCtr
l

an
d

ComboBo
x

wid
-

gets

because

they

apparently

do

not

come

with

them.

Once

we

have

them

all,



Example

19.9


wxPython

GUI

Demo

(
animalWx.pyw
)


Our

thi
r
d

example

uses

wxPython

(and

wxWid
g
ets).

Note

that

we

have

placed

all
our

wid
g
ets

inside

a

“si
z
er”

f
or

organization

and

the

mo
r
e

object
-
o
r
iented

natu
r
e

of
this

application.


1

#!/usr/bin/env python

2

3

import
wx

4

5

class
MyFrame(wx.Frame):

6

de
f

__init__(s
elf
,

parent=None
,

id=
-
1
,

title=''):

7

wx.Frame.__init__(self, parent, id, title,

8

size=(200, 140))

9

top = wx.Panel(self)

10

sizer = wx.BoxSizer(wx.VERTICAL)

11

fon
t

=

wx.Font(9
,

wx.SWISS
,

wx.NORMAL
,

wx.BOLD)

12

lb = wx.StaticText(top,
-
1,

13

'Animal
s

(i
n

pairs
;

min
:

pair
,

max
:

dozen)')

14

sizer.Add(lb)

15

16

c1 = wx.StaticText(top,
-
1, 'Number:')

17

c1.SetFont(font)

18

ct = wx.SpinCtrl(top,
-
1, '2', min=2, max=12)

19

sizer.Add(c1)

20

sizer.Add(ct)

21

22

c2 = wx.StaticText(top,
-
1, 'Type:')



25

choices=(
'dog'
,

'cat'
,

'hamster','python'))

26

sizer.Add(c2)

27

sizer.Add(cb)

28

29

qb = wx.Button(top,
-
1, "QUIT")

30

qb.SetBackgroundColour('red')

31

qb.SetForegroundColour('white')

32

self.Bind(wx.EVT_BUTTON,

33

lambda
e: self.Close(True), qb)

34

sizer.Add(qb)

3
5

36

top.SetSizer(sizer)

37

self.Layout()

38

39

class
MyApp(wx.App):

40

def
OnInit(self):

41

frame = MyFrame(title="wxWidgets")

42

frame.Show(True)

43

self.SetTopWindow(frame)

44

return
True

45



Example

19.9


wxPython

GUI

Demo

(
animalWx.pyw
)

(conti
n
ued)


46

def
main():

47

app = MyApp()

48

app.MainLoop()

49

50

if
__name__ == '__main__':

51

main()




we

add

them

to

the

size
r
,

set

the

sizer

to

our

panel,

and

lay

everything

out.

On

line

10,

you

will

note

that

the

sizer

is

vertically

oriented,

meaning

tha
t

our
widgets
will

be

placed

top

to

bottom.

One

weakness

of

the

SpinCtrl

widget

is

that

it

does

not

support

“step”
functionalit
y
.

W
ith

the

other

three

examples,

we

are

able

to

click

an

arrow
selector

and

have

it

increment

or

decrement

by

units

of

two,

but

that

is

not
possible

with

this

widget.


Lines

39

51

Our

application

class

instantiates

the

Frame

object

we

just

designed,

renders

it

to

the

screen,

and

sets

it

as

the

top
-
most

window

of

our

application.

Finall
y
,

the

setup

lines

just

instantiate

our

GUI

app
lication

and

start

it

running.


23

c2.SetFont(font)


24

cb = wx.ComboBox(top,
-
1,

'',


19.4.4

GTK+

and

PyGTK

GTK+



PyGTK


Finall
y
,

we

have

the

PyGTK

version,

which

is

quite

similar

to

the

wxPython
GUI


(See


Example


19.10).

The


biggest


difference


is


that


we

use


only


one
class,

and

it

seems

more

tedious

to

set

the

foreground

and

background

colors

of

objects,

buttons

in

particula
r
.


Line
-
b
y
-
Line

Explanation


Lines

1

6

W
e

import

three

different

modules

and

packages,

PyGTK,

GTK,

and

Pango,

a

library

for

layout

and

rendering

of

text,

specifically

for

I18N

purpos
es.

W
e
need


it


here


because


it


represents


the


core


of


text


and


font


handling


for GT
K


(2.x).


Lines

8

51

The

GTKapp

class

represents

all

the

widgets

of

our

application.

The

topmost
window

is

created

(with

handlers

for

closing

it

via

the

window

manager),

and


Example

19.10


PyGTK

GUI

Demo

(
animalGtk.pyw
)


Our

final

example

uses

PyGTK

(and

GTK+).

Li
k
e

the

wxPython

exampl
e
,

this

one
also

uses

a

c
lass

f
or

our

application.

It

is

inte
r
esting

to

note

h
o
w

similar

y
et
dif
f
e
r
ent

all

of

our

GUI

applications

a
r
e
.
This

is

not

surp
r
ising

and

all
o
ws
p
r
o
g
r
amme
r
s

to

s
wit
c
h

between

toolkits

with

r
elative

eas
e
.


1

#!/usr/bin/env python

2

3

import
pygtk

4

pygtk.require('2.0')




8

class
GTKapp(object):

9

def
__init__(self):

10

top = gtk.Window(gtk.WINDOW_TOPLEVEL)

11

top.connect("delete_event", gtk.main_quit)

12

top.connect("destroy", gtk.main_quit)

13

box = g
tk.VBox(False, 0)

14

lb = gtk.Label(

15

'Animals (in pairs; min: pair, max: dozen)')

16

box.pack_start(lb)

17

18

sb = gtk.HBox(False, 0)

19

adj = gtk.Adjustment(2, 2, 12, 2, 4, 0)

20

sl = gtk.Label('Number:')

21

sl.modify_font(

22

pango.FontDescription("Ar
ial Bold 10"))

23

sb.pack_start(sl)

24

ct = gtk.SpinButton(adj, 0, 0)

25

sb.pack_start(ct)


27

28

cb = gtk.HBox(False, 0)

29

c2 = gtk.Label('Type:')

30

cb.pack_start(c2)

31

ce = gtk.combo_box_entry_new_text()

32

for

anima
l

i
n

('dog'
,

'cat','hamster'
,

'pyt
hon'):

5

import

gtk

6

import

pango

7




33

ce.append_text(animal)

34

cb.pack_start(ce)

35

box.pack_start(cb)

36

37

qb = gtk.Button("")

38

red = gtk.gdk.color_parse('red')


40

for
st
in
(gtk.STATE_NORMAL,


41

gtk.STATE_PRELIGHT, gtk.STATE_ACTIVE):

42

sty.bg[st] = red

43

qb.set_style(sty)

4
4

ql = qb.child

45

ql.set_markup('<span color="white">QUIT</span>')

46

qb.connect_object("clicked",

47

gtk.Widget.destroy, top)

48

box.pack_start(qb)

49

top.add(box)

50

top.show_all()

51

52

if
__name__ == '__main__':

53

animal = GTKapp()

54

gtk.main()

Ex
ample

19.10


PyGTK

GUI

Demo

(
animalGtk.pyw
)




a

vertically

oriented

sizer

(
VBox
)

is

created

to

hold

our

primary

widgets.

This

is

exactly

what

we
did

in

the

wxPython

GUI.

Howeve
r
,

wanting

the

static

labels

for

the

SpinButton

and

ComboBox
-

Entr
y

t
o

b
e

nex
t

t
o

the
m

(unlik
e

abov
e

the
m

fo
r

th
e

wxPytho
n

example),
w
e

create

little

horizontally

oriented

boxes

to

contain

the

label
-
widget

pairs

(lines 18

36),
and

placed

those

HBox
es

into

the

all
-
encompassing

VBox
.

After

creating

the

quit

Button

and

adding

the

VBox

t
o

our

topmost

win
-

do
w
,

we

render

everything

on
-
screen.

Y
ou

will

notice

that

we

create

the

but
-

ton

with

an

empty

label

at

first.

W
e

do

this

so

that

a

Label

(child)

object

will
be

created

as

part

of

the

button.

Then

on

lines

45

46,

we

get

access

to

the
lab
el

and

set

the

text

with

white

font

colo
r
.

Th
e

reaso
n

wh
y

w
e

d
o

thi
s

i
s

becaus
e

i
f

yo
u

se
t

th
e

styl
e

foreground
,

i.e.
,

i
n

the
loo
p

an
d

auxiliar
y

cod
e

o
n

line
s

41

44
,

th
e

foregroun
d

onl
y

affect
s

th
e

button

s
foregroun
d

an
d

no
t

th
e

label

fo
r

example
,

i
f

yo
u

se
t

th
e

foregroun
d

styl
e

t
o

white
an
d

highligh
t

th
e

butto
n

(b
y

pressin
g

T
A
B

unti
l

i
t

i
s

“selected”
)

yo
u

wil
l

se
e

tha
t

the
insid
e

dotte
d

bo
x

identifyin
g

th
e

selecte
d

widge
t

i
s

white
,

bu
t

th
e

labe
l

tex
t

would
stil
l

b
e

blac
k

i
f

yo
u

di
d

no
t

alte
r

i
t

lik
e

w
e

di
d

wit
h

th
e

marku
p

o
n

lin
e

46.


Lines

53

55

Here

we
create

our

application

and

enter

the

main

event

loop.



19.5

Related

Modules

and

Other

GUIs

相关模块和其他
G啉


There

are

other

GUI

development

systems

that

can

be

used

with

Python.

W
e
present

the

appropriate

modules

along
with

their

corresponding

window

sys
-

tems

in

T
able

19.2.




T
able

19.2


GUI

Systems

A
vailable

f
or

Python


GUI

Module

or

System

Descri
ption



Tk
-
Related

Modules


Tkinter

TK
INTERface:

Python

s

default

GUI

toolkit
http://wiki.python.org/moin/TkInter


Pmw

Python

Mega
W
idgets

(Tkinter

extension)

http://pm
w
.sf.net


Tix

Tk
Interface

eXtension

(Tk
extension)

http://tix.sf.net


TkZinc (Zinc)

Ext
ended

Tk
canvas

type

(Tk
extension)

http://ww
w
.tkzinc.org


EasyGUI (easygui)

V
ery

simple

non
-
event
-
driven

GUIs

(Tkinter

extension)

http://ferg.org/easygui


TIDE + (IDE Studio)


T
ix

Integrated

Development

Environment

(including
IDE

Studio,

a
T
ix
-
enhanced

v
ersion
of

the

standard
IDLE

IDE)

http://starship.python.net/crew/mike


wx
W
idgets
-
Related

Modules


wxPython

Python

binding

to

wx
W
idgets,

a
cross
-
platform

GUI
framework

(formerly

known
as

wx
W
indows)
http://wxpython.org


Boa
Constructor

Python

IDE

and

wxPytho
n

GUI

builder
http://boa
-
constructo
r
.sf.net


PythonCard

wxPython
-
based
desktop

application

GUI

construction
kit
(inspired

by

HyperCard)

http://pythoncard.sf.net


wxGlade

another

wxPython

GUI

designer

(inspired

by

Glade,

the

GTK+/GNOME

GUI

builder)

http://w
xglade.sf.net


T
able

19.2


GUI

Systems

A
vailable

f
or

Python

(conti
n
ued)


GUI

Module

or

System

Description



GTK+/GNOME
-
Related

Modules


PyGTK

Python

wrapper

for

the

GIMP

T
oolkit

(GTK+)

library
http://pygtk.org


GNOME
-
Python

Python

binding

to

GNOME

desktop

and

development
libraries

http://gnome.org/start/unstable/bindings
http://download.gnome.org/sources/gnome
-
python


Glade

a
GUI

builder

for

GTK+

and

GNOME

http://glade.gnome.org


PyGUI

(
GUI
)

cross
-
platform

“Pythonic”

GUI

API

(built

on

Cocoa

[MacOS

X]

and

GT
K+

[POSIX/X11

and

W
in32])

http://ww
w
.cosc.canterbur
y
.ac.nz/~greg/python_gui


Qt/KDE
-
Related

Modules


PyQt

Python

binding

for

the

Qt

GUI/XML/SQL

C++

toolkit
from

T
rolltech

(partially
open

source

[dual
-
license])
http://riverbankcomputing.co.uk/pyqt


PyKDE

Py
thon

binding

for

the

KDE

desktop

environment
http://riverbankcomputing.co.uk/pykde


eric

Pytho
n

ID
E

writte
n

i
n

PyQ
t

usin
g

QScintill
a

edito
r

widget
http://die
-
offenbachs.de/detlev/eric3

http://ericide.python
-
hosting.com/


PyQtGPL

Qt

(
W
in32

Cygwin

port),

Sip
, QScintilla,
PyQt

bundle
http://pythonqt.vanrietpaap.nl


Other

Open

Source

GUI

T
oolkits


FXPy

Python

binding

to

FOX

toolkit (http://fox
-
toolkit.org)

http://fxp
y
.sf.net


pyF
L
TK (
fltk
)

Python

binding

to

F
L
TK toolkit
(http://fltk.org)

http://pyfltk.sf.net


P
yOpenGL

(
OpenGL
)

Python

binding

to

OpenGL

(http://opengl.org)

http://pyopengl.sf.net


T
able

19.2


GUI

Systems

A
vailable

f
or

Python

(conti
n
ued)


GUI

Module

or

System

Description



Commercial


win32ui

Microsoft

MFC

(via

Python

for

W
indows
Extensions)

http://
starship.python.net/crew/mhammond/win32


swing

Sun

Microsystems

Java/Swing

(via

Jython)

http://jython.org




Y
ou


can


find

out


more


about


all


GUIs


related


to


Python


from


the


general

GUI

Programming

page

on

the

Python

wiki

at

http://wiki.python.

org/moin/
GuiProgramming.




19.6


E
x
e
r
cises

练习


19

1.

Client/Server

Architecture.

Describe

the

roles

of

a
windows

(or

windowing)

server

and

a
windows

client.

19

2.

Object
-
Oriented

Programmin
g
.
Describe

the

relationship
between

child

and

parent

windows.

19

3.

Label

W
idget
s
.
Update

the

tkhello1.
py

script

to

display
your own
message

instead

of

“Hello

W
orld!”

19

4.

Label

and

Button

W
idget
s
.
Update

the

tkhello3.py

script

so

that

there

are

three

new

buttons

in

addition

to

the

QUIT

button.

Pressing

any

of

the

three

buttons

will

result

in
changing

th
e

text

label

so

that

it

will

then

contain

the

text

of
the

Button

(widget)

that

was

pressed.

19

5.

Label
,
Button
,
and

Radiobutton

W
idget
s
.
Modify

your

s
olutio
n

t
o

th
e

previou
s

proble
m

s
o

tha
t

ther
e

ar
e

three

R
adiobutton
s

presentin
g

th
e

choice
s

o
f

tex
t

fo
r

the

Label
.

Ther
e

ar
e

tw
o

buttons
:

th
e

QUI
T

butto
n

an
d

an

“Update


button
.

Whe
n

th
e

Updat
e

butto
n

i
s

pressed
,

th
e

text

labe
l

wil
l

the
n

b
e

change
d

t
o

contai
n

th
e

tex
t

o
f

th
e

selected
Radiobutton
.

I
f

n
o

Radiobutto
n

ha
s

bee
n

checked
,

the
Labe
l

wil
l

remai
n

unc
hanged.

19

6.

Label
,

Button
,

an
d

Entr
y

W
idget
s
.

Modif
y

you
r

solution
t
o

th
e

previou
s

proble
m

s
o

tha
t

th
e

thre
e

Radiobuttons
ar
e

replace
d

b
y

a

singl
e

Entr
y

tex
t

fiel
d

widge
t

wit
h

a

default

value
of

“Hello

W
orld!”

(to

reflect

the

initial
string

in
the

Labe
l
).
The

Entry

field

can

be

edited

by

the

user

with

a
new

text

string

for

the

Label

which
will

be

updated

if

the

Update

button

is

pressed.

19

7.

Label

and

Entry

W
idgets
and

Python

I/
O
.
Create

a
GUI
application

that

provides

an

Entry

field

where

the

user

c
an
provide

the

name

of

a
text

file.

Open

the

file

and

read

it,

dis
-

playing

its
contents

in

a

Label
.

Extra

Credit

(Menus):

Replace

the

Entry

widget

with

a
menu

that

has

a
File

Open

option

that

pops

up

a
window

to
allow

the

user

to

specify

the

file

to

read.

Also

add

an

Exit

or
Quit

option

to

the

menu

rather

than

having

a
QUIT

button.

19

8
.

Simpl
e

T
ex
t

Edito
r
.

Us
e

you
r

solutio
n

t
o

th
e

previous
proble
m

t
o

creat
e

a

simpl
e

tex
t

edito
r
.

A

fil
e

ca
n

b
e

cre
-

ate
d

fro
m

scratc
h

o
r

rea
d

an
d

displaye
d

int
o

a

Text

widg
e
t

tha
t

ca
n

b
e

edite
d

b
y

th
e

use
r
.

Whe
n

th
e

use
r

quits

th
e

applicatio
n

(eithe
r

wit
h

th
e

QUI
T

butto
n

o
r

th
e

Quit/
Exi
t

men
u

option)
,

th
e

use
r

i
s

prompte
d

whethe
r

t
o

save
th
e

changes.

Extra

Credit:

Interface

your
script

to

a
spellchecker

and

ad
d

a

butto
n

o
r

men
u

optio
n

t
o

spellchec
k

th
e

file
.

Th
e

words
tha
t

ar
e

misspelle
d

shoul
d

b
e

highlighte
d

b
y

usin
g

a

different
foregroun
d

o
r

backgroun
d

colo
r

i
n

th
e

Tex
t

widget.

19

9
.

Multithreade
d

Cha
t

Application
s
.

Th
e

cha
t

program
s

from

Chapter
s

13
,

16
,

an
d

1
7

nee
d

com
pletion
.

Creat
e

a

fully
-

functiona
l

multithreade
d

cha
t

serve
r
.

A

GU
I

i
s

no
t

really
necessar
y

fo
r

th
e

serve
r

unles
s

yo
u

wan
t

t
o

creat
e

on
e

a
s

a
front
-
en
d

t
o

it
s

configuration
,

i.e.
,

por
t

numbe
r
,

name
,

con
-

nectio
n

t
o

a

nam
e

serve
r
,

etc
.

Creat
e

a

multithread
e
d

chat
clien
t

tha
t

ha
s

separat
e

thread
s

t
o

monito
r

use
r

inpu
t

(and
send
s

th
e

messag
e

t
o

th
e

serve
r

fo
r

broadcast
)

an
d

another
threa
d

t
o

accep
t

incomin
g

message
s

t
o

displa
y

t
o

th
e

use
r
.
Th
e

clien
t

front
-
en
d

GU
I

shoul
d

hav
e

tw
o

portion
s

o
f

the
cha
t

window
:

a

large
r

sectio
n

wit
h

multipl
e

line
s

t
o

hol
d

all
th
e

dialog
,

an
d

a

smalle
r

tex
t

entr
y

fiel
d

t
o

accep
t

input

fro
m

th
e

use
r
.


19

10.

Using

Other

GUI
s
.
The

example

GUI

applications

using
the

variou
s

toolkit
s

i
n

Chapte
r

18.
4

ar
e

ver
y

similar
;

howeve
r
,

they
a
r
e

no
t

th
e

same
.

Althoug
h

i
t

i
s

impossibl
e

t
o

mak
e

the
m

all
loo
k

exactl
y

alike,

tweak

them

so

that

they

are

more

consis
-

tent

than

they

are

no
w
.

19

11.

Using

GUI

builder
s
.
Download

either

Boa
Constructor

(for
wx
W
idgets)

or

Glade

(for

GTK+)

[or

both!],

an
d

implement
the

“animal”

GUI

by

just

dragging

and

dropping

the

widgets
fro
m

th
e

correspondin
g

palette
.

Hoo
k

u
p

you
r

ne
w

GUI
s

with
callbacks

so

that

they

behave

just

like

the

sample

applications
we
looked

at

in

that

chapte
r
.