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


Groups > comp.lang.python > #93899

Re: Keypress Input

Return-Path <python-python-list@m.gmane.org>
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 <tjreedy@udel.edu>
Subject Re: Keypress Input
Date Thu, 16 Jul 2015 02:08:53 -0400
References <rBHbx.75089$MO7.40532@fx10.iad> <Tbtfx.178924$Jv.102633@fx26.iad> <mm08nk$caf$1@dont-email.me> <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 <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>
Newsgroups comp.lang.python
Message-ID <mailman.559.1437026959.3674.python-list@python.org> (permalink)
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

Show key headers only | View raw


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("<KeyPress>", self.evtKeyDown)
>          self.bind("<KeyRelease>", 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

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


Thread

Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-03 18:22 +0000
  Re: Keypress Input Laura Creighton <lac@openend.se> - 2015-06-03 20:59 +0200
  Re: Keypress Input Gary Herron <gherron@digipen.edu> - 2015-06-03 12:15 -0700
  Re: Keypress Input Gary Herron <gherron@digipen.edu> - 2015-06-03 11:47 -0700
  Re: Keypress Input Laura Creighton <lac@openend.se> - 2015-06-04 12:50 +0200
  Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-06 18:28 +0000
    Re: Keypress Input Laura Creighton <lac@openend.se> - 2015-06-06 22:52 +0200
    Re: Keypress Input Chris Angelico <rosuav@gmail.com> - 2015-06-07 07:20 +1000
    Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-06-06 22:31 -0600
  Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-15 05:15 +0000
    Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-16 15:09 +0000
    Re: Keypress Input Christian Gollwitzer <auriocus@gmx.de> - 2015-06-19 07:20 +0200
      Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-20 14:59 +0000
      Re: Keypress Input Rick Johnson <rantingrickjohnson@gmail.com> - 2015-07-15 18:03 -0700
        Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-07-15 22:30 -0600
          Re: Keypress Input Rick Johnson <rantingrickjohnson@gmail.com> - 2015-07-16 10:22 -0700
            Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-07-16 15:27 -0600
        Re: Keypress Input Terry Reedy <tjreedy@udel.edu> - 2015-07-16 02:08 -0400
          Re: Keypress Input Rick Johnson <rantingrickjohnson@gmail.com> - 2015-07-16 11:30 -0700
        Re: Keypress Input Terry Reedy <tjreedy@udel.edu> - 2015-07-16 03:10 -0400
        Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-07-16 15:29 -0600
  Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-15 05:23 +0000
    Re: Keypress Input Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-06-15 12:22 +0100
      Re: Keypress Input Grant Edwards <invalid@invalid.invalid> - 2015-06-15 15:22 +0000
    Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-06-16 11:15 -0600
      Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-16 20:06 +0000
        Re: Keypress Input Grant Edwards <invalid@invalid.invalid> - 2015-06-16 20:49 +0000
          Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-06-16 19:22 -0600
          Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-06-18 16:42 -0600
      Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-06-20 15:02 +0000
        Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-06-20 10:30 -0600
  Re: Keypress Input Paul Rubin <no.email@nospam.invalid> - 2015-06-16 14:22 -0700
  Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-07-15 19:05 +0000
    Re: Keypress Input Michael Torrie <torriem@gmail.com> - 2015-07-15 13:17 -0600
      Re: Keypress Input John McKenzie <davros@bellaliant.net> - 2015-08-16 19:45 +0000

csiph-web