Path: csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsfeed.freenet.ag!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Copy To Clipboard Highlighted Text From Text Widget Date: Tue, 22 Mar 2016 12:47:11 +1100 Lines: 44 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de ULxvg+OtbRSvGnVWQlV7DgaMVnomhsc+bUNiF1CAxLVQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'except:': 0.07; 'exception.': 0.07; 'cc:addr:python-list': 0.09; '22,': 0.09; 'exception': 0.13; 'def': 0.13; 'instead.': 0.15; '2016': 0.16; 'clause.': 0.16; 'exception;': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'highlighted': 0.16; 'perfect.': 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; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'otherwise,': 0.20; 'trying': 0.22; 'header:In-Reply-To:1': 0.24; 'message- id:@mail.gmail.com': 0.27; 'looks': 0.29; 'concern': 0.29; 'sure,': 0.29; 'code': 0.30; 'tue,': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'text': 0.35; 'text.': 0.35; 'quite': 0.35; "isn't": 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'basic': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'say': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'mean': 0.38; '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; 'chrisa': 0.84; 'fortunately': 0.84; 'to:none': 0.91; 'few.': 0.91; 'subject:From': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=7tgJOexEyyqAZfw8tVsAsujah5YThNA9pKiUP7/ZJQU=; b=NA80Fcsa0RNDM2TJfpo2Db1hyK4jhzVQThcg0vgnidPKlVkvjthTtPKlc8Gc2NvoYy yyRWBIkeIZyJp+W6mHXUfazkL2I4cxVdBzPDywGF8EJmMZpzRk5F6EpAlUAvhEmuIPCb OfyEomv5A/FY0WhQiKwAg1u3+IkNXWc1NPPqFpt49qdygsOhcBBiKGAMF9LcWWSJ9PR2 ET5L7CaqjfNoH8t/gR4KugKV0BwomLbkhO8pKD9SUFxPaHPCjmyStIl20qr/UtdYxb0E Z5HW9cRPPx+rZdZXrcLqXvJiBTvArtST47+ua9/BTr3HEsw2lHBVoNRMq03hGOlTv+Ek eQnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=7tgJOexEyyqAZfw8tVsAsujah5YThNA9pKiUP7/ZJQU=; b=NDsGco9KehjIdqY9If9EtHl/F43QcCxYKHOAwUzhuJ2nWQ1Wosqa0h3UnFHK0kQRqd o3nstXz3f24y2rgQ7rEtsTBhlh71TslkkDQXSOY5t0GJWz0K08HJ+KCZS4j0VYxfDEpn RVo4wvPrY1d4v/q5V6PiHRL96JL7bqF8F/NeT5RZlLrJb9o8S99YmNssQJep75pHbc1y aZVWYZ2ieZpJvsVN6rg2nqOwoCNsFMQNEs2fnTB8Pkw99efOacRP2ZlS/U89O+5DOwBQ cniZVwrFt3WeEbsd0fziVkSShODPacRpGUnbtdQB3hKKn0oPT2SBVVVkawgElSsHk1He gN1w== X-Gm-Message-State: AD7BkJIQlhoaeroTtDeRq0B+O44Zp+/auXJ+0ZOrBI+opvKwIGEqgoOdSB8tTX51+vY6Bh91b+8f/RltEWwaCQ== X-Received: by 10.50.138.233 with SMTP id qt9mr15689344igb.13.1458611231959; Mon, 21 Mar 2016 18:47:11 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105433 On Tue, Mar 22, 2016 at 12:24 PM, Wildman via Python-list 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