[Tkinter-discuss] Working with tear-off menus (change the "-----" label, customize location/appearance of tear-off window)

Michael Lange klappnase at web.de
Mon Nov 22 11:45:41 CET 2010


Hi,

Thus spoketh python at 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


More information about the Tkinter-discuss mailing list