473,583 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cut & paste text between tkinter widgets

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
Aug 3 '05 #1
7 17897
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.selectio n_get [so named because of the way that X handles its
clipboard]
Aug 3 '05 #2

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

Aug 3 '05 #3
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_co mmand(label="Cu t")
the_menu.add_co mmand(label="Co py")
the_menu.add_co mmand(label="Pa ste")

def show_menu(e):
w = e.widget
the_menu.entryc onfigure("Cut",
command=lambda: w.event_generat e("<<Cut>>"))
the_menu.entryc onfigure("Copy" ,
command=lambda: w.event_generat e("<<Copy>>") )
the_menu.entryc onfigure("Paste ",
command=lambda: w.event_generat e("<<Paste>>" ))
the_menu.tk.cal l("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><ButtonReleas e-3>", show_menu)

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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFC8T/iJd01MZaTXX0RAv RhAJ9nNGclYItSJ p/PlHYJlbB+77CpDQ CdE1xh
nIk5KAPLyOzut3K P5kdGO1U=
=45zY
-----END PGP SIGNATURE-----

Aug 3 '05 #4
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.selectio n_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\Li b\lib-tk\Tkinter.py", line 574, in selection_get
return self.tk.call((' selection', 'get') + self._options(k w))
_tkinter.TclErr or: 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_cle ar()
g.clipboard_app end('I just love the wonderful clipboard management functions
of Tk!') g.selection_get (selection='CLI PBOARD') 'I just love the wonderful clipboard management functions of Tk!'

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

'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)
Aug 4 '05 #5
handy.

Thanks,

Bill

je****@unpython ic.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_co mmand(label="Cu t")
the_menu.add_co mmand(label="Co py")
the_menu.add_co mmand(label="Pa ste")

def show_menu(e):
w = e.widget
the_menu.entryc onfigure("Cut",
command=lambda: w.event_generat e("<<Cut>>"))
the_menu.entryc onfigure("Copy" ,
command=lambda: w.event_generat e("<<Copy>>") )
the_menu.entryc onfigure("Paste ",
command=lambda: w.event_generat e("<<Paste>>" ))
the_menu.tk.cal l("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><ButtonReleas e-3>", show_menu)

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

Aug 4 '05 #6
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\Li b\lib-tk\Tkinter.py", line 574, in selection_get
return self.tk.call((' selection', 'get') + self._options(k w))
_tkinter.TclErr or: 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='CLI PBOARD')


I didn't know about this, though..

--
John.

Aug 4 '05 #7
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.
Aug 5 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1563
by: Charles Starrett | last post by:
I am a just a dabbler in programming, but I'd like to make some simple cross-platform tools for generating webpages, as well as for text munging. One thing I need is a Mac OS 9/Windows GUI solution. I thought that Tkinter could provide this, but I'm having trouble getting the simple test script from Learning Python p.22 to work under Mac...
4
5282
by: Patrick L. Nolan | last post by:
Our Tkinter application has a big ScrolledText widget which is a log of everything that happens. In order to prevent people from making changes, we have it in DISABLED mode except when the program wants to write a new entry. This works OK, except that sometimes we want to copy out of piece of the contents and paste it in another window. ...
2
2322
by: Adonis | last post by:
I am creating some widgets by inheriting from Tkinter.Frame and populating the frame with whatever the widget will be then creating certain attributes/methods to be accessed later. My question is, is this a poper way to create widgets or should I take a different approach? Any help is greatly appreciated. Adonis
25
3337
by: BJörn Lindqvist | last post by:
See: http://www.wxpython.org/quotes.php. especially: "wxPython is the best and most mature cross-platform GUI toolkit, given a number of constraints. The only reason wxPython isn't the standard Python GUI toolkit is that Tkinter was there first." - Guido van Rossum Guess, that answers my question, but isn't "Tkinter was there first" a...
2
2821
by: nholtz | last post by:
Is there any way to delete a widget (window) from a Text widget, and then add it back to the Text, without re-creating the original widget. For example, I think I would like to do something like the following: ########################################################## from Tkinter import * root = Tk()
32
7460
by: Kevin Walzer | last post by:
I'm a Tcl/Tk developer who has been working, slowly, at learning Python, in part because Python has better support for certain kinds of applications that I want to develop than Tcl/Tk does. Naturally, I thought that I would use Tkinter as the GUI for these programs. However, in doing research into GUI development techniques, sample code, and...
2
5369
by: Ben Finney | last post by:
Howdy all, Python programmers looking for a built-in GUI toolkit are told two things: one, Python already comes with a GUI toolkit, and two, it looks equally ugly on all platforms. This is because the Tk widget library, that Tkinter uses, defaults to looking like Motif, which hasn't been the default widget set of *anything* for a long time....
4
5427
by: BartlebyScrivener | last post by:
Using Python on Debian Etch. What is the best way to paste a block of text in at the command prompt. I'm trying something like: Quote = raw_input("Paste quote here: ") Which works great for one line of text with a single newline. It gets
0
1792
by: Guilherme Polo | last post by:
On 10/29/08, Olrik Lenstra <o.lenstra@gmail.comwrote: It will be a combination of commands, not a single one. Initially I considered this as "probably without solution", since tcl acquired a yield command just in the 8.6a3 release, but then I looked at wx.SafeYield code and apparently it is possible to replicate it. Here is an initial...
0
7811
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8314
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7922
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8185
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6571
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3811
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.