Working with tear-off menus (change the "-----" label, customize location/appearance of tear-off window)
I've been experimenting with Tkinter tear-off menus and have the following questions (relative to Windows): 1. Is there a way to change the default appearance of the "------" menu label that is used to indicate a tear-off menu -OR- is there a way to programmatically convert a menu widget to a torn-off state? Note: The tear-off menu item does not have a 'label' property. 2. Is there a way to customize the location and appearance of the window that contains a tear-off menu? On Windows, the tear-off menus appear at random(?) places on the desktop. And these windows have min/max buttons and are resizable - all behaviors that IMO don't make sense for a floating tear-off menu. What I would like to do is capture a reference to the window of a tear-off and customize it like I would any other Toplevel() window. Is this possible? Note: I can detect the tearoff event by using the tearoffcommand= option when I create a menu. I think this event is where I want to take control of the tear-off menu's window properties? But how to get a reference to the tear-off menu's window (and does it exist yet?). Thank you, Malcolm
Hi, Thus spoketh python@bdurham.com unto us on Sun, 21 Nov 2010 20:18:06 -0500:
I've been experimenting with Tkinter tear-off menus and have the following questions (relative to Windows):
1. Is there a way to change the default appearance of the "------" menu label that is used to indicate a tear-off menu -OR- is there a way to programmatically convert a menu widget to a torn-off state? Note: The tear-off menu item does not have a 'label' property.
Not sure, maybe you could find out the tearoff's x and y coords and use event_generate() to trigger the mouse event or so...
2. Is there a way to customize the location and appearance of the window that contains a tear-off menu? On Windows, the tear-off menus appear at random(?) places on the desktop. And these windows have min/max buttons and are resizable - all behaviors that IMO don't make sense for a floating tear-off menu. What I would like to do is capture a reference to the window of a tear-off and customize it like I would any other Toplevel() window. Is this possible?
Here on linux (I guess it should be the same on windows) the tearoffcommand takes two arguments, first the menu itself and second the newly created toplevel window, both as strings however, and Tkinter doesn't know about the toplevel, so you will have to use the tcl-syntax again :( See this example: ######################### from Tkinter import * root = Tk() def foo(menu, top): root.nametowidget(menu).add_command(label='baz') root.tk.call('wm', 'resizable', top, '0', '0') m = Menu(root, tearoffcommand=foo) m.add_command(label='foo') m.add_command(label='bar') m.add_command(label='blah') root.bind('<3>', lambda event: m.tk_popup(event.x_root, event.y_root)) root.mainloop() ####################### Regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. You are an excellent tactician, Captain. You let your second in command attack while you sit and watch for weakness. -- Khan Noonian Singh, "Space Seed", stardate 3141.9
Hi Michael, Thank you for your help. Your solution works well (tested under Windows 7 with 32-bit Python 2.7). Regards, Malcolm <snipped>
2. Is there a way to customize the location and appearance of the window that contains a tear-off menu? On Windows, the tear-off menus appear at random(?) places on the desktop. And these windows have min/max buttons and are resizable - all behaviors that IMO don't make sense for a floating tear-off menu. What I would like to do is capture a reference to the window of a tear-off and customize it like I would any other Toplevel() window. Is this possible?
Here on linux (I guess it should be the same on windows) the tearoffcommand takes two arguments, first the menu itself and second the newly created toplevel window, both as strings however, and Tkinter doesn't know about the toplevel, so you will have to use the tcl-syntax again :( See this example: from Tkinter import * root = Tk() def foo(menu, top): root.nametowidget(menu).add_command(label='baz') root.tk.call('wm', 'resizable', top, '0', '0') m = Menu(root, tearoffcommand=foo) m.add_command(label='foo') m.add_command(label='bar') m.add_command(label='blah') root.bind('<3>', lambda event: m.tk_popup(event.x_root, event.y_root)) root.mainloop() </snipped>
You can manually create a tearoff from a menu: % .m clone .mt tearoff and post it at a desired location: % .mt post 100 100 As suggested in the other message, you can use "wm resizeable" to make it not change size: % wm resiz .mt 0 0 (Of course, I only tested this on linux, so on Windows YMMV) Unfortunately, the menu 'clone' subcommand does not seem to be wrapped in Python. You can add it, but it'll be gross and touches stuff that should be considered implementation details of Tkinter (works with all python2.x I'm aware of, though): # ---------------------------------------------------------------------- import Tkinter, types def makewidget(master, klass, path): path = str(path) self = types.InstanceType(klass) self._name = path[len(master._w)+1:] self._w = path self.children = {} master.children[self._name] = self self.master = master self.tk = master.tk return self def tearoff(menu): child = str(menu) + ".tearoff" menu.tk.call((menu, "clone", child, "tearoff")) return makewidget(menu, Tkinter.Menu, child) # ---------------------------------------------------------------------- And then you'll find that 'wm_resizable' is not available because Menu is not a subclass of Wm. Just call it directly through Tk: >>> mt = tearoff(m) >>> mt.tk.call("wm", "resizable", mt, 0, 0) >>> mt.post(100, 100) Jeff
Thus spoketh Jeff Epler <jepler@unpythonic.net> unto us on Mon, 22 Nov 2010 07:26:41 -0600:
You can manually create a tearoff from a menu: % .m clone .mt tearoff and post it at a desired location: % .mt post 100 100 As suggested in the other message, you can use "wm resizeable" to make it not change size: % wm resiz .mt 0 0 (Of course, I only tested this on linux, so on Windows YMMV)
Unfortunately, the menu 'clone' subcommand does not seem to be wrapped in Python. You can add it, but it'll be gross and touches stuff that should be considered implementation details of Tkinter (works with all python2.x I'm aware of, though):
This is interesting, I never heard about that. And, from man menu I see that menubars are also cloned, which might explain the troubles with the menubars and cascading menus the OP had in the previous thread, I can confirm the same problem with <<MenuSelect>> with the torn off popup menu, while it works all right with the "normal" one. Regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. We Klingons believe as you do -- the sick should die. Only the strong should live. -- Kras, "Friday's Child", stardate 3497.2
Hi Jeff, Thank you! Confirming that your code works under Windows 7 with 32-bit Python 2.7. Regards, Malcolm <snipped> From: "Jeff Epler" <jepler@unpythonic.net> You can manually create a tearoff from a menu: % .m clone .mt tearoff and post it at a desired location: % .mt post 100 100 As suggested in the other message, you can use "wm resizeable" to make it not change size: % wm resiz .mt 0 0 (Of course, I only tested this on linux, so on Windows YMMV) Unfortunately, the menu 'clone' subcommand does not seem to be wrapped in Python. You can add it, but it'll be gross and touches stuff that should be considered implementation details of Tkinter (works with all python2.x I'm aware of, though): # ---------------------------------------------------------------------- import Tkinter, types def makewidget(master, klass, path): path = str(path) self = types.InstanceType(klass) self._name = path[len(master._w)+1:] self._w = path self.children = {} master.children[self._name] = self self.master = master self.tk = master.tk return self def tearoff(menu): child = str(menu) + ".tearoff" menu.tk.call((menu, "clone", child, "tearoff")) return makewidget(menu, Tkinter.Menu, child) # ---------------------------------------------------------------------- And then you'll find that 'wm_resizable' is not available because Menu is not a subclass of Wm. Just call it directly through Tk: >>> mt = tearoff(m) >>> mt.tk.call("wm", "resizable", mt, 0, 0) >>> mt.post(100, 100) </snipped>
participants (3)
-
Jeff Epler -
Michael Lange -
python@bdurham.com