bonjour à tous,
dans une application, je crée des boutons à l'aide d'une boucle for,
les boutons sont crées comme prévu, et je voudrais récupérer l'indice de chaque bouton,
je n'y arrive pas, j'ai épluché le web et les docs, j'y ai passé la nuit, pouvez vous s'il plaît éclairer ma lanterne, est-ce une bonne méthode pour créer des boutons ?
dans la fonction actBouton(self), je voudrais récupérer le texte de chaque
boutons contenu dans la liste self.alphab
par avance merci !
voici mon code :
import tkinter
class MonAppli(tkinter.Tk):
def __init__(self, parent):
tkinter.Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
self.geometry("600x200+10+10")
# le texte des boutons
self.alphab = ["a","â","b","c","ç","d","e","é","è","ê","ë","f"
,"g","h","i","î","ï","j","k","l","m","n","o","ô","p","q"
,"r","s","t","u","û","v","w","x","y","z"]
self.fram1 = tkinter.Frame(self, bg="#FFD100")
self.fram1.pack(side="top",fill="x")
self.btPri = {}
for i in range(36):
self.btPri[i] = tkinter.Button(self.fram1, text=self.alphab[i], width = 2, height = 2, padx = 3, pady = 3,command=self.actBouton).grid(row=0,column=i)
self.textPriVariable = tkinter.StringVar()
self.textPri = tkinter.Label(self.fram1, textvariable=self.textPriVariable).grid(row=1,column=0)
def actBouton(self):
self.textPriVariable.set("bouton cliqué")
if __name__ == "__main__":
app = MonAppli(None)
app.title("mon application")
app.mainloop()
Partager