Stacked Widgets launch as their own event

I have a problem with binding to event handlers.

I have two stacked frames placed on mainFrame. Each frame has its own button. When I click on one button, the corresponding frame is removed.

In each frame, I have one list. These lists are in the same position on mainFrame. When I select one item in the first list, it populates another list with new content. Then I press the second frame button to access the second list. I duplicated (with new names) an event to do the same with my second list, to populate the third list.

However, when I select one item in the second list, it seems that the event of the first list is also fired, but only once (if I repeat my selection, it will only trigger the event of the second list).

EDIT: I put a “short” version of my code as requested.

When I select db1 or db2, the first sheet, frame2 goes up and prints test2. Then I select item1 or item2, and how test2 / test1 is printed, not just test1

from tkinter import *

class Page(Frame):
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)
    def show(self):
        self.lift()

class Page1(Page):

    def dbCheckCommand(self, event, page2):
        page2.collections.delete(0, END)
        print("test2")
        page2.collections.insert(END, "item1")
        page2.collections.insert(END, "item2")

    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        self.grid_columnconfigure(0, weight=1)
        self.databases = Listbox(self, height=2)
        self.grid_rowconfigure(1, weight=1)
        self.databases.insert(END, "db1")
        self.databases.insert(END, "db2")
        self.databases.grid(row=1, sticky="nsew")

class Page2(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        self.grid_columnconfigure(0, weight=1)
        self.colls = []
        self.collections = Listbox(self, height=2)
        self.grid_rowconfigure(1, weight=1)
        self.collections.grid(row=1, sticky="nsew")

class MainView(Frame):

    def on_p1_listbox_selection(self, event, page1, page2):
        page1.dbCheckCommand(event, page2)
        page2.lift()

    def on_p2_listbox_selection(self, event):
        print("test1")

    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)
        self.grid_columnconfigure(1, weight=5)
        buttonframe = Frame(self)
        container = Frame(self)
        buttonframe.grid(column=0, row=0, sticky="nsew")

        container.grid(column=1, row=0, sticky="nsew")
        container.grid_columnconfigure(0, weight=1)
        container.grid_rowconfigure(0, weight=1)

        p1 = Page1(container)
        p2 = Page2(container)

        p1.grid(column=0, row=0, sticky="nsew")
        p2.grid(column=0, row=0, sticky="nsew")


        b1 = Button(buttonframe, text="1 - Server & Database", command=p1.lift)
        b2 = Button(buttonframe, text="2 - Collection", command=p2.lift)

        b1.pack(fill="both", expand=1)
        b2.pack(fill="both", expand=1)


        p1.databases.bind("<<ListboxSelect>>", lambda event: self.on_p1_listbox_selection(event, p1, p2))
        p2.collections.bind("<<ListboxSelect>>", lambda event: self.on_p2_listbox_selection(event))

        p1.show()


if __name__ == "__main__":
    root = Tk()
    main = MainView(root)
    main.pack(fill="both", expand=1)
    root.wm_geometry("1100x500")
    root.wm_title("MongoDB Timed Sample Generator")
    root.mainloop()
+4
source share
1 answer

An event is <<ListboxSelect>>fired whenever a selection changes. This means that both when choosing a new choice, and when choosing the current selection.

X selection. X, , . , , <<ListboxSelect>>.

, , -, . , exportselection False .

:

self.databases = Listbox(..., exportselection=False)
...
self.collections = Listbox(..., exportselection=False)
+3

Source: https://habr.com/ru/post/1679153/


All Articles