gpiozero button_when pressed reference to function - Raspberry Pi Forums
during oop-course, tried improve turtle race 4 buttons.
according gpiozero documentation 1.4.0 page 7 code should be
button1.when_pressed = lauraforward # lauraforward = function move turtle laura
code not move turtles, not stop, displays error messages each time button pressed.
what's wrong?
error messages:
##traceback (most recent call last):
## file "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 232, in <lambda>
## callback=lambda channel: self._when_changed(),
## file "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 311, in _fire_events
## self._fire_activated()
## file "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 343, in _fire_activated
## super(holdmixin, self)._fire_activated()
## file "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 289, in _fire_activated
## self.when_activated()
## file "/home/pi/downloads/2017courseoop/turtlerace4buttons.py", line 15, in lauraforward
## laura.forward(randint(1,2))
## file "/usr/lib/python3.4/turtle.py", line 1637, in forward
## self._go(distance)
## file "/usr/lib/python3.4/turtle.py", line 1605, in _go
## self._goto(ende)
## file "/usr/lib/python3.4/turtle.py", line 3158, in _goto
## screen._pointlist(self.currentlineitem),
## file "/usr/lib/python3.4/turtle.py", line 755, in _pointlist
## cl = self.cv.coords(item)
## file "<string>", line 1, in coords
## file "/usr/lib/python3.4/tkinter/__init__.py", line 2310, in coords
## self.tk.call((self._w, 'coords') + args))]
##runtimeerror: main thread not in main loop
program code:
according gpiozero documentation 1.4.0 page 7 code should be
button1.when_pressed = lauraforward # lauraforward = function move turtle laura
code not move turtles, not stop, displays error messages each time button pressed.
what's wrong?
error messages:
##traceback (most recent call last):
## file "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 232, in <lambda>
## callback=lambda channel: self._when_changed(),
## file "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 311, in _fire_events
## self._fire_activated()
## file "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 343, in _fire_activated
## super(holdmixin, self)._fire_activated()
## file "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 289, in _fire_activated
## self.when_activated()
## file "/home/pi/downloads/2017courseoop/turtlerace4buttons.py", line 15, in lauraforward
## laura.forward(randint(1,2))
## file "/usr/lib/python3.4/turtle.py", line 1637, in forward
## self._go(distance)
## file "/usr/lib/python3.4/turtle.py", line 1605, in _go
## self._goto(ende)
## file "/usr/lib/python3.4/turtle.py", line 3158, in _goto
## screen._pointlist(self.currentlineitem),
## file "/usr/lib/python3.4/turtle.py", line 755, in _pointlist
## cl = self.cv.coords(item)
## file "<string>", line 1, in coords
## file "/usr/lib/python3.4/tkinter/__init__.py", line 2310, in coords
## self.tk.call((self._w, 'coords') + args))]
##runtimeerror: main thread not in main loop
program code:
code: select all
from turtle import turtle random import randint gpiozero import button signal import pause button1 = button(6) button2 = button(13) button3 = button(19) button4 = button(26) def lauraforward(): print("laura") laura.forward(randint(1,2)) def rikforward(): print("rik") rik.forward(randint(1,2)) def laurenforward(): print("lauren") lauren.forward(randint(1,2)) def carrieanneforward(): print("carrieanne") carrieanne.forward(randint(1,2)) laura = turtle() laura.color("red") laura.shape("turtle") laura.penup() laura.goto(-160,100) laura.pendown() rik = turtle() rik.color("green") rik.shape("turtle") rik.penup() rik.goto(-160,70) rik.pendown() lauren = turtle() lauren.color("blue") lauren.shape("turtle") lauren.penup() lauren.goto(-160,40) lauren.pendown() carrieanne = turtle() carrieanne.color("pink") carrieanne.shape("turtle") carrieanne.penup() carrieanne.goto(-160,10) carrieanne.pendown() # test function lauraforward in range(10): lauraforward() print() # part causes error message # code based on gpiozero documentation page 7 button1.when_pressed = lauraforward button2.when_pressed = rikforward button3.when_pressed = laurenforward button4.when_pressed = carrieanneforward pause()
it seems tkinter isn't @ running outside main thread. https://stackoverflow.com/questions/146 ... -main-loop horrible hack functions added instructions lists and, in version based on previous (presumably) used keyboard input in loop, instructions list.
code: select all
... def lauraforward(): global laura_list print("laura") laura_list.append(randint(1, 2)) ... laura_list = [] ... while loop ... if len(laura_list) > 0: laura.forward(laura_list.pop(0))
raspberrypi
Comments
Post a Comment