Tutorial Tkinter

Embed Size (px)

Citation preview

  • !!""

    ##

    ##

    $ % &'( ) '* &+ )

    UNIVERSIDADE ESTADUAL PAULISTA JLIO DE MESQUITA FILHO

  • 2

    !"#$

    % $&'"#$

    ! ( )$* '$ ( )$

    ! " # $%& '&((%&)*+! "

    '&((*,,+ " $%&

    '&((---%&.**+

    / * 0

    *12

    30!4.

    5**)0!

    *"$%&230!

    156&'&((---*+

    6 & 1!

    7

    !&"18130

    4! 7!8".92

    0*

    Josu Labaki #$%&

    /&:

    ;

  • 3

    +&,-

    ! ., /

    >*" ?

    @ , A

    ! $ !&$! 0

    >B, C

    @- D

    E$-#;= >>

    ! ,&,1 2/

    > >?

    @F >G

    E* @>

    ! 3!4$&&!&$ 56

    >-% @E

    ! 3!7! 50

    > @G

    @5& @H

    E$8!:1 E>

    ? EA

    A#*) EH

    ! 3-'$,7$8 /9

    >) ?A

    @#;= ?G

    E*30 AI

    ?# A?

    ! 3:-;-,$ 9 ,4!!>!7!8 02

  • 4

    ! .,

    ,$%&

    " & " 0

    1 '=/51"+!

    -230*

    !*30

    *" $%& J- )" ,

    &$%&)"

    " , '*375130

    +

    2? '4 !&$$$7$-!@'>A,$

  • 5

    -+ > L$*M

    &Q'" *

    -+! &

  • 6

    4&K:&

    :%K#;=*

    2 ! 1!

    "#;=!!

    *0! & " 30

    !)*LM!

    *0X&*0LF&M

    1

    F*0X!)R*& ,

    1@! " *7 K! 1K

    &)" 0 #;=! * 8

    8*8 , ,&1*

    8P*8*)=/5

    "! 30 ,! =/5 *

    ""!

    ,=/5K!8

    ! 3 7

    S#;=

    ! $ !&$!

    2? ,!B',!!-.

    0-0!0

    #;= 8 ) -

    2 No necessrio implementar os exemplos deste Mdulo. Todos eles esto disponveis para download a

    partir de http://labaki.tk.

  • 7

    & * 1 '

    +!.&"0

    * /#;= )

    &!"&!

    &LM'LM+' '&+

    J1

    30.-

    "

    "1 & ) .!

    *7&!*8!P)&"

    30-!

    30" ,

    !

  • 8

    5? -$ C,$D,&1

    1 "

    -($

  • 9

    )!*)

    $%& 0 ) 8! *

    10,

    30T*,U*0KT*,U!

    8*T*U

    - 37 30 "

    $!*837*0

    self.botao['bg']='green'

    ;")&301

  • 10

    from Tkinter import *

    class Janela:

    def __init__(self,toplevel): self.fr1 = Frame(toplevel) self.fr1.pack()

    self.botao1 = Button(self.fr1,text='Oi!') self.botao1['background']='green' self.botao1['font']=('Verdana','12','italic','bold') self.botao1['height']=3 self.botao1.pack()

    self.botao2 = Button(self.fr1,bg='red', font=('Times','16')) self.botao2['text']='Tchau!' self.botao2['fg']='yellow' self.botao2['width']=12 self.botao2.pack()

    raiz=Tk() Janela(raiz) raiz.mainloop()

    '> !&$E

    P))&

    *7**0!P&>@"

    '*

    +!*0&! C*0

    &&>@!

    1 ! 30 LM

  • 11

    ! 3 37

    "*7 "! &2"*

    &)37!0

    6? $,-,$!&$D,&1 !

    *130

    *7 Q 0 *

    )!

    = ) 1 -

    , K ! 5F

    '+!6=#B '+! $'+ '*1+!

    -

    - ! 3

    1 - " ! *

    ,

  • 12

    ! *0L &9M ! !

    *1 *0

  • 13

    '> !&$E

    !R,'X $+*

    ! 0 0 & * 0

    $! 0 " 1

  • 14

    ! ,&,1

    O" & 0 37 1

    " !

    &37

    4 * & $

    1,6-/

    2? 7 $&$4$'

    LM< 6

    $1!

    *0! ""!

    < 6""

    / &$+ 1

    *0**01

    K10(&

    self.botaoverde.bind("", self.muda_cor)

    YK1*1

    8-"*0

    1-!9!

    1 9 37

    30)"&'5!,!+

  • 15

    from Tkinter import *

    class Janela:

    def __init__(self,toplevel):

    self.frame=Frame(toplevel) self.frame.pack()

    self.texto=Label(self.frame, text='Clique para ficar amarelo') self.texto['width']=26 self.texto['height']=3 self.texto.pack()

    self.botaoverde=Button(self.frame,text='Clique') self.botaoverde['background']='green' self.botaoverde.bind("",self.muda_cor) self.botaoverde.pack()

    def muda_cor(self, event): # Muda a cor do botao!

    if self.botaoverde['bg']=='green': self.botaoverde['bg']='yellow' self.texto['text']='Clique para ficar verde' else:

    self.botaoverde['bg']='green' self.texto['text']='Clique para ficar amarelo'

    raiz=Tk() Janela(raiz) raiz.mainloop()

    '> !&$E

  • 16

    ;*0&$+

    *0K

    ,*)*)

    *)37*

    370&!1

    *) ) ! *

    muda_cor(self, event)

    :$);!*0

    0*#;=2"!8

    :$1;!*0

    **

    & )

    2

    self.botaoverde.bind(("",""), self.muda_cor)

    *0*0

    *30

    - * 0 K

    *-F31

    ""*0Z

    'Z>!@E!*0

    + ""

    *-!"" -

  • 17

    5? F$-$7 $& ->!&$

    - "

    /&

    'J-1!1+2

    !":&

    ! K K &

    "

  • 18

    ;""

    5F! "" "

    P "" "ALFA" F !

    *""""

    B"$

    !&$E

    =3?4$ 8

  • 26

    ! 3!7!

    @-8"8&

    K 37! ! &!

    -! 37!

    $=&

    & *) &! ! 8 8 S

    *) ,!&!

    !8!:1

    /. -

    3730*7*!

    11 $30

    *-!1@$!K!K

    * -

    80A+ $! !!5! $ 20GG37

    " - &

  • 27

    instancia=Tk() Kanvas(instancia) instancia.mainloop()

    '> !&$E

    3K:"*

    )S30K

    Y 0 !

    1"**30

    )!.*&

    2? $$&!&!

    !0

    =*&&

  • 28

    *

    1%&!*.G:H(-I

    G:H JI!

    $/8VP)1

    0'>I!

    HI+!0"'>I!.HI+X'>I!>I+

    ;

    W

    &

  • 29

    5? ,)!

    1&&

    self.nome_do_canvas.create_line(x1, y1, ...., xn, yn)

    &3'1>!%>+!'1@!%@+!'1E!%E+

    '1! %+

    !!)

    create_line(x1, y1, x2, y2, x3, y3, x4, y4, ......)

    create_line((x1, y1), (x2, y2), (x3, y3), (x4, y4), ......)

    create_line((x1, y1, x2, y2), (x3, y3, x4, y4), ......)!

    *=*&

    )"0)!&&

    $>X'1>!%>+$@X'1@!%@+!*K +-$>$@/2!

    8"3 +-$>_I`$>_>`$@_I`$@_>`/

    aK&"!

    37300,!

    &!

  • 30

    from Tkinter import *

    class Linhas:

    def __init__(self,raiz): self.canvas = Canvas(raiz, width=400, height=400, cursor='watch', bd=5) self.canvas.pack() self.frame=Frame(raiz) self.frame.pack()

    self.last=[200,200]

    configs={'fg':'darkblue', 'bg':'ghostwhite', 'relief':GROOVE, 'width':11,'font':('Verdana','8','bold')} self.b1=Button(self.frame, configs, text='Esquerda', command=self.left) self.b1.pack(side=LEFT) self.b2=Button(self.frame, configs, text='Para cima', command=self.up) self.b2.pack(side=LEFT) self.b3=Button(self.frame, configs, text='Para baixo', command=self.down) self.b3.pack(side=LEFT) self.b4=Button(self.frame, configs, text='Direita', command=self.right) self.b4.pack(side=LEFT)

    def left(self): # Desenha um segmento para a esquerda x, y = self.last[0]-10, self.last[1] self.canvas.create_line(self.last, x, y, fill='red') self.last=[x,y] def up(self): # Desenha um segmento para cima x, y = self.last[0], self.last[1]-10 self.canvas.create_line(self.last, x, y, fill='yellow') self.last=[x,y] def down(self): # Desenha um segmento para baixo x, y = self.last[0], self.last[1]+10 self.canvas.create_line(self.last, x, y, fill='blue')

  • 31

    self.last=[x,y] def right(self): # Desenha um segmento para a direita x, y = self.last[0]+10, self.last[1] self.canvas.create_line(self.last, x, y, fill='purple') self.last=[x,y]

    instancia=Tk() Linhas(instancia) instancia.mainloop()

    '> !&$E#;=

    00=1

    ) & K

    ' ,+

    0"0.

    1;1

    6? $>;1$$H K1'>$ : $

    /&8&&3

    & +&"

    3&"

    8&8K&.

    30 2 &

    1!37$,

    6: 0 "& 2

    9 +

    &37 !,$*0

    8

  • 32

    $1*21

    self.nome_do_canvas.create_text(x, y)

    1!30 5!"'1!%+=

    1):!*1

    30 !

    :"*'1!%+/&X2J

    301'>II!>II+!*1

    "'>II!>II+0 < 6

    30 )" & *7 * *

    !130

    )30,!1"

    4&,"1

    & 1 2 "! 3 *

    30Q!

    &&30

    $!)05F '&S 0+!< 6

    'K+6=#B '&S+

  • 33

    ""&@II/!

    '>II!>I+"'>II!@II.>I+X'>II!>HI+!

    0 K &

    W@II* $

    '>II!$>I+!1

  • 34

    100, altura-10, fill='white') ret(15, altura-150, 185, altura-185, fill='black') pol(20, altura-140, 95, altura-140,

    95, altura-30,

    20, altura-140, fill='red') pol(105, altura-30, 105, altura-140,

    180, altura-140,

    105, altura-30, fill='black') self.canvas.create_text(100, altura-167.5, text='S P F C', font=('Arial','26','bold'), anchor=CENTER, fill='white')

    instancia=Tk() SPFC(instancia) instancia.mainloop()

    '> !&$E

    O" 0 0.9 2

    "&"!&0&

    300

  • 35

  • 36

    $30 ! 11

    .&"!&3

    &1!8XCI

    305!&8

    .&"!&"&P)

    115XHI)"&

    $ 4! &" 30

    6!B6/$=25='0+

    1 & 3

    *

    &!&*

    :6$

  • 37

    5&!&

    # -*- coding: cp1252 -*-

    from Tkinter import *

    class Fatias:

    def __init__(self,raiz): self.canvas=Canvas(raiz, width=200, height=200) self.canvas.pack() self.frame=Frame(raiz) self.frame.pack()

    self.altura = 200 # Altura do canvas

    self.canvas.create_oval(25, self.altura-25, 175, self.altura-175,

    fill='deepskyblue', outline='darkblue') fonte=('Comic Sans MS', '14', 'bold') Label(self.frame, text='Fatia: ', font=fonte, fg='blue').pack(side=LEFT) self.porcentagem=Entry(self.frame, fg='red', font=fonte, width=5) self.porcentagem.focus_force() self.porcentagem.pack(side=LEFT) Label(self.frame, text='%', font=fonte, fg='blue').pack(side=LEFT) self.botao=Button(self.frame, text='Desenhar', command=self.cortar, font=fonte,

    fg='darkblue', bg='deepskyblue') self.botao.pack(side=LEFT)

    def cortar(self):

    arco=self.canvas.create_arc

    fatia=float(self.porcentagem.get())*359.9/100. arco(25, self.altura-25, 175, self.altura-175,

  • 38

    fill='yellow', outline='red',

    extent=fatia) self.porcentagem.focus_force()

    instancia=Tk() Fatias(instancia) instancia.mainloop()

    '> !&$E

  • 39

    9? -,!4 $&$@L $&$!7!

    *)!*=/

    4..*)&=/

    Q *) ! 7 &

    !=/*)01

    'L*,M+

    *)+-/=/*)-Z>!

    Z@!!Z/*)=/A)!A0!!A4

    K*=/!!L>M!L@M!

  • 40

    /0 & &! "

    *=/!&30V;

    0 0 *. 30

    + + =/ ' !

    +a!)$

    &430

    !9

  • 41

    155, self.altura-85,

    182, self.altura-15, fill='dodgerblue') ret(45, self.altura-35, 90, self.altura-60, fill='darkblue', outline='') ret(110, self.altura-35, 155, self.altura-60, fill='darkblue', outline='') self.texto(100, self.altura-50, text=':', font=self.fonte, fill='yellow') # Fim do desenho do relgio-----------------------

    self.mostrar=Button(self.frame, text='Que horas so?', command=self.mostra,

    font=('Comic Sans MS', '11', 'bold'), fg='darkblue', bg='deepskyblue') self.mostrar.pack(side=LEFT)

    def mostra(self):

    self.canvas.delete('digitos_HORA') self.canvas.delete('digitos_MIN') HORA = str( localtime()[3] ) MINUTO = str( localtime()[4] ) self.texto(67.5, self.altura-50, text=HORA, fill='yellow', font=self.fonte, tag='digitos_HORA') self.texto(132.5, self.altura-50, text=MINUTO, fill='yellow', font=self.fonte, tag='digitos_MIN')

    instancia=Tk() Horas(instancia) instancia.mainloop()

  • 42

    '> !&$E

    Q &! " &

    ! 0 1 *$

    !'+1S&

    / ! K *)!

    &".!"8!:

    1&'#;=+

    !"B4$ '4$$$

    $666=C$$

    D3E ' !"$C

    6* 4$3$5 E 4$

    3$ 3

    -*70*)

    &0=K

    & ! 0!.-

    "**!

    1!*)"

    _ `

  • 43

    QK8*)VVV$

    !-=/ Z e/!*))=/

    30'Z!e+30'Z[Z!e[e+

    999

  • 44

    def esquerda(self, event): self.canvas.move('bola', -10, 0) def direita(self, event): self.canvas.move('bola', 10, 0) def cima(self, event): self.canvas.move('bola', 0, -10) def baixo(self, event): self.canvas.move('bola', 0, 10)

    instancia=Tk() Pacman(instancia) instancia.mainloop()

    '> !&$E

    30 -

    &:-"

  • 45

    K"1*8

    ! 3-'$,7$

    P): ,

    2? M $&$&!L!>!

    ).*3730$1!

    *

    *!)!!

    1K-

    B"K&)!

    5%-,GN"1GN"1/!&&

    "1)Y8!1!*0

    1K ) " *

    %-,GN8!GN8/ ) 0

    &7=4"

    )-$!

    %*8*:-&&&K)

    " 0 $ 1!

    %-,G$!G(/ )

    '-&+8"

    $ ! " 8 )!

    *8)

    K:

  • 46

    1 ) 0

    &"18

    & ) 0 ) 0

  • 47

    6)0)"

    &!*01K*89$!

    *01K)! &

    "1!EIIEII

    6 * 8 S 3

    &).Y.

    5? $>$,&$!

    1!&37

    ,"20-&!*!

    *,!! !%

    -!*-!,!!,!

    ! ,%* 'K +! * 'K

    LMV+ &-& '* + 1 * ,

    '+!'*!*+!

    ,,

    **H!,*HP

    "! , )

    6#&&!

    K 0 ) 4

    &1B""L=

    ,M23

    "#%02x%02x%02x" % (intens_vermelho, intens_verde, intens_azul)

    *"I

    @AA'#38c602+:

    ! - $ K

    *! * "1 '@AA! @AA! @AA+!

    8'I!I!I+

  • 48

    0 1 KW "

    *K*37*"&

    *30 #;=

  • 49

    def misturar(self): cor="#%02x%02x%02x" %(int(self.vermelho.get()), int(self.verde.get()), int(self.azul.get())) self.canvas.delete('bola') self.canvas.create_oval(15, 15, 185, 185, fill=cor, tag='bola') self.rgb['text'] = cor self.vermelho.focus_force()

    inst = Tk() Palheta(inst) inst.mainloop()

    '> !&$E

    2'+

    K.*0!KGIJK0L

    ($4$4$$$3

    5>0MM$ 36

  • 50

    6? $4$&-$@,!$,"#$&$4$'

    - )

    3010

    :-04!

    -

    ,+5-/,+-/5

    30'

    ) .+!)30300

    !,+5-/ ,+-/! 0 30

    30L&M

    *30"30

    !&

    2)@)30"2)

    x_absoluto = C1.winfo_pointerx()

    x_origem = C1.winfo_rootx()0!5+$N5+

    0

    $*3*30

    V20!)*"11

  • 51

    self.canvas.pack()

    def desenhar(self,event): x_origem = self.canvas.winfo_rootx() y_origem = self.canvas.winfo_rooty() x_abs = self.canvas.winfo_pointerx() y_abs = self.canvas.winfo_pointery() try:

    P = (x_abs - x_origem, y_abs - y_origem) self.canvas.create_line(self.ultimo_P, P) self.ultimo_P = P

    except:

    self.ultimo_P=(x_abs - x_origem, y_abs - y_origem)

    inst = Tk() AutoCADE(inst) inst.mainloop()

    '> !&$E

  • 52

    !*)"

    1&+ !)

    0'1!%+=/*)1'1!%+

    1130&K

    /K!"8*"

    + *&2&

    85!058>I

    Q)*!8'I!I!I+/

    " 8 K &W 8 "

    >I! I! @I ' &1 )"

    +

    # -*- coding: cp1252 -*-

    from Tkinter import *

    class Palheta2:

    def __init__(self,raiz): raiz.title('Palheta Grfica') self.canvas=Canvas(raiz, width=200, height=200) self.canvas.bind('', self.misturar) self.canvas.pack() bola = self.canvas.create_oval

    bola(20,180,70,130, fill='red', outline='') bola(75,180,125,130, fill='green', outline='') bola(130,180,180,130, fill='blue', outline='') bola(45, 120, 155, 10, fill='white', outline='', tag='bola') self.tom=[0,0,0]

    def misturar(self,event): xo=self.canvas.winfo_rootx() yo=self.canvas.winfo_rooty() xa=self.canvas.winfo_pointerx() ya=self.canvas.winfo_pointery() cor=self.canvas.find_closest(xa-xo, ya-yo)[0] self.tom[cor-1] = self.tom[cor-1]+10

  • 53

    cor="#%02x%02x%02x" %(self.tom[0], self.tom[1], self.tom[2]) self.canvas.delete('bola') self.canvas.create_oval(45, 120, 155, 10, fill=cor, outline='', tag='bola')

    inst = Tk() Palheta2(inst) inst.mainloop()

    '> !&$E

  • 54

    /? -,!&$&1$4 ,!

    Q #;= - &

    (&K!,0"Y

    *"K!

    , *

    -&-

    30"

    $,'&+ $'+!

    -"$1!30,)6- $G),G)/

    ,06- $G),G0/ & - -> "

    &-@"

    - &" & -@$

    !"30,16- $G0,G0/-

    -E"&@!@

    ,)$K

    * ! $!

  • 55

    2 " X@!

    30-

    &!,! K-

    "&

    Q-"!.

    )330 .

    37*1

    F! 30 "- 01 3

    $ &K - S

    !30530

  • 56

    /) - !

    +-/ " , *

    +-/

  • 57

    self.ok=Button(self.raiz, width=8, command=self.testar, text='OK')

    self.ok.grid(row=4, column=1, padx=2, pady=3)

    self.close=Button(self.raiz, width=8, command=self.fechar, text='Fechar') self.close.grid(row=4, column=2, padx=2, pady=3)

    def testar(self): if self.nome.get()==self.senha.get()[::-1]: self.msg['text']='Senha correta!' else: self.msg['text']='Senha incorreta!'

    def fechar(self): self.raiz.destroy()

    inst1=Tk() Griding(inst1) inst1.mainloop()

    '> !&$E

    78O$4$8D ' $

    C6

  • 58

    ! 3J:-;-,$

    3718!!"

    &! 0 $ !

    30

    . *,f* 37 0

    "&((*,,

    F"/8

    61

    *"*!

    "

    #;=

    "P)

    ,$

    & 1 $& #" " *

    *"=

    8*

    FK1#;=

    !!1*30&!

    !F3"

    *!**!&"

    )& $ 8

    1* 37 F 30

    80)"8#;=9"

    K&$!*

    8! K&830

    8

  • 59

    &

    "*0&)LM!

    6*&""0&

    *)

    " " 30

    ")K

    K!*&5

    30 " 1*! P

    "30

    30&

    /&**7"&

    *))&:!8&!*

    "&1*)&$

    !&8

    &8!

    "

  • 60

    ) 5*. ) 1K

    3 9 >E " $ )! *9

  • 61

    P *0 *1 -! &

    30"

    **4.!*

    * 1 ) * 1

    *9

    ;!$6&J,!

    $&/! ! K

    0 1 30

    30!/!

    3018&"

    F!*"S