SDC/tkinter.py

51 lines
1.1 KiB
Python

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()