Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; '"""': 0.05; '__name__': 0.07; 'msg': 0.07; 'event):': 0.09; 'executes': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'jan': 0.11; '"this': 0.13; 'def': 0.13; '"""\\': 0.16; "'__main__':": 0.16; 'anyhow,': 0.16; 'condense': 0.16; 'rationale': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'seeing.': 0.16; 'wrote:': 0.16; 'app': 0.16; 'example.': 0.18; 'try:': 0.18; 'gui': 0.18; 'input': 0.18; 'preferred': 0.20; '2.x': 0.22; 'keyboard': 0.22; 'keyerror:': 0.22; 'tkinter': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X -Complaints-To:1': 0.26; 'defining': 0.27; "skip:' 10": 0.28; 'device': 0.28; 'fine': 0.28; 'code:': 0.29; 'print': 0.30; 'code': 0.30; 'window': 0.30; 'skip:s 30': 0.31; 'skip:_ 10': 0.32; 'statement': 0.32; 'run': 0.33; 'class': 0.33; 'except': 0.34; 'false': 0.35; 'possible,': 0.35; 'but': 0.36; 'there': 0.36; 'lines': 0.36; 'skip:{ 10': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'being': 0.37; 'received:org': 0.37; 'johnson': 0.37; 'easily': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'press': 0.61; 'future,': 0.70; 'learn.': 0.84; 'mastering': 0.84; 'received:fios.verizon.net': 0.91; 'rick': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Keypress Input Date: Thu, 16 Jul 2015 02:08:53 -0400 References: <3fec5a1e-44e6-4e3c-ab01-2703f2911c50@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 In-Reply-To: <3fec5a1e-44e6-4e3c-ab01-2703f2911c50@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 80 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437026959 news.xs4all.nl 2952 [2001:888:2000:d::a6]:44717 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.stben.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:93899 On 7/15/2015 9:03 PM, Rick Johnson wrote: > You may have solved your input capturing problem, and i > don't think a GUI is the preferred solution for a > graphically deficient device anyhow, but you may well need a > GUI in the future, and this would be a fine example from which > to learn. This really is a nice example. Your rationale for defining an app class is the best I remember seeing. To run in 3.x, change the first two lines to import tkinter as tk from tkinter.messagebox import showinfo, showerror > import Tkinter as tk > from tkMessageBox import showinfo, showerror > > MSG1 = """\ > To begin retinal stimulation, press r or g or b on your keyboard > Hold key down for extended stimulation! > """ > > class App(tk.Tk): > def __init__(self): > tk.Tk.__init__(self) > self.bind("", self.evtKeyDown) > self.bind("", self.evtKeyUp) > self.protocol("WM_DELETE_WINDOW", self.evtDeleteWindow) > w = tk.Label(self, text=MSG1) > w.pack() > self.config(bg='white') > self.geometry('500x500') > self.focus_set() > > def evtDeleteWindow(self): > showinfo("The window is Dying", "Goodbye cruel world!", parent=self) > self.destroy() > > def evtKeyDown(self, event): > key = event.keysym.lower() > alert = False > if key == 'r': > self.config(bg='red') > elif key == 'g': > self.config(bg='green') > elif key == 'b': > self.config(bg='blue') > else: Can condense block above to this easily extended code: (Replacing if if/elif/elif/... chains, when possible, is part of mastering Python.) try: self['bg'] = {'r':'red', 'g':'green', 'b':'blue'}[key] except KeyError: > msg = 'I *TOLD* you to press r or g or b, not {0!r}!'.format(key) > showerror('', msg, parent=self) > > def evtKeyUp(self, event): > self.config(bg='white') > > if __name__ == '__main__': > app = App() > app.title('Retina Stimultor') > app.mainloop() > print "This code only executes *AFTER* the mainloop call returns!" Adding parens to print, when there is a single object being printed, has no effect in 2.x and makes the statement work in 3.x. The following works the same in both. print("Mainloop has returned") -- Terry Jan Reedy