Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #105431 > unrolled thread

Copy To Clipboard Highlighted Text From Text Widget

Started byWildman <best_lay@yahoo.com>
First post2016-03-21 20:24 -0500
Last post2016-03-21 22:31 -0500
Articles 6 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#105431 — Copy To Clipboard Highlighted Text From Text Widget

FromWildman <best_lay@yahoo.com>
Date2016-03-21 20:24 -0500
SubjectCopy To Clipboard Highlighted Text From Text Widget
Message-ID<s6ednccPwOtSA23LnZ2dnUU7-bOdnZ2d@giganews.com>
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.

[toc] | [next] | [standalone]


#105433

FromChris Angelico <rosuav@gmail.com>
Date2016-03-22 12:47 +1100
Message-ID<mailman.478.1458611235.12893.python-list@python.org>
In reply to#105431
On Tue, Mar 22, 2016 at 12:24 PM, Wildman via Python-list
<python-list@python.org> wrote:
> 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.

Fortunately your code reveals that you're using "tk" here, but
otherwise, please state up-front which GUI library you're using; there
are quite a few.

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

You're trying to do one of two things:

1) Copy the selected text to the clipboard
2) If there isn't any, copy all the text.

So I would say yes, the basic layout of try/except to get the text is
perfect. However, DON'T use a bare "except:" clause. You'll get back a
specific exception; catch that instead. Other than that, sure, your
code looks fine.

ChrisA

[toc] | [prev] | [next] | [standalone]


#105439

FromWildman <best_lay@yahoo.com>
Date2016-03-21 22:36 -0500
Message-ID<s_qdnf8l9J8rIG3LnZ2dnUU7-ecAAAAA@giganews.com>
In reply to#105433
On Tue, 22 Mar 2016 12:47:11 +1100, Chris Angelico wrote:

> On Tue, Mar 22, 2016 at 12:24 PM, Wildman via Python-list
> <python-list@python.org> wrote:
>> 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.
> 
> Fortunately your code reveals that you're using "tk" here, but
> otherwise, please state up-front which GUI library you're using; there
> are quite a few.

Noted.

>> 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.
> 
> You're trying to do one of two things:
> 
> 1) Copy the selected text to the clipboard
> 2) If there isn't any, copy all the text.

Yes, that is exactly it.

> So I would say yes, the basic layout of try/except to get the text is
> perfect. However, DON'T use a bare "except:" clause. You'll get back a
> specific exception; catch that instead. Other than that, sure, your
> code looks fine.
> 
> ChrisA

Thanks.  I changed the code as you (and MRAB) suggested.

def copy_clipboard(self):
    try:
        text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
    except tk.TclError:
        text2copy = self.text.get()
    root.clipboard_clear()
    root.clipboard_append(text2copy)

Works perfectly.  Thanks again.

-- 
<Wildman> GNU/Linux user #557453
"Our country's founders cherished liberty, not democracy."
  -Ron Paul

[toc] | [prev] | [next] | [standalone]


#105440

FromChris Angelico <rosuav@gmail.com>
Date2016-03-22 14:46 +1100
Message-ID<mailman.481.1458618385.12893.python-list@python.org>
In reply to#105439
On Tue, Mar 22, 2016 at 2:36 PM, Wildman via Python-list
<python-list@python.org> wrote:
> Thanks.  I changed the code as you (and MRAB) suggested.
>
> def copy_clipboard(self):
>     try:
>         text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
>     except tk.TclError:
>         text2copy = self.text.get()
>     root.clipboard_clear()
>     root.clipboard_append(text2copy)
>
> Works perfectly.  Thanks again.

Looks good!

For future reference, and for others reading, this was a great
question. You posted actual code, with a specific question, and it was
easy to figure out what's going on. Thank you.

ChrisA

[toc] | [prev] | [next] | [standalone]


#105435

FromMRAB <python@mrabarnett.plus.com>
Date2016-03-22 02:01 +0000
Message-ID<mailman.480.1458612128.12893.python-list@python.org>
In reply to#105431
On 2016-03-22 01:47, Chris Angelico wrote:
> On Tue, Mar 22, 2016 at 12:24 PM, Wildman via Python-list
> <python-list@python.org> wrote:
>> 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.
>
> Fortunately your code reveals that you're using "tk" here, but
> otherwise, please state up-front which GUI library you're using; there
> are quite a few.
>
>> 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.
>
> You're trying to do one of two things:
>
> 1) Copy the selected text to the clipboard
> 2) If there isn't any, copy all the text.
>
> So I would say yes, the basic layout of try/except to get the text is
> perfect. However, DON'T use a bare "except:" clause. You'll get back a
> specific exception; catch that instead. Other than that, sure, your
> code looks fine.
>
It'll raise TclError.

[toc] | [prev] | [next] | [standalone]


#105438

FromWildman <best_lay@yahoo.com>
Date2016-03-21 22:31 -0500
Message-ID<s_qdnfwl9J_pIW3LnZ2dnUU7-eednZ2d@giganews.com>
In reply to#105435
On Tue, 22 Mar 2016 02:01:53 +0000, MRAB wrote:

> On 2016-03-22 01:47, Chris Angelico wrote:
>> On Tue, Mar 22, 2016 at 12:24 PM, Wildman via Python-list
>> <python-list@python.org> wrote:
>>> 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.
>>
>> Fortunately your code reveals that you're using "tk" here, but
>> otherwise, please state up-front which GUI library you're using; there
>> are quite a few.
>>
>>> 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.
>>
>> You're trying to do one of two things:
>>
>> 1) Copy the selected text to the clipboard
>> 2) If there isn't any, copy all the text.
>>
>> So I would say yes, the basic layout of try/except to get the text is
>> perfect. However, DON'T use a bare "except:" clause. You'll get back a
>> specific exception; catch that instead. Other than that, sure, your
>> code looks fine.
>>
> It'll raise TclError.

Thanks, that saved me some research time.

-- 
<Wildman> GNU/Linux user #557453
"It is a dangerous notion that we need a
government to protect us from ourselves."
  -Ron Paul

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web