Python Forum
TKinter JSON Key Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TKinter JSON Key Error
#5
** Ok, I think I have figured out some of it after making myself a bit crazy. I was actually trying to read the device information from the wrong section. This is a JSON file output from a SONOFF SWITCH using Tasmota Firmware. Here is what I have for now and it seems to be working as expected. However, it is messy and I know needs some cleaning. I come from VB.NET so Python has been much different to me for defined functions and how they get called.

I guess right now my question is: Do I create a def for the temperature? I also want to create a label to display the time, which I have the code, I just want to be sure I am laying it out correctly so I can call these in a correctly. I plan to add 3-4 additional switch/relay devices so I want to be sure it is done in a clean way.

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

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

on = PhotoImage(file = "lvgRoom_on.png")
off = PhotoImage(file = "lvgRoom_off.png")

sonoff01  = 'http://192.168.0.113/cm?cmnd=status'
sonURL = 'NOT_INIT'

sonURL = sonoff01
sonoff01 = requests.get(sonURL)

x = json.loads(sonoff01.text)
status = x['Status']
is_on = status["Power"]

#New Section for TEMPS --------------------------------------------
sonoffTemp = 'http://192.168.0.113/cm?cmnd=status+10'
tempURL = 'NOT_INIT'
tempURL = sonoffTemp
sonoffTemp = requests.get(tempURL)
i = json.loads(sonoffTemp.text)
statusT = i["StatusSNS"]
timestamp = statusT["Time"]
device_names = list(statusT.keys())[1:-1]
temp_units = statusT["TempUnit"]
templabel = Label(text="temp info", bg="White", fg="Black", font=("Helvetica", 12))
templabel.place(x=100, y=10)
print(timestamp)
#---------------------

for name in device_names:
    device = statusT[name]
    #print(f'ID={name}, Temperature={device["Temperature"]} {temp_units}')
    templabel.configure(text='Living Room '+f'Temperature: {device["Temperature"]} {temp_units}')


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://192.168.0.113/cm?cmnd=Power Off')
        is_on = False
        
    else:
        button.config(image = on)
        label.config(text = "Switch is On", fg = "green", bg='white')
        requests.post('http://192.168.0.113/cm?cmnd=Power 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 = "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 = "Switch Is Off!", bg='white', fg = "grey", font = ("Helvetica", 32)) 
    label.pack(pady = 20)

ws.mainloop()
Reply


Messages In This Thread
TKinter JSON Key Error - by Nu2Python - Jan-02-2023, 06:09 AM
RE: TKinter JSON Key Error - by deanhystad - Jan-02-2023, 06:32 AM
RE: TKinter JSON Key Error - by Nu2Python - Jan-02-2023, 06:58 PM
RE: TKinter JSON Key Error - by deanhystad - Jan-02-2023, 08:23 PM
RE: TKinter JSON Key Error - by Nu2Python - Jan-02-2023, 10:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] How to insert data json to treeview tkinter? Shakanrose 8 10,100 Jan-19-2023, 03:58 PM
Last Post: Shakanrose

Forum Jump:

User Panel Messages

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