Sunday, February 17, 2008

Getting the Currently Selected Text in Tkinter

I tracked down the technique for getting the current text selection in Tkinter, as opposed to getting the current contents of the clipboard, and put them both in a nice neat function:
def _getCurrentText(widget, clipboard=False):
'''
Given a Tkinter widget, return the currently selected text (clipboard
is False) or the current contents of the system clipboard (clipboard
is True) or None if no text is currently available.
'''

try:
if clipboard:
return widget.selection_get(selection='CLIPBOARD')
else:
return widget.selection_get()
except TclError:
pass
The mechanics of that selection_get() method reach through Tk all the way to to the ICCCM standard and the X protocol!

1 comment:

See-ming Lee said...

Oh cool. Really getting into python since I plunged into Ubuntu. What would you suggest that I use for tinkling with RSS feeds using Python?