Home > front end >  need simple sample code to create ui using pysimplegui
need simple sample code to create ui using pysimplegui

Time:09-17

I'm trying to create a ui for an api call heartbeat using pysimplegui the function is pretty simple. i will call send api call request every 5minutes to check whether the server is alive if alive then show as ok if the api call is failing then display not ok button i just need a simple code to start with so i can modify after that never used pysimplegui before

the ui just need to be like this:

API 1 - OK / NOT OK
API 2 - OK / NOT OK

appreciate if anyone can help

Thank you

CodePudding user response:

Not work for Python 2.7

If not take much time to get the status of heartbeat, loop with option timeout of method read of sg.Window, else multi-thread required.

from random import choice
import PySimpleGUI as sg

def heartbeat(port):
    return choice(choices)

choices = [False]   [True]*10

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

layout = [
    [sg.Text("API 1   ", key="API 1")],
    [sg.Text("API 2   ", key="API 2")],
]
window = sg.Window('Title', layout, finalize=True)

while True:

    event, values = window.read(timeout=300000)     # 5 minutes = 300000ms
    if event == sg.WINDOW_CLOSED:
        break
    elif event == sg.TIMEOUT_KEY:
        val1 = "API 1 OK" if heartbeat(1) else "API 1 NG"
        val2 = "API 2 OK" if heartbeat(2) else "API 2 NG"
        window['API 1'].update(val1)
        window['API 2'].update(val2)

window.close()
  • Related