Tkinter: cut'n'paste from DISABLED window?

Fredrik Lundh fredrik at pythonware.com
Fri Apr 16 06:49:33 EDT 2004


Michael Geary wrote:


> I don't know if this will help, but maybe it will provide a hint...
>
> In a native Windows app, the way you do this is to make the edit control
> read-only instead of disabled. For example, if you right-click a file in
> Windows and select Properties, you'll notice that you can select and copy
> text from any of the text fields in the General tab of the property sheet.
>
> The way this is done is by making those text fields Edit controls with the
> ES_READONLY style (and without the WS_DISABLED style).

Tk's Text widget is a custom widget, which doesn't rely on Windows native
widgets.

however, in recent versions, many Tk entry widgets supports both
state="disabled"
and state="readonly", but I don't think this is yet available for the Text
widget.


as for the original poster, the best way to solve this is probably to add
custom
mouse bindings for this widget.  here's a start (needs a bit of tweaking,
but you
get the idea):

    w.tag_config("mysel", background="blue", foreground="white")

    def start(event):
        index = w.index("@%d,%d" % (event.x, event.y))
        w.mark_set("myanchor", index)

    def drag(event):
        index = w.index("@%d,%d" % (event.x, event.y))
        w.tag_remove("mysel", 1.0, END)
        w.tag_add("mysel", "myanchor", index)

    def copy(event):
        text = w.get("mysel.first", "mysel.last")
        if text: add to clipboard

    w.bind("<Button-1>", start)
    w.bind("<B1-Motion>", drag)
    w.bind("<Control-C>", copy)

</F>







More information about the Python-list mailing list