associação pythonbrasil[11] django zope/plone planet Início Logado como (Entrar)

Revisão 1e 2004-08-16 18:27:02

Excluir mensagem

SorteadorDeElemento

Receita: Sorteador de Elementos

Na empresa onde trabalho temos o hábito de promover sorteios para a distribuição de brindes que ganhamos em feiras, congressos e coisas do tipo. Recentemente ganhei um convite do Gmail e resolvi sortear entre o pessoal aqui da empresa. Para isso fiz o programinha abaixo... Sintam-se à vontade para melhorar ele :)

Código

   1 #!/usr/bin/env python
   2 # -*- coding: ISO-8859-1 -*-
   3 
   4 from Tkinter import *
   5 from Dialog import Dialog
   6 import time
   7 import random
   8 
   9 class MainFrame(Frame):
  10     def __init__(self, parent=None):
  11         Frame.__init__(self, parent)
  12         self.master.title("Sorteia")
  13         
  14         self.text = StringVar()
  15         self.nome = StringVar()
  16         self.createWidgets()
  17         self.pack(padx=10, pady=10)
  18 
  19         
  20     def createWidgets(self):
  21         panel1 = Frame(self)
  22         nomeL = Label(panel1, text="Nome:")
  23         nomeL.pack(side=LEFT)
  24         self.nomeE = Entry(panel1, \
  25             textvariable=self.nome, width=30)
  26         self.nomeE.pack(side=LEFT)
  27         self.nomeE.bind("<Return>", self.addEnter)
  28         addB = Button(panel1, text="  +  ", \
  29                       command=self.adicionar)
  30         addB.pack(side=LEFT)
  31         subB = Button(panel1, text="  -  ", \
  32                       command=self.remover)
  33         subB.pack(side=LEFT)
  34         panel1.pack(side=TOP)
  35 
  36         panel2 = Frame(self)
  37         
  38         scrollbar = Scrollbar(panel2, orient=VERTICAL)
  39         self.listbox = Listbox(panel2, \
  40                        yscrollcommand=scrollbar.set, \
  41                        font=("System", 10, "bold"))
  42         self.listbox.pack(side=LEFT, fill=BOTH, expand=1)
  43         scrollbar.config(command=self.listbox.yview)
  44         scrollbar.pack(side=RIGHT, fill=Y)
  45 
  46         panel2.pack(side=TOP, fill=BOTH, \
  47                               expand=Y, pady=10)
  48 
  49         panel3 = Frame(self)
  50         
  51         sorteiaB = Button(panel3, text="Sorteia",\
  52                           font=("Arial", 10, "bold"), \
  53                           command=self.sorteia)
  54 
  55         sorteiaB.pack(side=TOP, fill=BOTH, expand=Y)
  56 
  57         nomeSorteadoT = Label(panel3, \
  58                              textvariable=self.text, \
  59                              font=("Arial", 18, "bold"))
  60         nomeSorteadoT.pack(side=TOP, fill=BOTH, \
  61                              expand=Y, pady=20)
  62 
  63         panel3.pack(side=TOP, fill=BOTH, expand=Y)
  64 
  65 
  66     def sorteia(self):
  67         listaSorteia = self._getLista()
  68         
  69         if not len(listaSorteia):
  70             return
  71         
  72         random.shuffle(listaSorteia)
  73         sorteado = random.randint(0, \
  74                          len(listaSorteia) - 1)
  75         self.text.set(listaSorteia[sorteado])
  76 
  77     def _getLista(self):
  78         lista = []
  79         for element in range(self.listbox.size()):
  80             lista.append(self.listbox.get(element))
  81         return lista
  82     
  83     def adicionar(self):
  84         nome = self.nome.get()
  85         if not len(nome):
  86             Dialog(self, title="Erro!", \
  87                          text="Nome inválido", \
  88                          bitmap='error', \
  89                          default=0, \
  90                          strings=('OK',))
  91             return
  92 
  93         if nome in self._getLista():
  94             Dialog(self, title="Erro!", \
  95                          text="Já cadastrado", \
  96                          bitmap='error', \
  97                          default=0, \
  98                          strings=('OK',))
  99             return
 100 
 101         self.listbox.insert(END, nome)
 102         self.limpaCampos()
 103 
 104     def addEnter(self, ev):
 105         self.adicionar()
 106         
 107     def remover(self):
 108         self.listbox.delete(ANCHOR)
 109 
 110     def limpaCampos(self):
 111         self.nome.set("")
 112         self.nomeE.focus()
 113         
 114 
 115 
 116 def main():
 117     frm = MainFrame()
 118     frm.mainloop()
 119 
 120 if __name__ == '__main__':
 121     main()

Uma das coisas que tentei fazer e não consegui é um 'efeito visual' que faça com que os nomes fiquem passando antes de parar no nome realmente sorteado. Eu fiz mudar mas a tela não atualizava. Alguém sabe como fazer isso? -- OsvaldoSantanaNeto

Volta para CookBook.


OsvaldoSantanaNeto