Python Forum
tkinter toggle buttons not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter toggle buttons not working
#22
deanhystad, here is my current code. please let me know how this looks and if it looks like the correct way. Thanks

import tkinter as tk
from tkinter import *
from tkinter import messagebox
import requests
import http.client
import time
import json
   
path = ""

SONOFF_Temp  = 'http://192.168.0.113/cm?cmnd=status+10'
sonoff_url = 'NOT_INIT'


class RelayButton(tk.Button):
    """A button that toggles a relay"""
    def __init__(self, parent, url="", on_image=None, off_image=None, **kwargs):
        super().__init__(parent, image=off_image, command=self.toggle)
        self.url = url
        self._on = False
        self.on_image = on_image
        self.off_image = off_image
        self.label = Label(text="", fg="Black", font=("Helvetica", 12))
        self.label.place(x=10,y=15)
        self.update_clock()
        self.templabel = Label(text="temp info", fg="Black", font=("Helvetica", 12))
        self.templabel.place(x=10, y=40)
        self.getTemp()
        #self.master.configure(background='white')  <<< Frame Color
        
    @property
    def on(self):
        """Return state of the relay"""
        return self._on
   
    @on.setter
    def on(self, on):
        """Set state of the relay"""
        self._on = on

        if on:
            requests.post(f"{self.url}cm?cmnd=Power On")
            self["image"] = self.on_image
        else:
            requests.post(f"{self.url}cm?cmnd=Power Off")
            self["image"] = self.off_image
    
    def toggle(self):
        """Toggle state of the relay"""
        self.on = not self.on
        
    def update_clock(self):
        now = time.strftime("%I:%M:%S %p " + ' - ' + "%x")
        self.label.configure(text='Time/Date:  ' + now)
        self.after(1000, self.update_clock)
        
    def getTemp(self):
        sonoff_url = SONOFF_Temp
        sonR1 = requests.get(sonoff_url)
        x = json.loads(sonR1.text)
        status = x["StatusSNS"]
        timestamp = status["Time"]
        device_names = list(status.keys())[1:-1]
        temp_units = status["TempUnit"]
        
        for name in device_names:
            device = status[name]
            #print(f'ID={name}, Temperature={device["Temperature"]} {temp_units}')
            self.templabel.configure(text='Living Room '+f'Temperature={device["Temperature"]} {temp_units}')
            self.after(5000, self.getTemp)
            
   
def new_button(name, url, parent, row, column, **kwargs):
    """Convenience functon that creates a RelayButton"""
    on_image = tk.PhotoImage(file=f"{path}{name}_on.png")
    off_image = tk.PhotoImage(file=f"{path}{name}_off.png")
    button = RelayButton(parent, url, on_image, off_image, **kwargs)
    button.grid(row=row, column=column, padx=50, pady=110)
    return button


   
def main():
    app = tk.Tk()
    app.title('Home Relay Controls')
    buttons = [
        new_button('living', "http://192.168.0.101/", app, 0, 0),
        new_button('front_porch', "http://192.168.0.113/", app, 0, 1)
    ]
    
    for b in buttons:
        b.on = False
    app.mainloop()
    
    
if __name__ == "__main__":
    main()
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-19-2022, 11:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Tkinter inside function not working Ensaimadeta 5 5,413 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  [Tkinter] Radio Buttons Working Bassackwards gw1500se 6 2,458 Dec-07-2021, 07:13 PM
Last Post: menator01
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,312 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  TkInter Binding Buttons ifigazsi 5 5,032 Apr-06-2020, 08:30 AM
Last Post: ifigazsi
  python file(.py) not working with my tkinter project DeanAseraf1 9 7,537 Mar-22-2020, 10:58 PM
Last Post: ifigazsi
  Tkinter scaling windows conten to or with its size not working Detzi 5 4,700 Jan-12-2020, 12:42 PM
Last Post: Detzi
  Issue on tkinter with buttons Reldaing 1 2,529 Jan-07-2020, 08:21 AM
Last Post: berckut72
  Need tkinter help with clicking buttons pythonprogrammer 2 2,567 Jan-03-2020, 04:43 AM
Last Post: joe_momma
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 3,911 Dec-16-2019, 04:47 AM
Last Post: woooee
  Tkinter Buttons action d3fi 1 2,083 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