problème command avec boucle while
Bonjour à tous, voilà mon problème:
Je dois faire un genre de puissance 4 j'ai donc créé une grille avec des boutons au dessus de chaque colonne afin de pouvoir y mettre un jeton.
Mon problème est que quand je clic sur le bouton le jeton se met dans une nouvelle colonne.
Voici mon programme
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
from Tkinter import *
root = {0: Tk()}
yoda=PhotoImage(file="yoda.gif")
def jeton1(lig,col):
c=500+10*lig+col
root[c]=Label(root[5],relief=SOLID, padx=20, pady=20, image=yoda)
root[c].grid(row=lig-1, column=col-1)
return
transparent=PhotoImage(file="transparent.gif")
def vide(lig,col):
c=500+10*lig+col
root[c]=Label(root[5],relief=SOLID, padx=20, pady=20, image=transparent)
root[c].grid(row=lig-1, column=col-1)
return
root[4]=Frame(root[0])
root[4].pack(side=TOP)
def bouton_colonne(col):
i=1
while i<=col:
a=40+i
root[a]=Button(root[4], text=(' %d ')%(i), command=lambda:jeton1(1,i))
root[a].pack(side=LEFT, padx=25, pady=10)
i+=1
return
root[5]=Frame(root[0])
root[5].pack(side=TOP)
def grille(lig,col):
a=1
while a<=lig:
b=1
while b<=col:
vide(a,b)
b+=1
a+=1
return
grille(4,4)
bouton_colonne(4)
root[0].title('Sliding-rows')
root[0].protocol('WM_DELETE_WINDOW', root[0].quit)
root[0].mainloop()
root[0].destroy() |
Sur ce code quand j'écris command=lambda:jeton1(1,i), le i pris en compte est toujours le dernier pris en compte dans la fonction. Comment faire pour que chaque widget créé par cette boucle puisse éxecuter sa commande dans une colonne différente ?
J'espère avoir été clair, merci pour vos réponses