step 6: add animation and high rate update stress tests
This commit is contained in:
58
examples/live_worst_case.py
Normal file
58
examples/live_worst_case.py
Normal file
@@ -0,0 +1,58 @@
|
||||
# pyright: reportGeneralTypeIssues=false
|
||||
|
||||
import math
|
||||
import random
|
||||
import threading
|
||||
import time
|
||||
|
||||
import dearpygui.dearpygui as dpg
|
||||
|
||||
import dpg_gauges as dpgg
|
||||
|
||||
|
||||
def main() -> None:
|
||||
latest = {"rpm": 0.0, "boost": 0.0, "fuel": 0.0, "battery": 0.0}
|
||||
lock = threading.Lock()
|
||||
stop = threading.Event()
|
||||
|
||||
def producer() -> None:
|
||||
started = time.monotonic()
|
||||
while not stop.is_set():
|
||||
elapsed = time.monotonic() - started
|
||||
with lock:
|
||||
latest["rpm"] = random.uniform(0, 8000)
|
||||
latest["boost"] = 15 + math.sin(elapsed * 25.0) * 15
|
||||
latest["fuel"] = 50 + math.sin(elapsed * 4.0) * 50
|
||||
latest["battery"] = random.uniform(0, 100)
|
||||
time.sleep(1 / 500)
|
||||
|
||||
def gui_update() -> None:
|
||||
with lock:
|
||||
snapshot = dict(latest)
|
||||
for tag, value in snapshot.items():
|
||||
dpgg.set_value(tag, value)
|
||||
dpg.set_frame_callback(dpg.get_frame_count() + 1, gui_update)
|
||||
|
||||
dpg.create_context()
|
||||
thread = threading.Thread(target=producer, daemon=True)
|
||||
try:
|
||||
with dpg.window(label="Worst Case", width=760, height=420), dpgg.gauge_grid(columns=4):
|
||||
dpgg.analog_gauge(tag="rpm", label="RPM", max_value=8000, width=180, height=180)
|
||||
dpgg.bar_gauge(tag="boost", label="Boost", max_value=30, width=180, height=90)
|
||||
dpgg.level_gauge(tag="fuel", label="Fuel", width=120, height=180)
|
||||
dpgg.battery_gauge(tag="battery", label="Battery", width=180, height=90)
|
||||
|
||||
dpg.create_viewport(title="dpg-gauges worst case", width=800, height=460)
|
||||
dpg.setup_dearpygui()
|
||||
thread.start()
|
||||
dpg.set_frame_callback(1, gui_update)
|
||||
dpg.show_viewport()
|
||||
dpg.start_dearpygui()
|
||||
finally:
|
||||
stop.set()
|
||||
thread.join(timeout=1.0)
|
||||
dpg.destroy_context()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user