1

我有一个Toplevel小部件,只要用户点击窗口外,我就想销毁它。我尝试在互联网上寻找解决方案,但似乎没有关于这个话题的文章讨论。

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

4

3 回答 3

4

你可以尝试这样的事情: fen 是你的顶级

fen.bind("<FocusOut>", fen.quit)
于 2016-08-02T14:40:21.347 回答
1

我有一个类似的问题,并解决了它。以下示例工作正常。它在主窗口顶部显示一个顶级窗口,作为自定义配置菜单。每当用户单击其他位置时,配置菜单就会消失。

警告:不要在顶层窗口上使用 .transient(parent) ,否则会出现您描述的症状:单击主窗口时,菜单不会消失。您可以尝试在下面的示例中取消注释“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()
于 2017-10-30T20:16:29.040 回答
0

当我有 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()

它有一个窗口被聚焦的次数的计数器。当计数器达到 0 时,它会破坏窗口。

于 2021-02-18T16:44:14.830 回答