step 6: add animation and high rate update stress tests
This commit is contained in:
51
examples/animations.py
Normal file
51
examples/animations.py
Normal file
@@ -0,0 +1,51 @@
|
||||
# pyright: reportGeneralTypeIssues=false
|
||||
|
||||
import math
|
||||
import time
|
||||
|
||||
import dearpygui.dearpygui as dpg
|
||||
|
||||
import dpg_gauges as dpgg
|
||||
|
||||
|
||||
def main() -> None:
|
||||
dpg.create_context()
|
||||
started = time.monotonic()
|
||||
|
||||
def update() -> None:
|
||||
elapsed = time.monotonic() - started
|
||||
dpgg.set_value("animated", 50 + math.sin(elapsed * 2.0) * 50)
|
||||
dpgg.set_value("smoothed", 50 + math.sin(elapsed * 7.0) * 50)
|
||||
dpg.set_frame_callback(dpg.get_frame_count() + 1, update)
|
||||
|
||||
try:
|
||||
with dpg.window(label="Animations", width=460, height=260):
|
||||
dpgg.digital_gauge(
|
||||
tag="animated",
|
||||
label="Animated",
|
||||
value=0,
|
||||
width=360,
|
||||
height=90,
|
||||
animation=dpgg.AnimationConfig(enabled=True, duration=0.35),
|
||||
)
|
||||
dpgg.bar_gauge(
|
||||
tag="smoothed",
|
||||
label="Smoothed",
|
||||
value=0,
|
||||
width=360,
|
||||
height=90,
|
||||
animation=False,
|
||||
smoothing=dpgg.SmoothingConfig(enabled=True, alpha=0.18, max_step=8),
|
||||
)
|
||||
|
||||
dpg.create_viewport(title="dpg-gauges animations", width=500, height=300)
|
||||
dpg.setup_dearpygui()
|
||||
dpg.set_frame_callback(1, update)
|
||||
dpg.show_viewport()
|
||||
dpg.start_dearpygui()
|
||||
finally:
|
||||
dpg.destroy_context()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
73
examples/live_high_rate.py
Normal file
73
examples/live_high_rate.py
Normal file
@@ -0,0 +1,73 @@
|
||||
# pyright: reportGeneralTypeIssues=false
|
||||
|
||||
import math
|
||||
import threading
|
||||
import time
|
||||
|
||||
import dearpygui.dearpygui as dpg
|
||||
|
||||
import dpg_gauges as dpgg
|
||||
|
||||
|
||||
def main() -> None:
|
||||
latest = {"rpm": 0.0, "speed": 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"] = 4000 + math.sin(elapsed * 9.0) * 3500
|
||||
latest["speed"] = 120 + math.sin(elapsed * 3.0) * 80
|
||||
time.sleep(1 / 240)
|
||||
|
||||
def gui_update() -> None:
|
||||
with lock:
|
||||
rpm = latest["rpm"]
|
||||
speed = latest["speed"]
|
||||
dpgg.set_value("rpm", rpm)
|
||||
dpgg.set_value("speed", speed)
|
||||
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="High Rate", width=620, height=360):
|
||||
dpgg.analog_gauge(
|
||||
tag="rpm",
|
||||
label="RPM",
|
||||
value=0,
|
||||
max_value=8000,
|
||||
redline_start=6500,
|
||||
width=240,
|
||||
height=240,
|
||||
animation=False,
|
||||
smoothing=dpgg.SmoothingConfig(enabled=True, alpha=0.22, max_step=500),
|
||||
)
|
||||
dpgg.digital_gauge(
|
||||
tag="speed",
|
||||
label="Speed",
|
||||
value=0,
|
||||
max_value=240,
|
||||
unit="km/h",
|
||||
width=260,
|
||||
height=90,
|
||||
animation=dpgg.AnimationConfig(enabled=True, duration=0.12),
|
||||
)
|
||||
|
||||
dpg.create_viewport(title="dpg-gauges high rate", width=660, height=400)
|
||||
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()
|
||||
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