2016-08-02 155 views

回答

1

你可以嘗試這樣的事情: 芬是你的頂層

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

''似乎工作,但不是很敏感,當我點擊主窗口內(只有當我點擊以外的工作)。任何其他方式? –

+0

您可以添加然後 –

0

我也有類似的問題,並固定它。以下示例正常工作。 它在主窗口頂部顯示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()