0

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 !

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Jacques
  • 11
  • 4
  • You bound the `<>` event to the *result* of printing `self.listbox.get("active")` immediately - which is None. You need to bind a function that will do the printing (or other desired activity) when it is called. – jasonharper Nov 08 '20 at 15:49
  • Looks like you are totally right ; guess I was looking int he wrong direction... as you just posted a comment not an answer, I do not know how to thank you with a good point, but I do thank you the same to not point out my stupidity ! :) – Jacques Nov 08 '20 at 16:11

0 Answers0