-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemogui.py
executable file
·84 lines (59 loc) · 2.54 KB
/
demogui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#! /usr/bin/env python3
"""Script to drive a GUI for data visualization using tkinter."""
import datetime as dt
import random
import threading
import time
from tkinter import *
from tkinter import ttk
MAX_DATA_ENTRIES = 100 # Max number of entries to show in the Listbox
root = Tk()
root.title('Sensor Data Visualization | NSU Satellite Team')
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
main_frame = ttk.Frame(root, width=700, height=500, padding='5 5')
main_frame.grid(column=0, row=0, sticky='nwes')
main_frame.grid_propagate(0) # Disable auto-sizing of master
main_frame.columnconfigure(0, weight=1)
main_frame.columnconfigure(2, pad=5)
main_frame.rowconfigure(0, pad=5)
main_frame.rowconfigure((1, 2), weight=1)
main_frame.rowconfigure(3, pad=5)
lbl = ttk.Label(main_frame, text='Real-time temperature data from sensors')
lbl.grid(column=0, row=0, columnspan=2, sticky='nwe')
lbox = Listbox(main_frame, activestyle=NONE)
lbox.grid(column=0, row=1, rowspan=2, sticky='nwes')
sbar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=lbox.yview)
sbar.grid(column=1, row=1, rowspan=2, sticky='ns')
lbox['yscrollcommand'] = sbar.set
status = ttk.Label(main_frame, text='Latest entry: None', anchor='w')
status.grid(column=0, row=3, sticky='we')
populate_listbox_stop_event = threading.Event()
def populate_listbox(interval_secs=1.0):
"""Dummy data feed (randomized)."""
random.seed()
while not populate_listbox_stop_event.is_set():
timestamp = dt.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
temperature = float(random.randint(180, 280)) + random.random()
temperature_entry = f'{temperature:.2f} K ({timestamp})'
lbox.insert(END, temperature_entry)
lbox.see(END)
if lbox.index(END) > MAX_DATA_ENTRIES:
lbox.delete(0)
status['text'] = f'Latest entry: {temperature_entry}'
time.sleep(interval_secs)
threading.Thread(target=populate_listbox).start()
def trigger_populate_listbox_stop():
"""Callback to terminate the data-feed thread."""
if not populate_listbox_stop_event.is_set():
populate_listbox_stop_event.set()
btn = ttk.Button(main_frame, text='Stop Feed')
btn['command'] = trigger_populate_listbox_stop
btn.grid(column=2, row=2, sticky='se')
ttk.Sizegrip(main_frame).grid(column=2, row=3, sticky='se')
def handle_quit():
"""Callback to handle window-quitting after cleaning up."""
trigger_populate_listbox_stop() # Make sure the other thread is done
root.destroy()
root.protocol('WM_DELETE_WINDOW', handle_quit)
root.mainloop()