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

python at bdurham.com python at bdurham.com
Mon Nov 22 21:34:12 CET 2010


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 at 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>


More information about the Tkinter-discuss mailing list