简体   繁体   中英

Have a button table instead of a lot of variables

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

  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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM