I have a GUI based on PySimpleGUI (PySimpleGUI 3.4.2., Python 3.7.2, macOS Mojave 10.14.6) that works fine except buttons seem to be firing although disabled.
Users listen to 3 consecutive sounds and give a rating in my GUI by clicking a button. While the user listens to the sounds, the buttons are disabled so that a response cannot be given until all sounds are over. The buttons are greyed out successfully in the GUI (window.FindElement(button_label).Update(disabled=False)). However, if one presses a disabled button, the GUI then takes this response in as soon as the buttons are enabled (as if the response event was queued), even though no button was pressed after enabling the buttons, which messes up the GUI (e.g. an accidental double click is considered as the responses to two sets of 3 sounds.)
I've tried to find answers, but I haven't found any for PySimpleGUI. I have tried different ways to pause the code during sound presentation (sd.wait and time.sleep) just in case the buttons were enabled in the background somehow while the sound was played. I've tried to mess around with the events that are read in to work around the issue, but to no avail. I have had trouble with enabling these buttons with the .Update() approach before and only got the buttons to grey out once I added the window.Refresh() line. I somehow have to ensure that the buttons are not taking any inputs while disabled.
This is example code that is severely condensed from the real version. It is a working version in which the response buttons are disabled for 3 seconds after providing a response and all collected responses are printed. This visualises that buttons collect responses while being disabled.
import PySimpleGUI as sg
import time
response_buttons = ['b1', 'b2', 'b3', 'b4', 'b5']
current_event=[]
layout = [[sg.Button('start', key='start')],
[sg.Button('text5', key='b5')],
[sg.Button('text4', key='b4')],
[sg.Button('text3', key='b3')],
[sg.Button('text2', key='b2')],
[sg.Button('text1', key='b1')]]
window = sg.Window('GUI test').Layout(layout).Finalize()
for button_label in response_buttons:
window.FindElement(button_label).Update(disabled=True)
window.Refresh()
while True:
# Read the Window
event, values = window.Read()
print(event)
if event is None:
break
# Take appropriate action based on button
if event == 'start':
window.FindElement('start').Update(disabled=True)
for button_label in response_buttons:
window.FindElement(button_label).Update(disabled=False)
window.Refresh()
if event in response_buttons:
# collect and store response
current_event = current_event + [event]
# disable the buttons during sound presentation
for button_label in response_buttons:
window.FindElement(button_label).Update(disabled=True)
window.Refresh()
time.sleep(3)
for button_label in response_buttons:
window.FindElement(button_label).Update(disabled=False)
The goal is that a user can press a button once after listening to the sounds, then all buttons are disabled until the next set of sounds is over. Double-clicking a button and clicking a button while disabled will not result in a registered response.