Tkinter: cut'n'paste from DISABLED window?

Russell E. Owen no at spam.invalid
Fri Apr 16 19:56:00 EDT 2004


In article <mailman.679.1082112559.20120.python-list at python.org>,
 "Fredrik Lundh" <fredrik at pythonware.com> wrote:

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

Glad to hear about the new state="readonly". Looking forward to that 
making it into the Text widget. Meanwhile, here's the function I use. No 
gurantees, but it seems to work for me. Any bug reports would be most 
welcome.

-- Russell

def makeReadOnly(tkWdg):
    """Makes a Tk widget (typically an Entry or Text) read-only,
    in the sense that the user cannot modify the text (but it can
    still be set programmatically). The user can still select and copy 
text
    and key bindings for <<Copy>> and <<Select-All>> still work properly.
    
    Inputs:
    - tkWdg: a Tk widget
    """
    def killEvent(evt):
        return "break"
    
    def doCopy(evt):
        tkWdg.event_generate("<<Copy>>")

    def doSelectAll(evt):
        tkWdg.event_generate("<<Select-All>>")

    # kill all events that can change the text,
    # including all typing (even shortcuts for
    # copy and select all)
    tkWdg.bind("<<Cut>>", killEvent)
    tkWdg.bind("<<Paste>>", killEvent)
    tkWdg.bind("<<Paste-Selection>>", killEvent)
    tkWdg.bind("<<Clear>>", killEvent)
    tkWdg.bind("<Key>", killEvent)
    # restore copy and select all
    for evt in tkWdg.event_info("<<Copy>>"):
        tkWdg.bind(evt, doCopy)
    for evt in tkWdg.event_info("<<Select-All>>"):
        tkWdg.bind(evt, doSelectAll)



More information about the Python-list mailing list