Popup menu won't hide on Linux
Coot
dhein at acm.org
Fri Sep 20 19:49:36 EDT 2002
dhein at acm.org (Coot) wrote in message news:<a3a96207.0209151220.5228a262 at posting.google.com>...
> When a popup menu is displayed, I expect that it should goes away when
> one of the menu items is clicked (button <1>) or when I click anywhere
> else (such that the focus is no longer on the popup menu).
>
> However, on Linux, the latter behavior doesn't happen. The popup
> remains on top of the main app window and the only way to get rid of
> it is to click one of its menu command items or close the application.
>
> This behavior isn't there on Windows.
>
> Can anyone tell me how to solve, or work around, the problem???
>
> Here is some example code you can use to demonstrate this problem for
> yourself (taken from
> http://www.pythonware.com/library/tkinter/introduction/x5819-patterns.htm):
>
> # menu-example-4.py
>
> from Tkinter import *
>
> root = Tk()
>
> def hello():
> print "hello!"
>
> # create a popup menu
> menu = Menu(root, tearoff=0)
> menu.add_command(label="Undo", command=hello)
> menu.add_command(label="Redo", command=hello)
>
> # create a canvas
> frame = Frame(root, width=512, height=512)
> frame.pack()
>
> def popup(event):
> menu.post(event.x_root, event.y_root)
>
> # attach popup to canvas
> frame.bind("<Button-3>", popup)
>
> mainloop()
OK, on Linux I have to watch for a <FocusOut> event and "unpost" when
that event fires. If I do that and click outside the window, then the
popup disappears.
Here is the new code:
from Tkinter import *
root = Tk()
def hello():
print "hello!"
def popup(event):
menu.post(event.x_root, event.y_root)
menu.focus_set()
def popupFocusOut(self,event=None):
menu.unpost()
# create a canvas
frame = Frame(root, width=512, height=512)
frame.pack()
# create a popup menu
menu = Menu(frame, tearoff=0)
menu.add_command(label="Undo", command=hello)
menu.add_command(label="Redo", command=hello)
menu.bind("<FocusOut>",popupFocusOut)
# attach popup to canvas
frame.bind("<Button-3>", popup)
mainloop()
More information about the Python-list
mailing list