ChiPlusPlus
ChiPlusPlus

Reputation: 209

check a triggered event with tkinter

Assuming the following line of code,my problem is that when the user click on update twice ,the window is displayed twice. Is there a simple way to disable this feature and check if a widget is displayed?

filemenu.add_command(label="update...", command=CreateUpdateWindow)



def CreateUpdateWindow():
window=Toplevel()
window.title("update")

Upvotes: 0

Views: 451

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385870

You can disable the menu entry with the entryconfigure method

filemenu.entryconfigure("update...", state="disabled")

If you disable it, you'll probably want to put in some code to re-enable it if the user deletes the window.

Alternatively, you can check for the existence of the window, and only create it if it doesn't exist. Here's a fully working example:

import Tkinter as tk

window = None
def CreateUpdateWindow():
    global window
    if window is None or not window.winfo_exists():
        window = tk.Toplevel()
        window.title("update")
    window.lift()

root = tk.Tk()
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar)
filemenu.add_command(label="update...", command=CreateUpdateWindow)
menubar.add_cascade(label="File", menu=filemenu)
root.configure(menu=menubar)
root.mainloop()

Upvotes: 1

Related Questions