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


Groups > comp.lang.python > #92871

Re: Keypress Input

From Christian Gollwitzer <auriocus@gmx.de>
Newsgroups comp.lang.python
Subject Re: Keypress Input
Date 2015-06-19 07:20 +0200
Organization A noiseless patient Spider
Message-ID <mm08nk$caf$1@dont-email.me> (permalink)
References <rBHbx.75089$MO7.40532@fx10.iad> <Tbtfx.178924$Jv.102633@fx26.iad>

Show all headers | View raw


Am 15.06.15 um 07:15 schrieb John McKenzie:
>
> from Tkinter import *
> from blinkstick import blinkstick
>
> led = blinkstick.find_first()
>
> timered = 0
> timeyellow = 0
> timeblue = 0
>
> colour = None
>
> root = Tk()
> root.title('eFlag 1')
>
>
>
> def red1(event):
>      colour = 1
>
> def yellow1(event):
>      colour = 2
>
> def blue1(event):
>      colour = 3
>
> root.bind_all('r', red1)
> root.bind_all('b', blue1)
> root.bind_all('y', yellow1)

The nonsense starts here:

===================
> root.mainloop()
>
> while colour == None:
>      led.pulse(red=0, green=255, blue=0, repeats=1, duration=5000,
> steps=50)
>
> while colour == 1:
>      led.pulse(red=255, green=0, blue=0, repeats=1, duration=3000,
> steps=50)
>      timered += 1
>
> while colour == 2:
>      led.pulse(red=255, green=255, blue=0, repeats=1, duration=3000,
> steps=50)
>      timeyellow += 1
>
> while colour == 3:
>      led.pulse(red=0, green=0, blue=255, repeats=1, duration=2000,
> steps=50)
>      timeblue += 1
====================

it seems you don't understand event based programming. root.mainloop() 
never exits. It waits for the user input and does the dispatching, i.e. 
when a key is pressed, then according to your bindings, the functions 
red1, yellow1, blue1 are called, which set a variable but do not do 
nything else. To see that, just insert a print statement into these 
functions:

  def red1(event):
       colour = 1
       print("Red ws called")


Now your job is to also do the functionality there, i.e. you have to 
reformulate your task (waiting for red, then blue...) as a state 
machine. Alternatively you can circumvent to redo the logic in a state 
machine by using a coroutine.

You should read a text about GUI programming, or more specifically event 
based programming, to understand your mistake.

	Christian


>
> def exit_handler():
>      print '\033[0;41;37mRed Team:\033[0m ', timered
>      print '\033[0;43;30mYellow Time:\033[0m ', timeyellow
>      print '\033[0;44;37mBlue Time:\033[0m ', timeblue
>      flog = open('flag1log.text', 'a')
>      flog.write(timestamp + '\n' + 'Red Team: ' + str(timered) + '\n' +
> 'Yellow Team: ' + str(timeyellow) + '\n' + 'Blue Team: ' + str(timeblue)
> + '\n')
>      flog.close()
>      atexit.register(exit_handler)
>
>

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