I'm trying to bind a list, so I can update my widgets when the user would select another value. That list is placed in a frame, itself defined in a class and called by a "controller/frame manager"
As the bind event works on the opening of the frame, I guess my problem is that it is placed in the __init__
method. I did try to create another method and call it from the master, just after showing the frame, but got no result from it ?!
Any idea where to search for a path to the solution ? ListBoxSelect is in the StartPage here, but would be useful in others too !
import tkinter as tk
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.switch_frame(StartPage)
def switch_frame(self, frame_class):
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.config(width=300, height=300)
self._frame.pack()
class StartPage(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
tk.Label(self, text="This is the start page").place(relx=0.1, rely=0.1)
tk.Button(self, text="Open page one",
command=lambda: master.switch_frame(PageOne)).place(relx=0.1, rely=0.3)
tk.Entry(self, name="txt_my_entry").place(relx=0.1, rely=0.8)
tk.Entry(self, name="num_my_entry").place(relx=0.1, rely=0.9)
self.listbox = tk.Listbox(self)
self.listbox.place(relx=0.6, rely=0.1)
self.listbox.insert(1, 'one')
self.listbox.insert(2, 'two')
self.listbox.insert(3, 'three')
self.listbox.bind('<<ListboxSelect>>', print(self.listbox.get("active")))
class PageOne(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
tk.Label(self, text="This is page one").place(relx=0.1, rely=0.1)
tk.Button(self, text="Return to start page",
command=lambda: master.switch_frame(StartPage)).place(relx=0.1, rely=0.3)
tk.Entry(self, name="txt_my_entry").place(relx=0.1, rely=0.9)
if __name__ == "__main__":
app = SampleApp()
app.geometry("300x300")
app.mainloop()
Thank you !