6990
Comentário:
|
7531
|
Deleções são marcadas assim. | Adições são marcadas assim. |
Linha 2: | Linha 2: |
= Receita: Programa para adivinhacao = | = Receita: Programa para adivinhação = [attachment:AdivinhacaoTkinter.zip Baixe aqui o código completo] A lógica desse jogo é que o usuário pensa em um número de 1 à 60, e selecionando as tabelas em que o número aparece, o jogo adivinha o número que o usuário pensou. Este exemplo mostra como trabalhar com o Tkinter para carregar imagens, criar frames, trabalhar com botões e caixas de checagem, messagebox. |
Linha 227: | Linha 235: |
res = tkMessageBox.showinfo("RESULTADO ", "O valor é: "+str(self.x)) | res = tkMessageBox.showinfo("RESULTADO ", "Você pensou no número: "+str(self.x)) |
Linha 259: | Linha 267: |
Fabio Meincheim <fabio.jgs@gmail.com> | Fabio Meincheim <fabio.jgs@gmail.com> Graduando do curso de Sistemas de Informação IST - Instituto Superior Tupy de Joinville |
Linha 261: | Linha 272: |
Professor de programação do IST. |
Receita: Programa para adivinhação
[attachment:AdivinhacaoTkinter.zip Baixe aqui o código completo]
A lógica desse jogo é que o usuário pensa em um número de 1 à 60, e selecionando as tabelas em que o número aparece, o jogo adivinha o número que o usuário pensou.
Este exemplo mostra como trabalhar com o Tkinter para carregar imagens, criar frames, trabalhar com botões e caixas de checagem, messagebox.
Código
1 # -*- coding: cp1252 -*-
2 import Tkinter; Tk = Tkinter
3 import tkMessageBox
4
5 class App(Tk.Frame):
6 def __init__(self, master = None):
7 Tk.Frame.__init__ (self, master)
8
9 self.varA = Tk.IntVar()
10 self.varB = Tk.IntVar()
11 self.varC = Tk.IntVar()
12 self.varD = Tk.IntVar()
13 self.varE = Tk.IntVar()
14 self.varF = Tk.IntVar()
15
16 def criaImagens(self):
17 self.__image_runner1__ = Tk.PhotoImage('runner1',file="1.gif")
18 self.__image_runner2__ = Tk.PhotoImage('runner2',file="2.gif")
19 self.__image_runner3__ = Tk.PhotoImage('runner3',file="3.gif")
20 self.__image_runner4__ = Tk.PhotoImage('runner4',file="4.gif")
21 self.__image_runner5__ = Tk.PhotoImage('runner5',file="5.gif")
22 self.__image_runner6__ = Tk.PhotoImage('runner6',file="6.gif")
23
24 self.img_1 = Tk.Label (self, {'image' : 'runner1'})
25 self.img_2 = Tk.Label (self, {'image' : 'runner2'})
26 self.img_3 = Tk.Label (self, {'image' : 'runner3'})
27 self.img_4 = Tk.Label (self, {'image' : 'runner4'})
28 self.img_5 = Tk.Label (self, {'image' : 'runner5'})
29 self.img_6 = Tk.Label (self, {'image' : 'runner6'})
30
31 def criaCheck(self):
32 self.a = Tk.Checkbutton(self, variable=self.varA)
33 self.b = Tk.Checkbutton(self, variable=self.varB)
34 self.c = Tk.Checkbutton(self, variable=self.varC)
35 self.d = Tk.Checkbutton(self, variable=self.varD)
36 self.e = Tk.Checkbutton(self, variable=self.varE)
37 self.f = Tk.Checkbutton(self, variable=self.varF)
38
39 def criaBot(self):
40 self.btOK=Tk.Button(self, text = "Resultado", command=self.btOK)
41 self.btSAIR=Tk.Button(self, text = "Sair", command=self.btSAIR)
42
43 def configuraFrame(self):
44 self.rowconfigure (1, {'weight' : 1, 'minsize' : 30})
45 self.rowconfigure (2, {'weight' : 0, 'minsize' : 30})
46 self.rowconfigure (3, {'weight' : 0, 'minsize' : 30})
47 self.rowconfigure (4, {'weight' : 0, 'minsize' : 30})
48 self.columnconfigure (1, {'weight' : 0, 'minsize' : 30})
49 self.columnconfigure (2, {'weight' : 0, 'minsize' : 30})
50 self.columnconfigure (3, {'weight' : 0, 'minsize' : 30})
51 self.columnconfigure (4, {'weight' : 0, 'minsize' : 30})
52 self.columnconfigure (5, {'weight' : 0, 'minsize' : 30})
53
54 def posicionaObjetos(self):
55 self.img_1.grid({
56 'in' : self,
57 'column' : '1',
58 'row' : '1',
59 'sticky' : ''
60 })
61
62 self.img_2.grid({
63 'in' : self,
64 'column' : '2',
65 'row' : '1',
66 'sticky' : ''
67 })
68
69 self.img_3.grid({
70 'in' : self,
71 'column' : '3',
72 'row' : '1',
73 'sticky' : ''
74 })
75
76 self.img_4.grid({
77 'in' : self,
78 'column' : '1',
79 'row' : '3',
80 'sticky' : ''
81 })
82
83 self.img_5.grid({
84 'in' : self,
85 'column' : '2',
86 'row' : '3',
87 'sticky' : ''
88 })
89
90 self.img_6.grid({
91 'in' : self,
92 'column' : '3',
93 'row' : '3',
94 'sticky' : ''
95 })
96
97 self.a.grid({
98 'in' : self,
99 'column' : '1',
100 'row' : '2',
101 'sticky' : ''
102 })
103
104 self.b.grid({
105 'in' : self,
106 'column' : '2',
107 'row' : '2',
108 'sticky' : ''
109 })
110
111 self.c.grid({
112 'in' : self,
113 'column' : '3',
114 'row' : '2',
115 'sticky' : ''
116 })
117
118 self.d.grid({
119 'in' : self,
120 'column' : '1',
121 'row' : '4',
122 'sticky' : ''
123 })
124
125 self.e.grid({
126 'in' : self,
127 'column' : '2',
128 'row' : '4',
129 'sticky' : ''
130 })
131
132 self.f.grid({
133 'in' : self,
134 'column' : '3',
135 'row' : '4',
136 'sticky' : ''
137 })
138
139 self.btOK.grid({
140 'in' : self,
141 'column' : '2',
142 'row' : '5',
143 'sticky' : ''
144 })
145 self.btSAIR.grid({
146 'in' : self,
147 'column' : '3',
148 'row' : '5',
149 'sticky' : ''
150 })
151
152 def pegarA(self):
153 return self.varA.get()
154
155 def pegarB(self):
156 return self.varB.get()
157
158 def pegarC(self):
159 return self.varC.get()
160
161 def pegarD(self):
162 return self.varD.get()
163
164 def pegarE(self):
165 return self.varE.get()
166
167 def pegarF(self):
168 return self.varF.get()
169
170 def mostraFrame(self, **packOptions):
171 self.pack(packOptions)
172
173
174 def setres(self,res):
175 self.res = res
176
177 def btSAIR(self):
178 self.master.destroy()
179 self.destroy()
180
181 def btOK(self):
182 self.res.calcula(self)
183 self.varA.set(0)
184 self.varB.set(0)
185 self.varC.set(0)
186 self.varD.set(0)
187 self.varE.set(0)
188 self.varF.set(0)
189
190
191
192
193 class Resultado(Tk.Frame):
194 def calcula(self,app):
195 self.x = 0
196 if app.pegarA() == 1:
197 self.x+= 1
198 if app.pegarB() == 1:
199 self.x+= 2
200 if app.pegarC() == 1:
201 self.x+= 4
202 if app.pegarD() == 1:
203 self.x+= 8
204 if app.pegarE() == 1:
205 self.x+= 16
206 if app.pegarF() == 1:
207 self.x+= 32
208
209 res = self.montaTela()
210 return res
211
212 def montaTela(self, master2=None):
213
214 if self.x <= 0:
215 res = tkMessageBox.showinfo("RESULTADO ", "Você não pensou em nenhum número")
216 elif self.x > 60:
217 res = tkMessageBox.showinfo("RESULTADO ", "Por favor pense em números de 1 a 60")
218 else:
219 res = tkMessageBox.showinfo("RESULTADO ", "Você pensou no número: "+str(self.x))
220
221 return res
222
223 def btSAIR(self):
224 self.master2.destroy()
225
226 def inicia():
227 app = App()
228 res = Resultado()
229
230 app.setres(res)
231 app.criaImagens()
232 app.criaCheck()
233 app.criaBot()
234 app.configuraFrame()
235 app.posicionaObjetos()
236 app.mostraFrame(fill = 'both', expand = 1)
237
238 app.mainloop ()
239
240
241 if __name__ == '__main__':
242 inicia()
Volta para CookBook.
Fabio Meincheim <fabio.jgs@gmail.com> Graduando do curso de Sistemas de Informação IST - Instituto Superior Tupy de Joinville
Eduardo da Silva <eduardo.silva@gmail.com> Professor de programação do IST.