Python Forum
tkinter toggle buttons not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter toggle buttons not working
#27
ok deanhystad, here is my newest code that I slapped together... it is messy and I know that already. It does work, and it does tell me what the current state is of the relay and starts the window with button state ON or OFF based on the status.

from tkinter import *
from tkinter import messagebox
import requests
import time
import os
import json

ws = Tk() 
ws.title('Relay Control')
ws.geometry("600x400")
ws.configure(background='white')

on = PhotoImage(file = "frPorch_on.png")
off = PhotoImage(file = "frPorch_off.png")


shelly01  = 'http://shelly1-483fda82683f/status'
shURL = 'NOT_INIT'


shURL = shelly01
sh01 = requests.get(shURL)
x = json.loads(sh01.text)
status = x["relays"]
a = status
is_on = a[0]['ison']
print(is_on)

  
def Switch():
    global is_on

    
    if is_on:
        button.config(image = off)
        label.config(text = "Switch is Off", fg = "grey", bg='white')
        requests.post('http://shelly1-483fda82683f/relay/0/?turn=off')
        is_on = False
        
    else:
        
        button.config(image = on)
        label.config(text = "Switch is On", fg = "green", bg='white')
        requests.post('http://shelly1-483fda82683f/relay/0/?turn=on')
        is_on = True



if is_on == True:
    button = Button(ws, image = on, bg='white', bd = 0, command = Switch)
    button.pack(pady = 30)
    label = Label(ws, text = "The Switch Is On!", bg='white', fg = "green", font = ("Helvetica", 32)) 
    label.pack(pady = 20)
else:
    button = Button(ws, image = off, bg='white', bd = 0, command = Switch)
    button.pack(pady = 30)
    label = Label(ws, text = "The Switch Is Off!", bg='white', fg = "grey", font = ("Helvetica", 32)) 
    label.pack(pady = 20)


ws.mainloop()
Reply


Messages In This Thread
tkinter toggle buttons not working - by Nu2Python - Jan-15-2022, 05:17 AM
RE: tkinter toggle buttons not working - by Yoriz - Jan-15-2022, 02:39 PM
RE: tkinter toggle buttons not working - by Nu2Python - Jan-23-2022, 06:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Tkinter inside function not working Ensaimadeta 5 5,331 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  [Tkinter] Radio Buttons Working Bassackwards gw1500se 6 2,415 Dec-07-2021, 07:13 PM
Last Post: menator01
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,197 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  TkInter Binding Buttons ifigazsi 5 4,899 Apr-06-2020, 08:30 AM
Last Post: ifigazsi
  python file(.py) not working with my tkinter project DeanAseraf1 9 7,451 Mar-22-2020, 10:58 PM
Last Post: ifigazsi
  Tkinter scaling windows conten to or with its size not working Detzi 5 4,632 Jan-12-2020, 12:42 PM
Last Post: Detzi
  Issue on tkinter with buttons Reldaing 1 2,507 Jan-07-2020, 08:21 AM
Last Post: berckut72
  Need tkinter help with clicking buttons pythonprogrammer 2 2,549 Jan-03-2020, 04:43 AM
Last Post: joe_momma
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 3,877 Dec-16-2019, 04:47 AM
Last Post: woooee
  Tkinter Buttons action d3fi 1 2,069 Nov-20-2019, 09:16 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020