0

Novice in pyhton AND Raspberry prog, i want to create a Quizz-buzz Game. Up to 8 players can play it.

To catch the event when a button is pressed, (using the gpiozero lib) i want to do something like :

buttons = [Button(4), Button(5), Button(6), ....]
buttons.when_pressed = lambda : buttonpressed(buttons.buttonPressedIndex)

Rather than

button1 = Button(1)
button2 = Button(2)
button3 = Button(3)
....

button1.when_pressed = lambda : buttonpressed(1)
button2.when_pressed = lambda : buttonpressed(2)
....

Is it possible to do something like this ? How can i know which index of my array are trigger ?

Have a nice day

Amaury Laroze
  • 197
  • 2
  • 12

1 Answers1

1
  1. You can move the when_pressed initialization to Button's constructor.
  2. You can declare a list of buttons: buttons = [Button(i) for i in range(N)]
    and then set their when_pressed function like that: for button in buttons: button.when_pressed = lambda: buttonpressed(button.buttonPressedIndex)
Ben Lugasi
  • 146
  • 6