#python #pysimplegui
Вопрос:
Я хочу сделать счетчик с физической кнопкой, используя gpiozero. Я следую инструкциям в документации, но это явно не работает. Вот шаги, которые я выполнил в документации.
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()
Если у вас есть какие-либо подсказки или решения, очень благодарны.
Комментарии:
1. Попробуйте
Thread(target=detect_button, daemon=True).start()
и не обновляйте элементы в потоке , попробуйте использоватьwindow.write_event_value('-COUNT-', i)
для отправки события в'-COUNT-'
цикл событий, а затем обновите элемент в цикле событий.
Ответ №1:
У меня здесь нет 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()