#!/home/user_name/project/venv/bin/python3.11
import tkinter as tk
import RPi.GPIO as GPIO
# Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.OUT)
GPIO.output(24, GPIO.LOW) # Initial state is off
# Shared state variable to track GPIO pin state
gpio_state = False # False means off, True means on
def update_button_state():
"""Update button state based on the GPIO pin state"""
global gpio_state
if GPIO.input(24) == GPIO.HIGH:
gpio_state = True
btn1.config(text="LED ON")
lbl_title.configure(text="cooling on",fg='green')
else:
gpio_state = False
btn1.config(text="LED OFF")
lbl_title.configure(text="Warning cooling pump off",fg='red')
def led1():
"""Toggle the LED state and reflect changes across interfaces"""
global gpio_state
if gpio_state:
GPIO.output(24, GPIO.LOW)
gpio_state = False
else:
GPIO.output(24, GPIO.HIGH)
gpio_state = True
update_button_state() # Update button text based on the state
def close():
"""Cleanup GPIO pins on exit"""
GPIO.cleanup()
root.destroy()
# Tkinter GUI setup
root = tk.Tk()
root.configure(bg="black")
root.geometry("400x400")
# Custom top bar with a close button
top_bar = tk.Frame(root, bg="gray20", height=25)
top_bar.pack(fill=tk.X, side=tk.TOP)
lbl_title=tk.Label(top_bar,bg="gray20", text='',font=('Arial',18))
lbl_title.pack(side=tk.LEFT)
btn_close = tk.Button(top_bar, text="✖", bg="red", fg="white", command=close, width=3)
btn_close.pack(side=tk.RIGHT, padx=2)
# LED Toggle Button
btn1 = tk.Button(root, text="LED OFF", command=led1, height=2, width=10)
btn1.pack(pady=20)
# Update the button text on a timer, simulating polling for the GPIO state
def periodic_update():
update_button_state() # Check and update button state
root.after(1000, periodic_update) # Update every 1 second
periodic_update()
# Run the main loop
root.mainloop()