使用tkinter检查触发事件

时间:2015-07-06 16:53:19

标签: python tkinter

假设以下代码行,我的问题是当用户点击两次更新时,窗口会显示两次。 是否有一种简单的方法可以禁用此功能并检查是否显示了小部件?

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



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

1 个答案:

答案 0 :(得分:1)

您可以使用entryconfigure方法

禁用菜单条目
filemenu.entryconfigure("update...", state="disabled")

如果你禁用它,你可能想要输入一些代码,以便在用户删除窗口时重新启用它。

或者,您可以检查窗口是否存在,只有在窗口不存在时才创建窗口。这是一个完全有效的例子:

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()
相关问题