Tkinter: генерируется событие <>, хотя окна верхнего уровня имеют полный контроль ⇐ Python
-
Anonymous
Tkinter: генерируется событие <>, хотя окна верхнего уровня имеют полный контроль
I'm still quite a beginner when it comes to Tkinter, but I'd like to ask a question about my source code. I have a very simple constellation here, a listbox and a toplevel window. When I select "create entry" from the listbox, the toplevel window is created. I have configured it so that the user has to make entries here and the toplevel window has full control. So you can't click on any other widgets on the main window. However, it now happens that I write something in the first input field and jump to the 2nd with the tab key, that the onselect event is triggered. That shouldn't actually happen, should it? I see that the selection in the list box is canceled, which of course leads to the event.
from tkinter import Label, Tk, Entry, Toplevel, Button, Listbox def onselect(event): manipulateTree(listMenu.curselection()) def manipulateTree(selection): global nameCategory if selection[0] == 1: #listMenu.place_forget() #listMenu.selection_clear(0, "end") createEntryPopup() def createEntryPopup(): def cancel(): #listMenu.bind(sequence="", func=onselect) entryPopup.destroy() def saveEntry(): pass x = int(mainWindow.winfo_x() + mainWindow.winfo_width() / 2 - 400) y = int(mainWindow.winfo_y() + mainWindow.winfo_height() / 2 - 200) fontName = ("Arial", 12) #listMenu.unbind(sequence="") entryPopup = Toplevel(master=mainWindow, padx=20, pady=20) entryPopup.geometry(newGeometry=f"800x400+{x}+{y}") entryPopup.resizable(False, False) entryPopup.focus() entryPopup.title("Data") entryPopup.attributes('-topmost', True) label1= Label(master=entryPopup, text="Entry name ", font=fontName) label1.grid(row=0,column=0, sticky="w") entry1= Entry(master=entryPopup, width=15,font=fontName) entry1.grid(row=0,column=1) label2 = Label(master=entryPopup, text="Location ", font=fontName) label2.grid(row=1,column=0, sticky="w") entry2= Entry(master=entryPopup, width=15,font=fontName) entry2.grid(row=1,column=1) button1 = Button(master=entryPopup, text="Cancel", font=fontName, command=cancel, width=8,) button1.grid(row=5,column=0) button2 = Button(master=entryPopup, text="Save", command=saveEntry, width=8, font=fontName) button2.grid(row=5,column=2) entry1.focus() entryPopup.wait_visibility() entryPopup.grab_set() entryPopup.wait_window() mainWindow = Tk() mainWindow.geometry(newGeometry="1200x600") mainWindow.title(string="Password Manager") options_list = ["Create category", "Create entry", "Rename", "Delete", "Move"] listMenu = Listbox(master=mainWindow, height=5, width=15, fg="black", selectmode="BROWSE", relief="raised",activestyle="none", font=("Arial", 12), border=2) listMenu.bind(sequence="", func=onselect) for x in range(0,len(options_list)): listMenu.insert(x,options_list[x]) listMenu.place(x=20,y=20) mainWindow.mainloop() I have already tried to cancel the selection before calling createEntryPopup(), but it didn't help. Only removing the event with listMenu.unbind(sequence="") has led to success. What am I missing?
Источник: https://stackoverflow.com/questions/780 ... has-full-c
I'm still quite a beginner when it comes to Tkinter, but I'd like to ask a question about my source code. I have a very simple constellation here, a listbox and a toplevel window. When I select "create entry" from the listbox, the toplevel window is created. I have configured it so that the user has to make entries here and the toplevel window has full control. So you can't click on any other widgets on the main window. However, it now happens that I write something in the first input field and jump to the 2nd with the tab key, that the onselect event is triggered. That shouldn't actually happen, should it? I see that the selection in the list box is canceled, which of course leads to the event.
from tkinter import Label, Tk, Entry, Toplevel, Button, Listbox def onselect(event): manipulateTree(listMenu.curselection()) def manipulateTree(selection): global nameCategory if selection[0] == 1: #listMenu.place_forget() #listMenu.selection_clear(0, "end") createEntryPopup() def createEntryPopup(): def cancel(): #listMenu.bind(sequence="", func=onselect) entryPopup.destroy() def saveEntry(): pass x = int(mainWindow.winfo_x() + mainWindow.winfo_width() / 2 - 400) y = int(mainWindow.winfo_y() + mainWindow.winfo_height() / 2 - 200) fontName = ("Arial", 12) #listMenu.unbind(sequence="") entryPopup = Toplevel(master=mainWindow, padx=20, pady=20) entryPopup.geometry(newGeometry=f"800x400+{x}+{y}") entryPopup.resizable(False, False) entryPopup.focus() entryPopup.title("Data") entryPopup.attributes('-topmost', True) label1= Label(master=entryPopup, text="Entry name ", font=fontName) label1.grid(row=0,column=0, sticky="w") entry1= Entry(master=entryPopup, width=15,font=fontName) entry1.grid(row=0,column=1) label2 = Label(master=entryPopup, text="Location ", font=fontName) label2.grid(row=1,column=0, sticky="w") entry2= Entry(master=entryPopup, width=15,font=fontName) entry2.grid(row=1,column=1) button1 = Button(master=entryPopup, text="Cancel", font=fontName, command=cancel, width=8,) button1.grid(row=5,column=0) button2 = Button(master=entryPopup, text="Save", command=saveEntry, width=8, font=fontName) button2.grid(row=5,column=2) entry1.focus() entryPopup.wait_visibility() entryPopup.grab_set() entryPopup.wait_window() mainWindow = Tk() mainWindow.geometry(newGeometry="1200x600") mainWindow.title(string="Password Manager") options_list = ["Create category", "Create entry", "Rename", "Delete", "Move"] listMenu = Listbox(master=mainWindow, height=5, width=15, fg="black", selectmode="BROWSE", relief="raised",activestyle="none", font=("Arial", 12), border=2) listMenu.bind(sequence="", func=onselect) for x in range(0,len(options_list)): listMenu.insert(x,options_list[x]) listMenu.place(x=20,y=20) mainWindow.mainloop() I have already tried to cancel the selection before calling createEntryPopup(), but it didn't help. Only removing the event with listMenu.unbind(sequence="") has led to success. What am I missing?
Источник: https://stackoverflow.com/questions/780 ... has-full-c