-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgui.py
104 lines (83 loc) · 3.01 KB
/
gui.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import ipaddress
import logging
import os
import pathlib
import subprocess
import sys
import tkinter as tk
import tkinter.ttk
from console import MODEL_TO_URL, generate_image
logger = logging.getLogger(__name__)
def open_directory(path):
"""Open 'path' in explorer/finder/etc."""
if sys.platform == "win32":
return os.startfile(path)
if sys.platform == "darwin":
return subprocess.Popen(["open", path])
return subprocess.Popen(["xdg-open", path])
def create_window():
"""Create and return a Tkinter GUI window."""
window = tk.Tk()
window.title("PCDS PLC Image Generator")
row = -1
def to_row(*widgets):
nonlocal row
row += 1
for idx, widget in enumerate(widgets):
widget.grid(row=row, column=idx, sticky="ew")
plc_model_combo = tk.ttk.Combobox(
master=window, values=tuple(MODEL_TO_URL), state="readonly"
)
plc_name_entry = tk.Entry(master=window)
plc_desc_entry = tk.Entry(master=window)
plc_ip_entry = tk.Entry(master=window)
create_button = tk.Button(master=window, text="Create")
status_label = tk.Label(master=window)
to_row(tk.Label(master=window, text="PLC Model"), plc_model_combo)
to_row(tk.Label(master=window, text="PLC Name"), plc_name_entry)
to_row(tk.Label(master=window, text="PLC Description"), plc_desc_entry)
to_row(tk.Label(master=window, text="PLC IP Address"), plc_ip_entry)
to_row(create_button)
to_row(status_label)
def create_image(event):
model = plc_model_combo.get()
name = plc_name_entry.get().strip()
description = plc_desc_entry.get().strip()
addr = plc_ip_entry.get().strip()
if not name:
status_label.config(text="Please enter a PLC name")
return
if not model:
status_label.config(text="Please select a PLC model")
return
try:
ipaddress.IPv4Address(addr)
except Exception:
status_label.config(text="Please enter a valid IP address")
return
generation_desc = f"{model} {name!r} ({addr})"
status_label.config(text=f"Generating image for {generation_desc}...")
generate_image(
plc_model=model,
plc_name=name,
ip_address=addr,
plc_description=description,
auto_delete=True,
)
status_label.config(text=f"Generated image for {generation_desc}.")
image_path = pathlib.Path("images").resolve() / name
if image_path.exists():
try:
open_directory(image_path)
except Exception:
logger.exception("Failed to open the directory %s", image_path)
window.columnconfigure((0, 1), weight=1)
window.rowconfigure(tuple(range(row + 1)), weight=1)
create_button.bind("<Button-1>", create_image)
return window
def main():
"""Create the main tk window and run the main loop."""
window = create_window()
window.mainloop()
if __name__ == "__main__":
main()