aboutsummaryrefslogtreecommitdiff
path: root/tkinter.py
diff options
context:
space:
mode:
Diffstat (limited to 'tkinter.py')
-rw-r--r--tkinter.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/tkinter.py b/tkinter.py
new file mode 100644
index 0000000..99dcaa1
--- /dev/null
+++ b/tkinter.py
@@ -0,0 +1,50 @@
+import subprocess
+import sys
+import tkinter
+from tkinter import ttk
+from tkinter import font
+
+
+# Initialize Tk root
+root = tkinter.Tk()
+root.geometry('1280x720')
+root.title('SD GUI')
+
+# Start SD
+proc = subprocess.Popen(
+ sys.argv[1:] + ['-n'], stdin=subprocess.PIPE, stdout=subprocess.PIPE
+)
+
+# Display key
+key = proc.stdout.readline().decode('utf-8')
+label = ttk.Label(root, text=key, font=font.Font(size=48), wraplength=900)
+label.place(relx=0.5, rely=0.5, anchor='center')
+
+
+def key_handler(event):
+ if label['text'].count('\n') == 2 and event.char not in 'yn':
+ # Manually quit here because stdout.readline blocks and doesn't error when SD exits
+ root.quit()
+
+ # Write to SD
+ proc.stdin.write((event.char + '\n').encode('utf-8'))
+ proc.stdin.flush()
+
+ # Get response
+ text = proc.stdout.readline().decode('utf-8')
+ if text.startswith('>'):
+ # Key
+ label['text'] = text
+ else:
+ # Value
+ label['text'] += text
+
+
+def on_closing():
+ root.quit()
+
+
+# Run the main loop
+root.bind('<Key>', key_handler)
+root.protocol('WM_DELETE_WINDOW', on_closing)
+root.mainloop()