Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #105431
| From | Wildman <best_lay@yahoo.com> |
|---|---|
| Subject | Copy To Clipboard Highlighted Text From Text Widget |
| Newsgroups | comp.lang.python |
| Organization | Wildman Productions |
| Message-ID | <s6ednccPwOtSA23LnZ2dnUU7-bOdnZ2d@giganews.com> (permalink) |
| Date | 2016-03-21 20:24 -0500 |
I have a gui that has text widget and I want to be able to
copy to the clipboard the text that is highlighted or the
text widget's entire contents if no text is highlighted.
This line of code works for the highlighted text:
text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
However, this code will generate an exception if no text
is highlighted. So here is what I come up with and it
works:
def copy_clipboard(self):
try:
text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
except:
text2copy = self.text.get()
root.clipboard_clear()
root.clipboard_append(text2copy)
My concern is whether or not this approach is acceptable.
Is it ok to let the exception occur or would it be better
to avoid it? If the later, I would appreciate suggestions
on how to do that, I mean how to determine if any text is
highlighted without generating an exception. My research
was not very fruitful.
--
<Wildman> GNU/Linux user #557453
May the Source be with you.
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Copy To Clipboard Highlighted Text From Text Widget Wildman <best_lay@yahoo.com> - 2016-03-21 20:24 -0500
Re: Copy To Clipboard Highlighted Text From Text Widget Chris Angelico <rosuav@gmail.com> - 2016-03-22 12:47 +1100
Re: Copy To Clipboard Highlighted Text From Text Widget Wildman <best_lay@yahoo.com> - 2016-03-21 22:36 -0500
Re: Copy To Clipboard Highlighted Text From Text Widget Chris Angelico <rosuav@gmail.com> - 2016-03-22 14:46 +1100
Re: Copy To Clipboard Highlighted Text From Text Widget MRAB <python@mrabarnett.plus.com> - 2016-03-22 02:01 +0000
Re: Copy To Clipboard Highlighted Text From Text Widget Wildman <best_lay@yahoo.com> - 2016-03-21 22:31 -0500
csiph-web