我有一个Toplevel
小部件,只要用户点击窗口,我就想销毁它。我尝试在互联网上寻找解决方案,但似乎没有文章讨论这个话题。
我怎样才能做到这一点。谢谢你的帮助!
答案 0 :(得分:1)
您可以尝试这样的事情: 芬是你的顶级
fen.bind("<FocusOut>", fen.quit)
答案 1 :(得分:0)
我有类似的问题,并修复了它。以下示例正常工作。 它在主窗口顶部显示一个Toplevel窗口,作为自定义配置菜单。 每当用户点击其他地方时,配置菜单就会消失。
警告:不要在Toplevel窗口上使用.transient(parent),否则,您所描述的症状会发生:点击主窗口时,菜单不会消失。您可以尝试在下面的示例中取消注释&#34; self.transient(parent)&#34;重现你的问题。
示例:
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 :(得分: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 时,它会销毁窗口。