Jan-15-2022, 05:17 AM
Hi All,
I am working on some code in tkinter for creating a small GUI to control relays with a simple toggle on/off button image. For some reason, the toggle buttons are not working properly and I cannot figure out why. I click one button and the other button switches its image. Once I get past this, I will have probably 6-8 buttons for relay controls. Hoping that someone can see an obvious issue here.... Thanks for any help
I am working on some code in tkinter for creating a small GUI to control relays with a simple toggle on/off button image. For some reason, the toggle buttons are not working properly and I cannot figure out why. I click one button and the other button switches its image. Once I get past this, I will have probably 6-8 buttons for relay controls. Hoping that someone can see an obvious issue here.... Thanks for any help
#!/usr/bin/env python """ http POST example """ from guizero import App, PushButton, Slider, Text import time import requests ESPR1_Relay_On = 'http://192.168.0.101/cm?cmnd=Power On' ESPR1_Relay_Off = 'http://192.168.0.101/cm?cmnd=Power Off' SONOFF_Relay01_On = 'http://192.168.0.113/cm?cmnd=Power On' SONOFF_Relay01_Off = 'http://192.168.0.113/cm?cmnd=Power Off' sonoff_R1_url = 'NOT_INIT' esp_R1_url = 'NOT_INIT' #global is_on on_sonoff_R1 = 0 on_esp_R1 = 0 class button: def __init__(self,index,image,grid): self.index = index self.image = image self.grid = grid def sonoff_R1_button(n): global on_sonoff_R1 print('SONOFF R1 STATUS()',n) if on_sonoff_R1 == 0: on_sonoff_R1 = 1 pushButtons[n].image = buttons[n].image+'_on.jpg' sonoff_R1_url = SONOFF_Relay01_On sonR1 = requests.post(sonoff_R1_url) else: on_sonoff_R1 = 0 pushButtons[n].image = buttons[n].image+'_off.jpg' sonoff_R1_url = SONOFF_Relay01_Off sonR1 = requests.post(sonoff_R1_url) def espR1_button(n): global on_esp_R1 print('ESP R1 STATUS()',n) if on_esp_R1 == 0: on_esp_R1 = 1 pushButtons[n].image = buttons[n].image+'_on.jpg' esp_R1_url = ESPR1_Relay_On espR1 = requests.post(esp_R1_url) else: on_esp_R1 = 0 pushButtons[n].image = buttons[n].image+'_off.jpg' esp_R1_url = ESPR1_Relay_Off espR1 = requests.post(esp_R1_url) path = '/home/pi/' buttons = [] buttons.append(button(0,'living_room',[0,0])) buttons.append(button(1,'kitchen',[1,0])) app = App(title="Keypad example", width=1280, height=520, layout="grid") app.bg='black' pushButtons=[] for button in buttons: pushButtons.append(PushButton(app, args=[button.index], command=espR1_button, grid=button.grid, align='left', image = path + button.image+'_off.jpg')) pushButtons.append(PushButton(app, args=[button.index], command=sonoff_R1_button, grid=button.grid, align='left', image = path + button.image+'_off.jpg')) def main(): #app.tk.attributes("-fullscreen",True) #app.tk.config(cursor='none') app.display() if __name__ == "__main__": main()