Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #93899
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Keypress Input |
| Date | 2015-07-16 02:08 -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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.559.1437026959.3674.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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