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


Groups > comp.lang.python > #105435

Re: Copy To Clipboard Highlighted Text From Text Widget

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From MRAB <python@mrabarnett.plus.com>
Newsgroups comp.lang.python
Subject Re: Copy To Clipboard Highlighted Text From Text Widget
Date Tue, 22 Mar 2016 02:01:53 +0000
Lines 46
Message-ID <mailman.480.1458612128.12893.python-list@python.org> (permalink)
References <s6ednccPwOtSA23LnZ2dnUU7-bOdnZ2d@giganews.com> <CAPTjJmqOPoTjzJRBDor6rtS=t6L7=ES2tPMOYqbJbMQ=JQA6-A@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de FVoajw/c58AVFRL3aHJIRg/X5b4LJJ6uuDyFvkXtrItA==
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'except:': 0.07; 'exception.': 0.07; '22,': 0.09; 'exception': 0.13; 'def': 0.13; 'instead.': 0.15; '2016': 0.16; 'clause.': 0.16; 'exception;': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'highlighted': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'perfect.': 0.16; 'received:192.168.1.4': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Text': 0.16; 'try/except': 0.16; 'wrote:': 0.16; 'any,': 0.18; 'try:': 0.18; 'widget': 0.18; 'gui': 0.18; 'library': 0.20; 'otherwise,': 0.20; 'trying': 0.22; 'header :In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'looks': 0.29; 'concern': 0.29; 'sure,': 0.29; 'raise': 0.29; 'code': 0.30; 'tue,': 0.34; 'that,': 0.34; 'text': 0.35; 'text.': 0.35; 'quite': 0.35; "isn't": 0.35; 'but': 0.36; 'there': 0.36; 'basic': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'say': 0.37; 'mean': 0.38; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'determine': 0.61; "you'll": 0.61; 'avoid': 0.61; 'entire': 0.61; 'back': 0.62; 'research': 0.62; 'mar': 0.65; 'python-list': 0.66; 'here': 0.66; 'fortunately': 0.84; 'few.': 0.91; 'subject:From': 0.97
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=K//fZHiI c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IkcTkHD0fZMA:10 a=8AHkEIZyAAAA:8 a=v90uTLD8y3-FSl_38jwA:9 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett@:2500
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0
In-Reply-To <CAPTjJmqOPoTjzJRBDor6rtS=t6L7=ES2tPMOYqbJbMQ=JQA6-A@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:105435

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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