[Python] il modulo random
lorenzo
renzocavallini a alice.it
Dom 15 Apr 2007 10:29:49 CEST
Alle 02:03, mercoledė 11 aprile 2007, Paolo ha scritto:
> email
io ho usato il modulo random per creare ina lista di mumeri casuali cosė
import random
from Tkinter import *
class variabili :
lista_bt=[['question','error','error','info','info','warning','warning'],
['n','n','n','n','n','n','n']]
bottoni = ['zero','uno','due','tre','quattro','cinque','sei']
cont_bottoni =[1,0,0,0,0,0,0] # flag per il controllo per vedere se sono
gia stati premuti i bottoni
bt_press = 0
class form1:
"""definizione della form"""
def __init__(self):
self.__root = tk = Tk()
self.__root.title('testo_scorrevole')
self.__root.maxsize(1000,400)
self.__root.minsize(300,210)
self.button1 =Button(tk,bitmap='question',command=self.__command1)
self.button1.place(x=25,y=50,height=50,width=50)
self.button2 =Button(tk,bitmap='question',command=self.__command2)
self.button2.place(x=100,y=50,height=50,width=50)
self.button3 =Button(tk,bitmap='question',command=self.__command3)
self.button3.place(x=175,y=50,height=50,width=50)
self.button4 =Button(tk,bitmap='question',command=self.__command4)
self.button4.place(x=25,y=120,height=50,width=50)
self.button5 =Button(tk,bitmap='question',command=self.__command5)
self.button5.place(x=100,y=120,height=50,width=50)
self.button6 =Button(tk,bitmap='question',command=self.__command6)
self.button6.place(x=175,y=120,height=50,width=50)
self.button7 =Button(tk,text='start',command=self.__command7)
self.button7.place(x=50,y=10,height=20,width=50)
self.label1 =Label(tk,text='',bg='white')
self.label1.place(x=120,y=10,height=20,width=100)
def __command1(self):
if variabili.cont_bottoni[1] == 0:
self.button1.configure (bitmap=variabili.bottoni[1])
if variabili.bt_press == 0:
variabili.bt_press = 1
variabili.cont_bottoni[1] = 1
else:
if variabili.bottoni[1] ==
variabili.bottoni[variabili.bt_press]:
variabili.bt_press = 0
variabili.cont_bottoni[1] = 1
self.verifica_imm ()
else:
variabili.cont_bottoni[1] = 0
variabili.cont_bottoni[variabili.bt_press] = 0
variabili.bt_press = 0
self.verifica_imm ()
def __command2(self):
if variabili.cont_bottoni[2] == 0:
self.button2.configure (bitmap=variabili.bottoni[2])
if variabili.bt_press == 0:
variabili.bt_press = 2
variabili.cont_bottoni[2] = 1
else:
if variabili.bottoni[2] ==
variabili.bottoni[variabili.bt_press]:
variabili.bt_press = 0
variabili.cont_bottoni[2] = 1
self.verifica_imm ()
else:
variabili.cont_bottoni[2] = 0
variabili.cont_bottoni[variabili.bt_press] = 0
variabili.bt_press = 0
self.verifica_imm ()
def __command3(self):
if variabili.cont_bottoni[3] == 0:
self.button3.configure (bitmap=variabili.bottoni[3])
if variabili.bt_press == 0:
variabili.bt_press = 3
variabili.cont_bottoni[3] = 1
else:
if variabili.bottoni[3] ==
variabili.bottoni[variabili.bt_press]:
variabili.bt_press = 0
variabili.cont_bottoni[3] = 1
self.verifica_imm ()
else:
variabili.cont_bottoni[3] = 0
variabili.cont_bottoni[variabili.bt_press] = 0
variabili.bt_press = 0
self.verifica_imm ()
def __command4(self):
if variabili.cont_bottoni[4] == 0:
self.button4.configure (bitmap=variabili.bottoni[4])
if variabili.bt_press == 0:
variabili.bt_press = 4
variabili.cont_bottoni[4] = 1
else:
if variabili.bottoni[4] ==
variabili.bottoni[variabili.bt_press]:
variabili.bt_press = 0
variabili.cont_bottoni[4] = 1
self.verifica_imm ()
else:
variabili.cont_bottoni[4] = 0
variabili.cont_bottoni[variabili.bt_press] = 0
variabili.bt_press = 0
self.verifica_imm ()
def __command5(self):
if variabili.cont_bottoni[5] == 0:
self.button5.configure (bitmap=variabili.bottoni[5])
if variabili.bt_press == 0:
variabili.bt_press = 5
variabili.cont_bottoni[5] = 1
else:
if variabili.bottoni[5] ==
variabili.bottoni[variabili.bt_press]:
variabili.bt_press = 0
variabili.cont_bottoni[5] = 1
self.verifica_imm ()
else:
variabili.cont_bottoni[5] = 0
variabili.cont_bottoni[variabili.bt_press] = 0
variabili.bt_press = 0
self.verifica_imm ()
def __command6(self):
if variabili.cont_bottoni[6] == 0:
self.button6.configure (bitmap=variabili.bottoni[6])
if variabili.bt_press == 0:
variabili.bt_press = 6
variabili.cont_bottoni[6] = 1
else:
if variabili.bottoni[6] ==
variabili.bottoni[variabili.bt_press]:
variabili.bt_press = 0
variabili.cont_bottoni[6] = 1
self.verifica_imm ()
else:
variabili.cont_bottoni[6] = 0
variabili.cont_bottoni[variabili.bt_press] = 0
variabili.bt_press = 0
self.verifica_imm ()
def __command7(self):
self.assegna()
self.button1.configure (bitmap=variabili.bottoni[1])
self.button2.configure (bitmap=variabili.bottoni[2])
self.button3.configure (bitmap=variabili.bottoni[3])
self.button4.configure (bitmap=variabili.bottoni[4])
self.button5.configure (bitmap=variabili.bottoni[5])
self.button6.configure (bitmap=variabili.bottoni[6])
self.__root.update()
self.__root.after(3000)
self.button1.configure (bitmap='question')
self.button2.configure (bitmap='question')
self.button3.configure (bitmap='question')
self.button4.configure (bitmap='question')
self.button5.configure (bitmap='question')
self.button6.configure (bitmap='question')
def verifica_imm (self):
self.__root.update()
self.__root.after(500)
self.button1.configure (bitmap='question')
self.button2.configure (bitmap='question')
self.button3.configure (bitmap='question')
self.button4.configure (bitmap='question')
self.button5.configure (bitmap='question')
self.button6.configure (bitmap='question')
if variabili.cont_bottoni[1] ==1 :
self.button1.configure (bitmap=variabili.bottoni[1])
if variabili.cont_bottoni[2] ==1 :
self.button2.configure (bitmap=variabili.bottoni[2])
if variabili.cont_bottoni[3] ==1 :
self.button3.configure (bitmap=variabili.bottoni[3])
if variabili.cont_bottoni[4] ==1 :
self.button4.configure (bitmap=variabili.bottoni[4])
if variabili.cont_bottoni[5] ==1 :
self.button5.configure (bitmap=variabili.bottoni[5])
if variabili.cont_bottoni[6] ==1 :
self.button6.configure (bitmap=variabili.bottoni[6])
def assegna (self):
""" assegna le figure ai bottoni"""
cont =1 #contatore per il ciclo wile
variabili.bt_press =0
for i in range (7):
variabili.cont_bottoni[i]= 0
variabili.lista_bt[1][i] = 'n'
print variabili.lista_bt[1][i]
while cont <7 :
num = random.randrange(7)
print num
if num <> 0 :
if variabili.lista_bt[1][num] == 'n' :
variabili.lista_bt[1][num] = 's'
variabili.bottoni[cont]=variabili.lista_bt[0][num]
print variabili.bottoni[cont]
cont = cont+1
def run(self):
self.__root.mainloop()
t= form1()
t.run()
nella funzione assegna ho usato la var num che assegna il valore random
Maggiori informazioni sulla lista
Python