You are on page 1of 12

P a rt ı

lı: P u ttlÆ o n to W o r k

O U ıP ro g ra m m n g To
ı o kı
ı t s fo r P yth o n
ik its Y o u c a n fin d a d o z e n
T h ¢r e Iı w id e ıu p p o r t fo r w r itin g G U Ia w y d jffe r e n t to o
ith p y th o n w ith m a n
o d u le s fo r
o p d ıl¢u ıl w w w w t h o n o r g / m o i n / g u łp r o g r a n m ī n g to
tr y o u t t h e s e to o lk its b in a r y m
n d o ffe r d iffe r e n
i t
lı h d iff t A P s a
p y d ım th ı¢in b e r fıc e w ith ru tiv e G U ıc o d e w r itte n in C / C + + a a v e e r e n
Iť l s ib le
r a b le T K G u I to o lk it
s a w a y p
s o s
b ıı u r e « tı o n ly o n e c o m e s w ith p y th o n b y d e fa u lt th e v e n e
T h e m ıly b r a v e
l w in 3 2 a ll a n d t ıs e th e w in 3 2 A p I d ir e c tly
d u ıil y o u r e tu a t u = in g w u \d o w s y o u ll in s ta ı
w ill w r ih th e ir e n tire G U I in p y Ń a m e a n d a d d s o u n d to e v e r y s li e r
d

r ta n t w a y is
O th e r o p tio ¢u a r e w x p y th o n P y Q T a n d p y Ń T K T h e s e d iffe r in m a n y w a y s b u t o n e im p o
th e ıic m e T h e p y Q T w e b p a g e s h o w s th is p ro b ı em
h de s
o f h o w it c o u ld r e s tr iđ t e
d i o r s
ı yo u can m a ke
i Y th i in t h e fo llo w in g
a in c la s s e s o f a p p lic a tio n s o r lib r a r e s
U v o u a r e tr y in g to c re a te c e r ı o u c a n s e e s

P* tıgr ı

