aboutsummaryrefslogtreecommitdiff
path: root/tkinter.py
blob: 136f1a26bca9dffa97981b1f97790ef5c00a615e (plain)
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
#!/usr/bin/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.path[0] + '/sd', *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()