PySimpleGUI 读取按钮事件,尽管当时按钮被禁用



我有一个基于PySimpleGUI(PySimpleGUI 3.4.2.,Python 3.7.2,macOS Mojave 10.14.6(的GUI,它工作正常,除了按钮似乎被禁用了。

用户收听 3 个连续的声音,并通过单击按钮在我的 GUI 中给出评级。当用户聆听声音时,按钮将被禁用,以便在所有声音结束之前无法给出响应。按钮在 GUI(窗口。FindElement(button_label(.更新(禁用=假((。但是,如果按下禁用的按钮,GUI 会在按钮启用后立即接收此响应(就像响应事件已排队一样(,即使启用按钮后没有按下任何按钮,也会弄乱 GUI(例如,意外双击被视为对两组 3 个声音的响应。

我试图找到答案,但我还没有找到任何关于PySimpleGUI的答案。我已经尝试了不同的方法来在声音演示期间暂停代码(sd.wait 和 time.sleep(,以防万一在播放声音时在后台以某种方式启用按钮。我试图弄乱阅读的事件以解决此问题,但无济于事。我在使用 .之前的 Update(( 方法,只有在我添加窗口后,按钮才会变灰。刷新(( 行。 我必须以某种方式确保按钮在禁用时不接受任何输入。

这是从真实版本严重压缩的示例代码。这是一个工作版本,其中响应按钮在提供响应后禁用 3 秒,并打印所有收集的响应。这可视化了按钮在禁用时收集响应。

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)

目标是用户可以在听完声音后按一次按钮,然后禁用所有按钮,直到下一组声音结束。双击按钮并在禁用时单击按钮不会导致注册响应。

你的逻辑不好。 按下text按钮后,您未正确设置按钮的状态。 我去掉了所有的声音内容,只是离开了GUI,所以我们只看那个,只看那个。

当您单击Text按钮时出现问题。 看看你的代码...您在 if 阻止中做的最后一件事是启用所有Text按钮。 它们应该被禁用,并且应该启用"开始"按钮。

这是一个工作演示,我相信它以您想要的方式设置按钮状态。 首先仅启用"开始"。 如果单击,它将禁用并启用文本按钮。 一切都工作正常。

import PySimpleGUI as sg
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()
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()
window.FindElement('start').Update(disabled=False)
window.Close()

最新更新