p yQ t is lic m se d e r th e G N U G P L (fo r U N D ( / L in u x a n d M a c O S / X ) u n d e r th e
d u n

QT N o n « o m m e r c ia (fo r u s e w ith th e Q T v 2 3 0 n o n c o m m e r c ia l v e r s io n fo r
l L ic e n s e
W in d o w s ) u n d e r th e Q ī E d u c a tio n a l L ic e n s e (fo r u s e w ith th e e d u c a tio n a l e d it io n o f
Q T fo r W in d o w s ) a n d u n d e r a c o m m e r c ia l lic e n s e (fo r W in d o w s U N D ( / L im ıx a n d

T he y go o n to s ta te

W d e p lo y in g c o m m e r c ia l P Q t a
p p lic a tio n s it is n e c e s s a r y to d is c o u r a g e u s e r s
n en
y
fr o n ıc c e r s in
ı g th e u n d e r ly in g P y t m o d u le s fo r th e m s e lv e s A u s e r th a t u s e d th e
U l e b a h ip p e d w ith y o u r a
p p lic a tio n to d e v e lo p n e w a p p lic a tio n s w o u ld th e m
s e lv e s b e c o m id e r e d a d e v e lo e r a n d w o ld
p u n e e d th e ir o w n c o m m e r c i l
a Q T an d
P y Q t lic e r u e s

O n e ao ı
u tio n to th is
p ro b le m is th e V e n d o r lD p a c k a g e 1 h is e n a b le s o
W e x te n s io n m
d u le s th a t c a n o n l b e im o r te d b y u to b u ild
o
y p y a d ig ita lly s ig n e d c u s to m
in ıe r p re te r T h e
p a c k a g e e r ıa b le s y o u to c r e a te s u c h a n in te r r e te r
p w ith y o u r
a p p lim tio n e m b e d d e d w ith i it T h
n e r e s u lt is a n in te r r e t
p e r th a t c a n o n l
P y t m o d u le s th a t c a n o n ly b e im p o r te d b ttm t iy
a p p lic a tio n a n d
ru n
y
Yo u c a n th e Ħ c k a g e to s im ila r l re s h i t y n te r r e te r
p
y c a c c e s s to a n
y e x te n s io n m o d u le

lio a u in 吕Ia n d ıc ı e T h e T
p Q pe op e w o ld ı a im iiu t th e d v a n a e s f th e ir m :
o fUa a » ł n fo r d fe w p e o ie w h o W in d o w
ki o v e m h e
ı th¢
p u se ' If y o u a g r e e tr e a d w a r ily
. st
, in to th e r ı
ic e n s in
g

C h a p te r 13 : W r itin g a G U I w ith P y th o n

O n e o pen so u rce o p tio n is w x p y th o n W ×P y th o n is b a s e d o n w x w id g e ts , a p o r ta b le (W in d o w s , L in tıx ,


M ac O S X ) gra h
p c s to o lk it w ith a lo n g h is to r y a n d a tr a d itio n o f lo o k in g a n d r u n n in g ju s t lik e n a tiv e
i
co d e Yö u can fin d th e b e s t in fo r m a tio n o n w x p th o n o n th e r e a ll n ic e w ik i a t h t t
y y p / /w ĺ k ĺ
w x py th o n o r g /in d e x c g i /f r o n tpa g e

B e g in n e is to G U I c r e a tio n m a
y fe e l o v e r w h e lm e d b y w x p y th o n A lth o u g h th e r e is g o o d u s e r s u p p o r t in
ilin g lis ts a n d p r o fe s s io n a l o r a n iz a tio n s th e w x p th o n lib r a r is in tim id a tin
m a
g , y y g N e v e r th e le s s , iť s a
g o o d o p tio n fo r p e o p le w illin g to c lim b th e le a r n in g c u r v e A t th e tim e o f th is w r itin g , n o t a ll o f th e s e
p ro g ra m s su p p o r t 3 1 th o , u gh m o st p ro m is e to

W i th is in m in d ,
fo r th e re st o f th is
p te r y o u w ill b e w o r k in g w ith T k in te i T k in te r is P y th o n s
ch a
'

s ta n d a rd G U I p a c k a ge a n d c o m e s in s ta lle d w ith P y th o n It is p e r h a p s th e m o s t u s e d G U I P r o g r a n u n in g
k it a n d is p o ń a b le a c r o s s v ir tu a lly e v e r y p la tfo r m F o r m o r e in fo r m a t io n o n th is v e n e r a b le s ta n d b y fe e l ,

fr e e to v is it h t t p / / t k ī n t e r u n py th o n ĺ c n e t /w ī k ī l yo u can a ls o v is it its h o m e p a ge a t
http / /w ī k į py th o n o r g /m o i n /t k l n Łe r

T k in t e r ı
n t ro d u c t io n
d e r s to d th e b a s ic p ts , h o w ll fin d
G U is p le a s th e y lo o k O
'
a re n o ta s s im n ce you v e u n o co n ce ev e Ļ yo u

th e ın d e r s ta n d a b le d p ro p e r p ro d e s ig n ill h e lp y o u n a v ig a te a ro u n d th e m j
a o r ro a d b lo c k s
u n ,
an g r a m w

im p le d ia lo g b o x th a t y o u v e
'
b
p p lic a t io n s h a v e to b e c o m p le x Y o u r a p p lic a t o n m a y e a s
N o t a ll 1 1« in te r a i
h
w r itte n to a u to m a te a b tıs in e s s p r o c e s s y o u o fte n d o T h e s a m e t in g s
th a t m a d e la rg e a p p lic a tio n s lik e
l
C A N V A S , D a s h b o a r d a n d P y th o n c A D q u ic k a n d e a s y to w r ite m a k e s im p le a p p lic a tio n s n e a r ly tr iv ia
,

l ID L E i lf
1 1ç in te r its e lf h a s b e e n tıs e d to m a k e m a n y p o p u la r p r o g r a m s , in c lu d in g m o s t fa m o u s y tse

C re a t in g G U ıW id g e ts w it h T k in t e r
id t
th fir s t th in g to 1ın d e r s ta n d is th a t m o s t G U I fr a m e w o r k s in c lu d in g T k in te Ļ a r e b a s e d o n a ıu g e
e ,

fa G U I b u tto n s la b e ls a n d te x t b o x e s a r e a wll id g s
e t M o s t w id g e ts
m o d e l A w id g e t is a c o m p o n e n t o ,

b l d b i l
h a v e g r a p h ic a i r e p r e s e n ta tio n s o n s c r e e n , b u t s o m e w id g e ts , s u c h a s ta e s a n y to
o x e s ex s t o n ,

h o n th e s c r e e n A G U I is c o n s tr u c et d t f an a r r a n g e m en t o f
c o n ta in o th e r w id g e ts a n d a r r a n g e t e m
o u o

w id g e ı s In th e fo llo w in g s e c tio n , y o u c r e a e a s
t fi
im p le G U I b y d e n in g s o m e w id t
ge s a n p d la c in g th e m
in s id e e a c h o th e r

W r itin g a e T k in te
S im p ı r P ro g ra m

W ith T k in te r a lr e a d y in p la c e , y o u
'
re r e a d y to w r ite a r e a l G U I a p p lic a tio n ıh is s c r ip t, M y F ĺ r s t G u i,

c r e a te s a G U I o fa s im p le w in d o w an d a la b e l T h e la b e l o n th e w in d o w d is p la y s a m e s s a g e

in p o r t t k in t e r

£r o m t k in te r i rrp o r t
*

T h ia is fir s t G l l!' ) # c r e a te a la b e l
L a b e 1 (N t t y u
'
w id g e t = o n e ,
e x - m

w id g e t pa c k t
w id g e t M a ĺ n lo o p (

229
P a r t lll P u ttı
n g Fy h o n to W
o rk

R u n th ı
a p ro ı d yo
gw m an u lıs e e th e m Ia
a m y fin t G u ill la b e ıin th e w ł
n do w A s ıh o w n


g u re 1 1

H ow ı
tW o rk s


d n ln o r ıim

y im p o r t ・liľ ) fm m Tkinte a s o u d ı
y d in th e
\ fw e a ch w id g e t (ın th is c a s e L b
a e l) T h e La b l
exa m p le A fte r th a t y o u e m a te a n
is th e n
g e d in th e p a r e n t w in d o w F ı
, e a rra n
ru ll»

O ne p ro b le m w ith th is s c r ip t ls th a t u n lik e
m o s tG u I a
ın m T h e r e a s o n fo r th is is s im ı ppı ic a ı io m th is o n e d o e s n t a c h a ll
p e T h e s c r Ip t a s it is d a e s n t h a n d le a n G
i y do
y U I e v e n ts D o n t w o r ry

R e s lz fn g th e W ld g e t

cou ld ıe tc h o u
sı t th e w in d o w G o a head an d g ra b th e r i h t s id
ie w g e o f th e w in d o
ıb u w ill n o te th a t th e ı
a b e l s ta
w a n d
p u lıit to th e r ig h t a
y s in th e c e n te r n e a r th e to
p T h is is a g o o d th in
g

w iM o W y o łıH m t th e w id e t(q in s id
8 o be ha v e
e ı
d ıe la b e l o en k rs w h a w to p r ia te ı
y W ith th is in in d d ify y o
m th e w in d o w is re s i d m m o u r c o d e 5o
ze

tort t k ın t e r
tr o l t k i n t e r i» o r t ・
p
W ı{ t e x t . Il f i
y r s t G U ıI ) Pa c k (e x p a n d y ES
°
6 11 1°B O T H )

W n en yo u n ın th iıp¢Dg rım
W m iz ln g th e w in d o w y o u 1 łs e e th e M y H m t G u IĽ la be ı
B la y ( r n te re lı

F * u ıe ıa a



230

■自由自
r 1 3 w r it in C h a p te
g a G u I w ith p y th o n
fiıtlıjs e m ı ¢
p e Jlo ıłin ıp 0 r te d a J1加 ın J7
c in te r O n æ m o
g o u ıs s im th e al v e s Y e s t . a n d la e n c r e a ta d a ı
a b e l w ith s o m e te x t N ex t
o e x pım d A n d B o t h to ū l
P ım t w in d o w is e x ım d e d j T h is te lls p y th o n to e x p a n d th e t o id g e t u Jh m th e
p B y d e li
n u lt Th is o tio n i
p s h łm ed o ñ
Co n fı
g u rı
n g W ı
d g e t O p t io n s
Yo '
u v e se en o n e m
e th o d to a s s ig n n o t o n ly te x t to o u r la b e l b
w id g e ť y u t a ls o h o w to c o n fig u ï e s o m e o f th e
s o p tiw su cH as ex
pan d an d fill T h o u h th e m e th d
g o y o u u s e d w a s c o n v e n ie n t in th is in s ttia n icC ee
yo u m a y lin d y o u ıs e lf w an tin g to c r e a te
y o u r w id g e ı
s fir s t a n d th e n c o n fi u r in
g g th e ir o p tio n s la te r o n

Co n fig u r in g Y o u r W id g e t
h th is e x a m
p le y o u c r e a te th e s a m e p a r e n t w in d o w d th e la b e l H
an sam e o w ev e r in s te a d o f s e ttin
g
y o u r o p tio n s a t th e s a m e tim e o u c r e a te th e m
y yo u a ie g o in g to w a it an d d o th e m fte r th e y h a v e
a lr e a d y b e e n c r e a te d a

i\ r t t k in t e r
fr o m t k in t e r im p o r t *

r o o t - T k (

w i ◆g e t L a b e l (r o o t )
w i ◆g e t c o n f i g (t e x t M y fir t GU I !
s )
w id g e t p a c k (s i d e T O P e x d Y E S £i l l
p a n BO T H )
r o o t M a i n ıo o p o

In th is e x a m p le y o u c a lle d u
p o n th e c o n fig u r e m e th o d to a c h ie v e th e s a m e r e s u lt a s th e p r e v io u s
exm p le If y o u w a n te d to y o u c o u ld c h a n g e th e a p p e a ra n c e o f th e w id g e t la te r in th e r o r a Jm F o r
p g
in s ta n c e m a y b e th e u s e r w is h e s to c h a n e th e w a th e w in d o
g y w lo o k s Y o u c o u ld in s e r t a b u tto n ttm t
w o u ld tr ig g e r th e c o n fig u re m e th o d w h ic h in h u n w o u ı d c h a n g e y o u r w id e ť s o t io n s
g p


ıt tı
n g t h e W ld g e ts to W o rk
S o fa r yo u v e see n ho
w to m a k e a b a s ic la b e l a n d h o w to fo r m a t o u r w id
y g e ts B u t fo r a p r o g i a Ja ıto b e
s u cc w it n o to n ly h a s to lo o k g o o d it h a s to a c tu a ll d o s o m e th in
y g T h e n e x t fe w e x a m p le s n o t o n ly
e a c h yo u h o w
ı to a d d m o r e th a n o n e w id e t to o u r G U I b u t te a c h o u to a
g y y ppı y a c tio n s to th o s e
id g e ls E v e n m o r e im p o r ta n tly
w
y o u te a m to m a k e th e p r o g r a m r e s p o n d to th e u s e r s a c tio n s

Yo u hav e te a m e d ho w to c r e a te o n ty p f w id g e t s o fa th e la b e l In th is
e e o r ex am p ıe yo u te a m to c n ea »
a b u tto n Tr y ty p in g th e fo llo w in g co d e in to a file c a lle d M y p ir s tB u t t o n p v

im p o r t s y s
fr o nı tkĺ n te r īw r t *

w ĺ dge t B u t t o n tN o n e Łe x t C ıi c k M e c o nm a n d s y s E x it )
w id g e t pa c k (
w iđg e t na
ı in lo o p (
Conñgur\ngYourW\dget

Upte pointyou haved thedefauı


, tıookf oryourw idgete,w hichInprettydrabThklepyour
,

w eť iattention andcreate program thatare vl


aual ookofyour
lyappealing, you haveto tw eakthtıı
Widgı . ryoutūw f olhw ingcode

WOrt tk\nter
fro»ıtk\nter Įmport *
rw Ł・ m t

lał ont ・ ( tīnw a ' , 24, ' \ta1Įc ' )


e1£ # ı*ヒļ ng th・Eam\1y, IĮz* , ・nd
'
》 ヒ

自yl。
wįđ
gat Jab・
ı 1{root, t・xtn 'EaŁAŁıJOES' ) * ıı
=
«tLĮng ı・b・1 Lıxt
w į dg»ŁconfĮg{bg= 'bı
. ack , tg« ' red' )
'
*ı eLŁ
ı \ng th・b・ok *nd lor・ground
colcJra
w į dg・
t pack(«xpand«yEB,
. £
į 11= BOTH)
na ļ nloop(
root ı
.

Rm thb application andubtłerve the reeuı


tThoughthe pro吕
. riı lydu anything,th*
m doesnotactual
deai8n doeıdraw the eye'aattention .


广 low ingtable lea lietofthe differentwayethatyou can cuutom lzen w \dget
The foı
C h a p te r 1 3 : W
ritin g a G u I w ith p th o n
y
R a d io B u tto n s a n d C he c kbox es
S o fa r y o u h a v e w o r k e d
w ith th e fo llo
w in g w id e ts th
lin e s e a r e a ll o w e r ftd g e to p le v e l w in d o w f
r a m e s la b e ls a n d b u tt
p w id g e ts b u t s o on s
m e tim e s
, ,

w h e r e r a d io b u tto n s y ou m a ,
y w a n t to g iv e y o u r u s e rs m o r e o tio ru T h a t is
a n d ch e c kbo x e s p
co m e in to p la y

A s id e fr o m a p p e a r a n c e r a d io b tt
u on , s an d c h e c kb o x e s d iffe r in o n e s i n ific a n t w
g a y r a d io b u tto n s o ffe r
u s e r s a lis t o f o ū o n s b u t a ll
p o w th e m to s e le c t o n l o n e c h e c k b o x e s o ffe r u s e r m
,
y e a n y o p « o iu , a n d le ts
th e m c h o o s e a s m a n
y a s th e w a n t y

C re a t in g R a d io B u tto ns

C re a te a n ew file ca lle d M yR a d io p y an d in c lu d e th e fo llo w in g co d e in it

į m p o r t t k īn t e r
£r o m t k īn t e r im p o r t *

s t a t e

b u t t o n s []

d e f c h o o s e (i )
g l o b a l s t a t e

s t a t e ī
f o r b t n in b u t t o n s

b t n D e s e l e c t (
t t [ī ] s e l e c t (
b u o n s

r o o t T k (

d ło d ( ıa m b d a ī ĺ c h o o s e (ī t )
r a
v a lu e s t r (Ĺ ) c o m m a n

(s ĺ d B O T T C ıÏ )
p a c k
e
r a đī o
e n d (r a d Ĺo )
b u t t o n s A p p

b e r e t a Ľe )
Ľh e fo l l o w in g n u m
c h o s e
t ( Y o u
p r Ĺn d 3
to ta l) ith th e n u m be r 1 2 a n

f b u tto n s ran g in g fr o m
m (fo u r w

c rea te s a s e r ie s o
m p r o g ra m c lo s e ou to f
s e le c te d W
h en th e u s e r s
F a ls e m e a n in g th a t it is n o lo n g e r
t a te b e co m e s
h i h b e r th e y ch o s e
o e r bu tto n s s
a s ta te m e n t s h o w in g ì v c n u m

ra m th e y a re g iv e n
th e p r o g ,

h e th e r th e
In th is in s ta n ce h o w e v e yo u s im p ly return w

im ila r w ith che ckbo x e s


m e th in g s
\o
Yo u can do so
is tr u e o r fa ls e
fe a ch c h e ckb o x
v a lu e o
*
im p o r t
fr o m t k in t e r

s t a t e s
[]

n o t e t a Łe e [ĺ ]
e t a t e s [i l =

Tk (
r o o t
-

235
fo r ĺ in r a n g e (4 )
te s t = Ch e c kb u t to n (r o o t te x t ・ s tr (i ) c o m m a n d = (l a m bd a ĺ ĺ c h e c k (ĺ ) )
te s t p a c k (s ī d e - TO P )
s ta t e s A pp e n d (0 )
r o o t M a īn ] o o p (
p r ī n t (s t a te s )

fyo
ı u ru n th is p d h e ck ff b o 2 d 3 , th e n d th e in d o lt s h o ld b e
ro
g ra ln a n c o x es a n o se w w yo u r re su u

[O T n ıe T n ıe O ]

D la ı
og B o xe s
S a n i m e s y o u w a n t to g iv e th e lıs e r a ie c e o f a d d iū o n a l in fo r m tio Yo do b t fa m ilia r
p a n u a re n o u w ith
d ia ıo g b o »ce s T h e y p o
p u p a n y tim e th e r e is a n e r r o r o r a p r o g r a m , w a n ts to co n fh n ı somethin su c h a s if
y o ıı r e a lly w a n t to u n in s ta ll a p r o g r a m o r if y o u w a n t to d is p la y sen s it iv e in fo rm a tio n

T k in t e r o «e rs u p tw o ty p e s o f d ia lo g b o d a la d da l M d a l d ia lo g b o
x es m o n n o n m o o x e s w a it fo r so m e
æ tio n ħo m th e u se r b e fo r e g o in g a w a y d p th e p r o g r e f th e p
an a u se ss o ro g ra m N o n m o d a l d ia lo g b o x e s
do n o t in ı
e iT u p t th e flo w o f th e p r o g r a m

C re a t in g a C u s to m D ia ı
o g B o x

Yo u c än u se s e v era lm e th o d s to c re a te d ia lo g bo x e s In th is Tr y It O t, c r e a te a c u s to m d ia lo g b o
u yo u x
u s in g To p le v e L C re a te a n e w file ca lle d M y p o p u p p y an d en te r in th e fo llo w in g te x t

o r t s y s

f r o nı tk ĺ n t e r im p o r t *

p o p u p p e r (l e n {s y s A r g v ) > 1 )

d e f d ĺ a l o g ( )
w ĺ n - T o p le v e 1 (
1 (w i n t e x t D o Y o u A lw a y s D o W n a t Y o u A r e T o ıd ? ) p a c k t
B u t t o n (w i n t e x t - N o w c li c k th ĺ s o n e c o m m a n d w ĺ n D e s t r o y ) p a c k (
ĺ f p o p u p p e r

w ĺ n F o c u s s e t (
w ĺ n G r a b s e ( t
w īn w a īt w in d o w (
p r in t ( Y o u b e t t e r o b e y m e )

r o o t = T k (
B tıt t o n (r o o t t e x t C li c k M e , c o m m a n d d ia lo g ) p a c k (
r o o t M a in lo o p (

W n e n th is p r o g r a m , y o u fir s t g e t a p o p u p , s ta tin g s im p ly
yo u ru n C lic k M e W n e n y o u d o tw o
th in g s fir s t, s o m e te x t is w r itte n to s td o u t (s p e c iń c a lly , " Y o u b e tte r o b e m e
o cc u r i Se c o n d ,
y
an n u m u p w in d o w a p p e a r s . ta k in g fo c u s a n d a s k in g fo r
w y o u to c lic k it a s w e ll W ħe n y o u d o , it
d o s e s itó e lf, a n d th e o r ig in a l p o p u p w in d o w ta k e s b a c k fo c u s

236

You might also like