pysimplegui 应用程序中的动态状态指示器

Dynamic status indicator in pysimplegui app

我正在尝试将状态指示器功能添加到我正在制作的小型 PySimpleGUI 应用程序中。当事件“Connect”执行无误时,我想将指示器设置为绿色,如果执行“Disconnect”,则指示器应设置为红色。可能吗?App preview

if event == "Connect":
    print("IP address", inputIP)
    try:
        process = subprocess.Popen(connecttotv.split(), stdout=subprocess.PIPE)
        output, error = process.communicate()
    except:
        window['-OUTPUT-'].update("Something went wrong")

if event == "Disconnect":
    try:
        process = subprocess.Popen(disconnecttotv.split(), stdout=subprocess.PIPE)
        output, error = process.communicate()
    except:
        window['-OUTPUT-'].update("Something went wrong")

您可以使用多种方式来指示,这里的一个例子是 sg.Text 的颜色。

import PySimpleGUI as sg

sg.theme("DarkBlue3")
sg.set_options(font=("Courier New", 16))

status = [('\u2B24'+' Disconnect', 'red'), ('\u2B24'+' Connect', 'green')]
state = 0

layout = [
    [sg.Text(text=status[state][0], text_color=status[state][1], size=(20, 1),
        justification='center', font=("Courier New", 24), key='INDICATOR')],
    [sg.Column([[sg.Button('Connect'), sg.Button('Disconnect')]], justification='center')],
]

window = sg.Window('Title', layout, finalize=True)

while True:
    event, values = window.read()
    if event in (sg.WINDOW_CLOSED, "Exit"):
        break
    elif event == 'Connect':
        state = 1
    elif event == 'Disconnect':
        state = 0
    if event in ('Disconnect', 'Connect'):
        value, text_color = status[state]
        window['INDICATOR'].update(value=value, text_color=text_color)

window.close()