Keypress Input

Christian Gollwitzer auriocus at gmx.de
Fri Jun 19 01:20:03 EDT 2015


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)
>
>




More information about the Python-list mailing list