import tkinter as tk
import serial
import time

ser = serial.Serial('COM5', 9600, timeout=0.05)
time.sleep(2)

def read_serial():
    if ser.in_waiting > 0:
        line = ser.readline().decode('utf-8', errors='ignore').strip()
        if line == "PRESSED":
            btn_status.config(text="Button: ON", bg="lightgreen")
        elif line == "RELEASED":
            btn_status.config(text="Button: OFF", bg="lightgray")
    root.after(50, read_serial)

def on_close():
    ser.close()
    root.destroy()

root = tk.Tk()
root.title("Button Status")
root.geometry("250x120")
root.protocol("WM_DELETE_WINDOW", on_close)

btn_status = tk.Label(root, text="Button: OFF", font=("Arial", 14, "bold"), bg="lightgray", width=16, pady=15)
btn_status.pack(pady=30)

read_serial()
root.mainloop()