带有物理按钮的 PysimpleGUI 计数器

PysimpleGUI counter with physical button

我想用 gpiozero 制作一个带有物理按钮的计数器。我按照文档中的步骤操作,但显然不起作用。这是我在 documentation.

中遵循的步骤
import PySimpleGUI as sg
from threading import Thread
from gpiozero import Button
from time import sleep

button = Button(2)
i = 0
sg.theme('Dark blue 3')   # Add a touch of color
# All the stuff inside your window.
layout = [  [sg.Text('Bonjour et bienvenue en STS',font=("Helvetica", 25), text_color='white')],
            [sg.Text('Votre score est de :'+ str(i), key='count')],
            [sg.Button('Cancel')] ]

# Create the Window
window = sg.Window('Flipper STS', layout, resizable=True, icon=r'balls_1.ico', element_justification='center')


def detect_button():
    global i

    while True:
      if button.is_pressed:
        i += 1
        sleep(0.12)
        window['count'].update(i)
        print("test")


detect_button_th = Thread(target=detect_button)
detect_button_th.run()

# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break 
        

window.close()

如果您有任何线索或解决方案,非常感谢。

我这里没有gpiozero,只是为了说明要走的路。

import PySimpleGUI as sg
from threading import Thread
from time import sleep

i = 0
sg.theme('Dark blue 3')   # Add a touch of color
# All the stuff inside your window.
layout = [  [sg.Text('Bonjour et bienvenue en STS',font=("Helvetica", 25), text_color='white')],
            [sg.Text('Votre score est de :'+ str(i), key='count')],
            [sg.Button('Cancel')] ]

# Create the Window
window = sg.Window('Flipper STS', layout, finalize=True)

def detect_button():
    global i

    while True:
      if True:
        i += 1
        window.write_event_value('-COUNT-', i)
        sleep(1)

Thread(target=detect_button, daemon=True).start()

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == '-COUNT-':
        count = values[event]
        print(count)
        window['count'].update(str(count))

window.close()