der Übung halber will ich ein kleines kniffel-spiel programmieren. Um zu wählen welche würfel man behalten will benutze ich checkbuttons.
Mein erster ansatz funktionierte nicht, da hab ich es dann etwas ausführlicher gemacht und nun geht's.
Meine Frage: was hab ich im ersten Anlauf falsch gemacht????
Hier mal ein stück code aufs wesentliche reduziert:
def roll1 funktioniert
def roll2 nicht....warum??
Code: Alles auswählen
#!/usr/bin/python
from random import randint
from Tkinter import *
root=Tk()
root.title("Checkbuttons")
w1=0
w2=0
w3=0
w4=0
w5=0
r=0
######################################
# Folgende variante funktioniert: #
def roll1():
global w1,w2,w3,w4,w5,r,list
r=r+1
print "Wurf Nr: ",r
if r<=3:
if v1.get()==0:
w1=randint(1,6)
if v2.get()==0:
w2=randint(1,6)
if v3.get()==0:
w3=randint(1,6)
if v4.get()==0:
w4=randint(1,6)
if v5.get()==0:
w5=randint(1,6)
print w1,w2,w3,w4,w5
else:
print "schon 3 versuche"
update()
##################################
# Die hier funktioniert nicht: #
def roll2():
global w1,w2,w3,w4,w5,r,list
r=r+1
print "Wurf Nr: ",r
if r<=3:
for a in range(1,6):
exec ("print v"+str(a)+".get()") # Checkboxes funktionieren
if "v"+str(a)+".get()"!=1:
w=randint(1,6)
if a==1: w1=w
elif a==2: w2=w
elif a==3: w3=w
elif a==4: w4=w
elif a==5: w5=w
print w1,w2,w3,w4,w5
else:
print "schon 3 versuche"
update()
def update():
ergebnis1.configure(text=w1)
ergebnis1.update()
ergebnis2.configure(text=w2)
ergebnis2.update()
ergebnis3.configure(text=w3)
ergebnis3.update()
ergebnis4.configure(text=w4)
ergebnis4.update()
ergebnis5.configure(text=w5)
ergebnis5.update()
ergebnis1=Label(root,text=w1,width=5)
ergebnis1.grid(row=0,column=4)
ergebnis2=Label(root,text=w2,width=5)
ergebnis2.grid(row=0,column=5)
ergebnis3=Label(root,text=w3,width=5)
ergebnis3.grid(row=0,column=6)
ergebnis4=Label(root,text=w4,width=5)
ergebnis4.grid(row=0,column=7)
ergebnis5=Label(root,text=w5,width=5)
ergebnis5.grid(row=0,column=8)
v1=IntVar()
v2=IntVar()
v3=IntVar()
v4=IntVar()
v5=IntVar()
hold1=Checkbutton(root,variable=v1).grid(row=1,column=4)
hold2=Checkbutton(root,variable=v2).grid(row=1,column=5)
hold3=Checkbutton(root,variable=v3).grid(row=1,column=6)
hold4=Checkbutton(root,variable=v4).grid(row=1,column=7)
hold5=Checkbutton(root,variable=v5).grid(row=1,column=8)
butroll=Button(root,text="Roll 1!",command=lambda: roll1()).grid(row=5,column=5)
butroll=Button(root,text="Roll 2!",command=lambda: roll2()).grid(row=5,column=7)
root.mainloop()
thomas