Tkinter Toplevel:未聚焦时销毁 window

Tkinter Toplevel : Destroy window when not focused

我有一个 Toplevel 小部件,我想在用户点击 window 时销毁它。我试着在网上寻找解决方案,但似乎没有文章讨论这个话题。

我怎样才能做到这一点。感谢您的帮助!

您可以尝试类似的操作: fen 是你的顶层

fen.bind("<FocusOut>", fen.quit)

我遇到了类似的问题,并已解决。以下示例工作正常。 它在主 window 之上显示一个 Toplevel window 作为自定义配置菜单。 每当用户点击其他地方时,配置菜单就会消失。

警告:不要在 Toplevel window 上使用 .transient(parent) ,否则会出现您描述的症状:单击主 window 时,菜单不会消失.您可以尝试在下面的示例中取消注释 "self.transient(parent)" 行以重现您的问题。

示例:

import Tix


class MainWindow(Tix.Toplevel):

    def __init__(self, parent):
        # Init
        self.parent = parent
        Tix.Toplevel.__init__(self, parent)
        self.protocol("WM_DELETE_WINDOW", self.destroy)
        w = Tix.Button(self, text="Config menu", command=self.openMenu)
        w.pack()

    def openMenu(self):
        configWindow = ConfigWindow(self)
        configWindow.focus_set()


class ConfigWindow(Tix.Toplevel):

    def __init__(self, parent):
        # Init
        self.parent = parent
        Tix.Toplevel.__init__(self, parent)
        # If you uncomment this line it reproduces the issue you described
        #self.transient(parent)
        # Hides the window while it is being configured
        self.withdraw()
        # Position the menu under the mouse
        x = self.parent.winfo_pointerx()
        y = self.parent.winfo_pointery()
        self.geometry("+%d+%d" % (x, y))
        # Configure the window without borders
        self.update_idletasks() # Mandatory for .overrideredirect() method
        self.overrideredirect(True)
        # Binding to close the menu if user does something else
        self.bind("<FocusOut>", self.close)  # User focus on another window
        self.bind("<Escape>", self.close)    # User press Escape
        self.protocol("WM_DELETE_WINDOW", self.close)
        # The configuration items
        w = Tix.Checkbutton(self, text="Config item")
        w.pack()
        # Show the window
        self.deiconify()


    def close(self, event=None):
        self.parent.focus_set()
        self.destroy()


tixRoot = Tix.Tk()
tixRoot.withdraw()
app = MainWindow(tixRoot)
app.mainloop()

当我有 2 个 <tkinter.Entry> 时,其他解决方案对我不起作用,但我发现了这个:

import tkinter as tk

focus = 0

def focus_in(event):
    global focus
    focus += 1

def focus_out(event):
    global focus
    focus -= 1
    if focus == 0:
        root.destroy()

root = tk.Toplevel()
root.bind("<FocusIn>", focus_in)
root.bind("<FocusOut>", focus_out)
root.mainloop()

它有一个计数器,用于记录 window 被聚焦的次数。它会在计数器达到 0 时销毁 window。