在 PySimpleGuiQT 中更新复选框的背景颜色

Update Background Color of Checkbox in PySimpleGuiQT

我正在尝试更新复选框被单击时的背景颜色。但是,在我调用更新方法的那一刻,同一元素上的另一个事件以某种方式被触发,导致意外行为。有人可以告诉我如何实现吗?!

我的最小代码如下:

import PySimpleGUIQt as sg

layout = [
    [sg.Checkbox('test', enable_events=True, key='test', background_color="green",default=True)]
]

window = sg.Window('Sample GUI', layout, finalize=True)
while True:  # Event Loop
    event, values = window.read(timeout=100)
    if event == sg.WINDOW_CLOSED:
        break
    elif event == "test":
        if not values[event]:
            window[event].update(background_color="red")
        else:
            window[event].update(background_color="green")

window.close()

Qt 端口仍在修改中,并非一切正常。

尝试在设置 background_color 时同时为复选框设置 value

import PySimpleGUIQt as sg

layout = [[sg.Checkbox('test', enable_events=True, key='test', background_color="green",default=True)]]
window = sg.Window('Sample GUI', layout, finalize=True)

while True:  # Event Loop
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event == "test":
        value = values[event]
        if not value:
            window[event].update(value=value, background_color="red")
        else:
            window[event].update(value=value, background_color="green")

window.close()