我有一個Toplevel
窗口小部件,我想在用戶單擊窗口時單擊該窗口。我試圖在互聯網上找到解決方案,但似乎沒有文章討論這個話題。Tkinter Toplevel:當未聚焦時銷燬窗口
我該如何做到這一點。謝謝你的幫助 !
我有一個Toplevel
窗口小部件,我想在用戶單擊窗口時單擊該窗口。我試圖在互聯網上找到解決方案,但似乎沒有文章討論這個話題。Tkinter Toplevel:當未聚焦時銷燬窗口
我該如何做到這一點。謝謝你的幫助 !
你可以嘗試這樣的事情: 芬是你的頂層
fen.bind("<FocusOut>", fen.quit)
我也有類似的問題,並固定它。以下示例正常工作。 它在主窗口頂部顯示Toplevel窗口,作爲自定義配置菜單。 只要用戶在其他地方點擊,配置菜單就會消失。
警告:請勿在Toplevel窗口上使用.transient(父母),否則會出現您描述的症狀:單擊主窗口時,菜單不會消失。您可以在下面的示例中嘗試取消註釋「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()
''似乎工作,但不是很敏感,當我點擊主窗口內(只有當我點擊以外的工作)。任何其他方式? –
您可以添加然後 –