impossible d'appeler une fonction depuis une autre fonction
Salut, j'essaie d'utiliser tkinter pour créer une page avec un bouton qui, lorsqu'il est enfoncé, changera l'état d'une zone de saisie sur cette page de lecture seule à normal.
voici mon code où la fonction appelée par le bouton est dans la classe mon script s'exécute toujours mais le bouton ne fait rien:
class playlist(tkinter.Frame): #creates playlist page
def __init__(self, parent, controller):
tkinter.Frame.__init__(self, parent)
A4 = tkinter.Canvas(self, bg="black", bd=20, height=1080, width=1920, relief="sunken") #creates black background
AC = tkinter.Label(self, bg="white", bd=5, height=45, width=225,) #creates white canvas
B1 = tkinter.Button(self, anchor="center", height=1, width=6, bg="#16EBF2", activebackground="#23F216", relief="sunken",
font=("Elephant", 24), text="Home", command=lambda: controller.show_frame(homepage))
B2 = tkinter.Button(self, anchor="center", height=1, width=4, bg="#16EBF2", activebackground="#23F216", relief="sunken",
font=("Elephant", 24), text="Edit", command=lambda: edit)
E1 = tkinter.Entry(self, state="readonly", text="Name:", bd=5, bg="red", font=("Elephant", 24), exportselection=0)
E1.pack()
A4.pack(fill="both", expand=True)
AC.pack()
B1.pack()
B2.pack()
A4.create_window(960, 550, anchor="center", window=AC) #puts new canvas on background
A4.create_window(200, 80, anchor="center", window=B1) #adds buttons to canvas
A4.create_window(960, 80, anchor="center", window=E1)
A4.create_window(650, 80, anchor="center", window=B2)
def edit():
E1.configure(state = "normal")
B2.configure(text="Done")
E1.pack()
B2.pack()
App.update_idletasks()
de cette façon, la fonction ne peut pas définir E1 ou B2, j'ai donc essayé une version où la fonction est dans la première fonction:
class playlist(tkinter.Frame): #creates playlist page
def __init__(self, parent, controller):
tkinter.Frame.__init__(self, parent)
A4 = tkinter.Canvas(self, bg="black", bd=20, height=1080, width=1920, relief="sunken") #creates black background
AC = tkinter.Label(self, bg="white", bd=5, height=45, width=225,) #creates white canvas
B1 = tkinter.Button(self, anchor="center", height=1, width=6, bg="#16EBF2", activebackground="#23F216", relief="sunken",
font=("Elephant", 24), text="Home", command=lambda: controller.show_frame(homepage))
B2 = tkinter.Button(self, anchor="center", height=1, width=4, bg="#16EBF2", activebackground="#23F216", relief="sunken",
font=("Elephant", 24), text="Edit", command=lambda: edit)
E1 = tkinter.Entry(self, state="readonly", text="Name:", bd=5, bg="red", font=("Elephant", 24), exportselection=0)
E1.pack()
A4.pack(fill="both", expand=True)
AC.pack()
B1.pack()
B2.pack()
A4.create_window(960, 550, anchor="center", window=AC) #puts new canvas on background
A4.create_window(200, 80, anchor="center", window=B1) #adds buttons to canvas
A4.create_window(960, 80, anchor="center", window=E1)
A4.create_window(650, 80, anchor="center", window=B2)
def edit():
E1.configure(state = "normal")
B2.configure(text="Done")
E1.pack()
B2.pack()
App.update_idletasks()
de cette façon, le script s'exécute toujours et le bouton ne fait toujours rien. Aucune erreur ne fonctionne tout simplement. Merci d'avance :)
Ma principale recommandation serait de jeter un coup d'œil aux classes python sans tkinter d'abord, et de se familiariser avec la construction d'une classe avant d'y revenir.
Fondamentalement, vous devez affecter vos widgets tkinter aux attributs de votre classe, afin qu'ils puissent être référencés de n'importe où ailleurs dans votre classe. c'est à dire. lorsque vous créez vos boutons qui doivent être référencés ultérieurement dans votre edit
méthode, attribuez-les à des attributs:
self.B1 = tkinter.Button(self, ...)
self.B2 = tkinter.Button(self, ...)
Ensuite, pour que votre edit
méthode puisse y accéder plus tard, l'instance de classe,, lui self
est passée, vous devez donc donner à votre méthode d'édition un self
paramètre, ie.
def edit(self):
# then you can reference your attributes which store the widgets
self.B1.config(...)
Enfin, la commande de votre bouton self.B2
doit simplement être une référence à la edit
méthode, vous n'avez pas besoin d'une instruction lambda, c'est-à-dire.
Button(...command=self.edit)