completed gui
This commit is contained in:
parent
ecc1ab1337
commit
3010def921
1 changed files with 45 additions and 28 deletions
69
app.py
69
app.py
|
|
@ -1,6 +1,5 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from typing import Any, cast
|
|
||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
from tkinter import ttk, messagebox, simpledialog
|
from tkinter import ttk, messagebox, simpledialog
|
||||||
from typing import final
|
from typing import final
|
||||||
|
|
@ -33,8 +32,10 @@ class SignControlGUI(tk.Tk):
|
||||||
frm_devices = ttk.LabelFrame(self, text="Devices")
|
frm_devices = ttk.LabelFrame(self, text="Devices")
|
||||||
frm_devices.pack(fill="both", expand=True, padx=5, pady=5)
|
frm_devices.pack(fill="both", expand=True, padx=5, pady=5)
|
||||||
|
|
||||||
self.lst_devices = tk.Listbox(frm_devices, height=6)
|
self.radios_frame = ttk.Frame(frm_devices)
|
||||||
self.lst_devices.pack(fill="both", expand=True, padx=5, pady=5)
|
self.radios_frame.pack(fill="both", expand=True, padx=5, pady=5)
|
||||||
|
|
||||||
|
self.selected_index = tk.IntVar(value=-1)
|
||||||
|
|
||||||
btn_frm = ttk.Frame(frm_devices)
|
btn_frm = ttk.Frame(frm_devices)
|
||||||
btn_frm.pack(fill="both", padx=5, pady=5)
|
btn_frm.pack(fill="both", padx=5, pady=5)
|
||||||
|
|
@ -78,18 +79,11 @@ class SignControlGUI(tk.Tk):
|
||||||
_ = self.bind("<Return>", lambda e: self._send_message())
|
_ = self.bind("<Return>", lambda e: self._send_message())
|
||||||
|
|
||||||
def _get_selection(self) -> tuple[int, NetBrite] | None:
|
def _get_selection(self) -> tuple[int, NetBrite] | None:
|
||||||
sel = cast(
|
idx = self.selected_index.get()
|
||||||
tuple[int],
|
if idx == -1:
|
||||||
self.lst_devices.curselection(), # pyright: ignore[reportUnknownMemberType]
|
|
||||||
)
|
|
||||||
|
|
||||||
if not sel:
|
|
||||||
_ = messagebox.showwarning("No device", "Please select a device first.")
|
_ = messagebox.showwarning("No device", "Please select a device first.")
|
||||||
return None
|
return None
|
||||||
|
return idx, self.devices[idx]
|
||||||
index = sel[0]
|
|
||||||
|
|
||||||
return (index, self.devices[index])
|
|
||||||
|
|
||||||
def _add_device(self):
|
def _add_device(self):
|
||||||
ip = simpledialog.askstring("Add device", "IP address:")
|
ip = simpledialog.askstring("Add device", "IP address:")
|
||||||
|
|
@ -100,13 +94,21 @@ class SignControlGUI(tk.Tk):
|
||||||
def _add_device_ip(self, ip: str):
|
def _add_device_ip(self, ip: str):
|
||||||
try:
|
try:
|
||||||
device = NetBrite(ip)
|
device = NetBrite(ip)
|
||||||
|
|
||||||
length = len(self.devices)
|
length = len(self.devices)
|
||||||
self._reset_zone(device, length)
|
|
||||||
|
|
||||||
|
self._reset_zone(device)
|
||||||
self.devices.append(device)
|
self.devices.append(device)
|
||||||
self.lst_devices.insert("end", f"{ip}")
|
|
||||||
self.lst_devices.selection_set(length)
|
radio = ttk.Radiobutton(
|
||||||
|
self.radios_frame,
|
||||||
|
text=ip,
|
||||||
|
value=length,
|
||||||
|
variable=self.selected_index,
|
||||||
|
)
|
||||||
|
radio.pack(anchor="w")
|
||||||
|
|
||||||
|
self.selected_index.set(length)
|
||||||
|
|
||||||
except NetbriteConnectionException as exc:
|
except NetbriteConnectionException as exc:
|
||||||
_ = messagebox.showerror("Connection error", str(exc))
|
_ = messagebox.showerror("Connection error", str(exc))
|
||||||
|
|
||||||
|
|
@ -114,18 +116,26 @@ class SignControlGUI(tk.Tk):
|
||||||
sel = self._get_selection()
|
sel = self._get_selection()
|
||||||
if sel is None:
|
if sel is None:
|
||||||
return
|
return
|
||||||
|
idx, _ = sel
|
||||||
|
|
||||||
idx = int(sel[0])
|
|
||||||
_ = self.devices.pop(idx)
|
_ = self.devices.pop(idx)
|
||||||
self.lst_devices.delete(idx)
|
|
||||||
|
radio = self.radios_frame.winfo_children()[idx]
|
||||||
|
radio.destroy()
|
||||||
|
|
||||||
|
for new_idx, widget in enumerate(self.radios_frame.winfo_children()):
|
||||||
|
if isinstance(widget, ttk.Radiobutton):
|
||||||
|
_ = widget.configure(value=new_idx)
|
||||||
|
|
||||||
|
self.selected_index.set(
|
||||||
|
-1 if not self.devices else min(idx, len(self.devices) - 1)
|
||||||
|
)
|
||||||
|
|
||||||
def _send_message(self):
|
def _send_message(self):
|
||||||
sel = self._get_selection()
|
sel = self._get_selection()
|
||||||
if sel is None:
|
if sel is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
print(sel)
|
|
||||||
|
|
||||||
_, dev = sel
|
_, dev = sel
|
||||||
raw_text = self.txt.get("1.0", "end-1c").strip()
|
raw_text = self.txt.get("1.0", "end-1c").strip()
|
||||||
if not raw_text:
|
if not raw_text:
|
||||||
|
|
@ -136,22 +146,29 @@ class SignControlGUI(tk.Tk):
|
||||||
dev.message(msg, DEFAULT_ZONE_NAME)
|
dev.message(msg, DEFAULT_ZONE_NAME)
|
||||||
self.txt.delete("1.0", "end")
|
self.txt.delete("1.0", "end")
|
||||||
except NetbriteTransferException as exc:
|
except NetbriteTransferException as exc:
|
||||||
_ = messagebox.showerror("Send failed", str(exc))
|
_ = messagebox.showerror(
|
||||||
|
"Send failed, try removing and adding the device.", str(exc)
|
||||||
|
)
|
||||||
|
|
||||||
def _reset_selected_zone(self):
|
def _reset_selected_zone(self):
|
||||||
sel = self._get_selection()
|
sel = self._get_selection()
|
||||||
if sel is None:
|
if sel is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
index, dev = sel
|
_, dev = sel
|
||||||
self._reset_zone(dev, index)
|
self._reset_zone(dev)
|
||||||
|
|
||||||
def _reset_zone(self, device: NetBrite, index: int):
|
def _reset_zone(self, device: NetBrite):
|
||||||
zone = DEFAULT_ZONE
|
zone = DEFAULT_ZONE
|
||||||
message = Message("{erase}Nr. " + str(index))
|
message = Message("hello")
|
||||||
|
|
||||||
|
try:
|
||||||
device.zones({DEFAULT_ZONE_NAME: zone})
|
device.zones({DEFAULT_ZONE_NAME: zone})
|
||||||
device.message(message, DEFAULT_ZONE_NAME)
|
device.message(message, DEFAULT_ZONE_NAME)
|
||||||
|
except NetbriteTransferException as exc:
|
||||||
|
_ = messagebox.showerror(
|
||||||
|
"Send failed, try removing and adding the device.", str(exc)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue