FAQ
Is there a simple way to cut and paste from a tkinter text widget to an
entry widget? I know I could create a mouse button event that triggers
a popup (message widget) prompting for cut/paste in each of the widgets
using a temp variable to hold the text, but I don't wnat to reinvent the
wheel if there already is something that does the job.

Thanks,

Bill

Search Discussions

  • Christopher Subich at Aug 3, 2005 at 3:37 pm

    William Gill wrote:
    Is there a simple way to cut and paste from a tkinter text widget to an
    entry widget? I know I could create a mouse button event that triggers
    a popup (message widget) prompting for cut/paste in each of the widgets
    using a temp variable to hold the text, but I don't wnat to reinvent the
    wheel if there already is something that does the job.
    1) TKinter text and entry widgets should already have proper event
    bindings for cut/copy/paste. Test first with your system-default
    keyboard shortcuts (^C, ^X, ^V on Windows). I haven't tried it myself,
    but I think those events bind to '<<Cut>>', '<<Copy>>', and '<<Paste>>',
    so generating them should Do The Right Thing with selected text.

    2) If you need to do any processing on the clipboard data, look at
    widget.selection_get [so named because of the way that X handles its
    clipboard]
  • William Gill at Aug 3, 2005 at 9:31 pm

    Is there a simple way to cut and paste from a tkinter text widget to
    an entry widget? I know I could create a mouse button event that
    triggers a popup (message widget) prompting for cut/paste in each of
    the widgets using a temp variable to hold the text, but I don't wnat
    to reinvent the wheel if there already is something that does the job.

    1) TKinter text and entry widgets should already have proper event
    bindings for cut/copy/paste. Test first with your system-default
    keyboard shortcuts (^C, ^X, ^V on Windows). I haven't tried it myself,
    but I think those events bind to '<<Cut>>', '<<Copy>>', and '<<Paste>>',
    so generating them should Do The Right Thing with selected text.
    ^C, ^X, and ^V work just fine! (I swear I tried that before I posted
    and they didn't???)

    2) If you need to do any processing on the clipboard data, look at
    widget.selection_get [so named because of the way that X handles its
    clipboard]
    From my reading, w.selection_get will return the selected text in w,
    and places it on the clipboard. I didn't see any way to get data from
    the clipboard.

    Thanks,

    Bill
  • Christopher Subich at Aug 4, 2005 at 1:15 am
    Apologies in advance to anyone who has this post mangled, I use a couple
    Unicode characters at the end and Thunderbird wants to use UTF8 for the
    message encoding. Unless it does something weird, this post should
    still be legible... but I'm not going to rely on that. :)

    William Gill wrote:
    2) If you need to do any processing on the clipboard data, look at
    widget.selection_get [so named because of the way that X handles its
    clipboard]
    From my reading, w.selection_get will return the selected text in w,
    and places it on the clipboard. I didn't see any way to get data from
    the clipboard.
    Not exactly. The docs for selection_get say:

    """Return the contents of the current X selection.

    A keyword parameter selection specifies the name of
    the selection and defaults to PRIMARY. A keyword
    parameter displayof specifies a widget on the display
    to use."""

    The "X selection" is another way of saying "clipboard," again because of
    the way that X manages the clipboard.

    In experimenting with this, I found a slight... fun issue involved in
    this. Selection_get is the correct method to call, but it doesn't quite
    work out of the box.
    g.selection_get()
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "C:\Python24\Lib\lib-tk\Tkinter.py", line 574, in selection_get
    return self.tk.call(('selection', 'get') + self._options(kw))
    _tkinter.TclError: PRIMARY selection doesn't exist or form "STRING" not
    defined

    This poses a small problem. I'm not sure whether this is a
    Win32-related issue, or it's because the PRIMARY selection isn't fully
    configured. Regardless, even if it worked it still wouldn't be what we
    wanted; the selection we want is CLIPBOARD. See this code:
    g.clipboard_clear()
    g.clipboard_append('I just love the wonderful clipboard management
    functions
    of Tk!')
    g.selection_get(selection='CLIPBOARD')
    'I just love the wonderful clipboard management functions of Tk!'

    And then, copying some text from this compose window...
    g.selection_get(selection='CLIPBOARD')
    'And then, copying some text from this compose window...'

    In theory, the clipboard will support more than text. This support is
    nontrivial, which is code for saying that I have no idea how to get it
    to work.

    Also, despite ICCCM standards, it looks like this clipboard management
    on win32 works as-is with unicode data -- under Tkinter, it returns a
    unicode string. (? ? ? ? -- and good luck with -that- going through
    unmangled)
  • Repton at Aug 4, 2005 at 11:36 pm

    Christopher Subich wrote:
    In experimenting with this, I found a slight... fun issue involved in
    this. Selection_get is the correct method to call, but it doesn't quite
    work out of the box.
    g.selection_get()
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "C:\Python24\Lib\lib-tk\Tkinter.py", line 574, in selection_get
    return self.tk.call(('selection', 'get') + self._options(kw))
    _tkinter.TclError: PRIMARY selection doesn't exist or form "STRING" not
    defined

    This poses a small problem. I'm not sure whether this is a
    Win32-related issue, or it's because the PRIMARY selection isn't fully
    configured.
    You need to select something first :-)
    from Tkinter import *
    tk = Tk(); e = Entry(tk); e.pack()
    # Type 'foo' into the Entry, then highlight it
    ...
    e.selection_get()
    'foo'

    e.selection_get() will raise TclError if the widget you call it on has
    nothing selected.
    g.selection_get(selection='CLIPBOARD')
    I didn't know about this, though..

    --
    John.
  • Christopher Subich at Aug 5, 2005 at 12:36 am

    Repton wrote:
    This poses a small problem. I'm not sure whether this is a
    Win32-related issue, or it's because the PRIMARY selection isn't fully
    configured.

    You need to select something first :-)
    That doesn't work for inter-process communication, though, at least not
    with win32 native programs.
  • Jepler at Aug 3, 2005 at 10:06 pm
    Here's some code that gives a cut-copy-paste pop-up window on all Entry widgets
    in an application.

    This code is released into the public domain.

    Jeff Epler
    #------------------------------------------------------------------------
    import Tkinter

    def make_menu(w):
    global the_menu
    the_menu = Tkinter.Menu(w, tearoff=0)
    the_menu.add_command(label="Cut")
    the_menu.add_command(label="Copy")
    the_menu.add_command(label="Paste")

    def show_menu(e):
    w = e.widget
    the_menu.entryconfigure("Cut",
    command=lambda: w.event_generate("<<Cut>>"))
    the_menu.entryconfigure("Copy",
    command=lambda: w.event_generate("<<Copy>>"))
    the_menu.entryconfigure("Paste",
    command=lambda: w.event_generate("<<Paste>>"))
    the_menu.tk.call("tk_popup", the_menu, e.x_root, e.y_root)

    t = Tkinter.Tk()
    make_menu(t)

    e1 = Tkinter.Entry(); e1.pack()
    e2 = Tkinter.Entry(); e2.pack()
    e1.bind_class("Entry", "<Button-3><ButtonRelease-3>", show_menu)

    t.mainloop()
    #------------------------------------------------------------------------
    -------------- next part --------------
    A non-text attachment was scrubbed...
    Name: not available
    Type: application/pgp-signature
    Size: 189 bytes
    Desc: not available
    Url : http://mail.python.org/pipermail/python-list/attachments/20050803/2fab42da/attachment.pgp
  • William Gill at Aug 4, 2005 at 4:04 pm
    handy.

    Thanks,

    Bill

    jepler at unpythonic.net wrote:
    Here's some code that gives a cut-copy-paste pop-up window on all Entry widgets
    in an application.

    This code is released into the public domain.

    Jeff Epler
    #------------------------------------------------------------------------
    import Tkinter

    def make_menu(w):
    global the_menu
    the_menu = Tkinter.Menu(w, tearoff=0)
    the_menu.add_command(label="Cut")
    the_menu.add_command(label="Copy")
    the_menu.add_command(label="Paste")

    def show_menu(e):
    w = e.widget
    the_menu.entryconfigure("Cut",
    command=lambda: w.event_generate("<<Cut>>"))
    the_menu.entryconfigure("Copy",
    command=lambda: w.event_generate("<<Copy>>"))
    the_menu.entryconfigure("Paste",
    command=lambda: w.event_generate("<<Paste>>"))
    the_menu.tk.call("tk_popup", the_menu, e.x_root, e.y_root)

    t = Tkinter.Tk()
    make_menu(t)

    e1 = Tkinter.Entry(); e1.pack()
    e2 = Tkinter.Entry(); e2.pack()
    e1.bind_class("Entry", "<Button-3><ButtonRelease-3>", show_menu)

    t.mainloop()
    #------------------------------------------------------------------------

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 3, '05 at 3:17p
activeAug 5, '05 at 12:36a
posts8
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase