Skip to content

Instantly share code, notes, and snippets.

@wa5znu
Last active February 3, 2023 22:26
Show Gist options
  • Save wa5znu/9031c1907f9a344a72e9ac6d3f996ae2 to your computer and use it in GitHub Desktop.
Save wa5znu/9031c1907f9a344a72e9ac6d3f996ae2 to your computer and use it in GitHub Desktop.
import tkinter as tk
import requests
import json
import time
class App:
def __init__(self, master):
self.frame = tk.Frame (master)
self.frame.pack()
self.variable_a = tk.StringVar()
self.label1 = tk.Label(self.frame, textvariable=self.variable_a, width=20, height=10, relief="solid")
self.label1.pack(side="left")
self.variable_b = tk.StringVar()
self.label2 = tk.Label(self.frame, textvariable=self.variable_b, width=20, height=10, relief="solid")
self.label2.pack(side="right")
def update_variables (self):
response = requests.get("http://hipme.com/foo.json")
if response.status_code==200:
data = json.loads(response.text)
self.variable_a.set(data["a"])
self.variable_b.set(data["b"])
else:
self.variable_a.set("FAILED")
self.variable_b.set(response.status_code)
root = tk.Tk()
app = App(root)
last_time=0
while True:
now = time.time()
#import pdb; pdb.set_trace()
if now - last_time >= 1.0:
app.update_variables()
last_time = now
root.update_idletasks()
root.update()
@wa5znu
Copy link
Author

wa5znu commented Feb 3, 2023

it might be better to use tk.mainloop and code the updates asynchronously, but it's more complex to explain
this version uses a bit of CPU in the loop, so the fan will run :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment