2016-08-02 11 views
0

焦点を当てていないときに私は、私は、ユーザーがウィンドウの外にクリックするたびに破壊したいToplevelウィジェットを持っているウィンドウを破壊します。私はインターネット上で解決策を見つけようとしましたが、このトピックについて議論する記事はありません。Tkinterのトップレベル:

これをどのように達成できますか?助けてくれてありがとう!

答えて

1

あなたはそのような何かを試すことができます: フェンは、あなたのトップレベル

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

'' 動作しているようですが、私は、メインウィンドウの内側をクリックしたときに非常に敏感ではない(私は外をクリックした場合にのみ動作します)。他の方法? –

+0

その後、を追加することができます –

0

である私は、同様の問題を抱えていたし、それを修正しました。以下の例は正常に動作します。 これは、カスタマイズされた設定メニューとして、メインウィンドウの上部のトップレベルのウィンドウを表示します。ユーザーがどこかをクリックするたびに、設定メニューが消え 。

警告:メインウィンドウをクリックすると、メニューが消えません。そうでない場合、あなたは説明した現象が発生し、トップレベルウィンドウ上(親)を.transient使用しないでください。あなたはあなたの問題を再現するために、「self.transient(親)」行のコメントを解除するには、以下の例では試すことができます。

例:

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() 
関連する問題