-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimplegui.py
161 lines (137 loc) · 5.72 KB
/
simplegui.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3
"""
Telsis language translator
GUI using PySimpleGUI
Copyright (c) 2021 Vivian Ng
"""
import os
import sys
import io
try:
import PySimpleGUI as sg
except ImportError as e:
print("PySimeplGUI is needed to run the GUI.")
sys.exit(1)
from languages import *
USER_FONT_SIZE = 24
UI_FONT_SIZE = 20
DEFAULT_FONT = "Courier"
TELSIS_FONT = "TelsisTyped"
def find_key(x, d):
# Helper function: find key in dictionary d with value of x
for key, value in d.items():
if x == value:
return key
return "key doesn't exist"
def on_press_translate_button(window, values, translator):
if values['source_text'] == "":
window['error_text'].update("Nothing to translate")
window['target_text'].update("")
window['telsis_text'].update("")
return
srclang = values['source_lang']
display_telsis_flag = False
if srclang != '':
srclang = find_key(values['source_lang'], avail_languages)
tgtlang = values['target_lang']
if tgtlang != '':
tgtlang = find_key(values['target_lang'], avail_languages)
srctext = values['source_text']
if srclang == '' and tgtlang == '':
window['error_text'].update("Source or target language must be defined")
window['target_text'].update("")
window['telsis_text'].update("")
elif srclang == tgtlang:
window['error_text'].update("Source and target language must be different")
window['target_text'].update("")
window['telsis_text'].update("")
else:
if srclang == 'telsis': # From Telsis language to another language
if tgtlang == '':
tgtlang = 'en' # Defaults to English when translating from Telsis
translator.telsis2lang(srctext, tgtlang)
display_telsis_flag = False
elif tgtlang == 'telsis': # From another language to Telsis language
translator.lang2telsis(srctext, srclang)
display_telsis_flag = True
elif srclang != 'telsis' and srclang != '': # From another language to Telsis language
translator.lang2telsis(srctext, srclang)
display_telsis_flag = True
elif tgtlang != 'telsis' and tgtlang != '': # From Telsis language to another language
if tgtlang == '':
tgtlang = 'en' # Defaults to English when translating from Telsis
translator.telsis2lang(srctext, tgtlang)
display_telsis_flag = False
window['target_text'].update(translator.results['tgt_text']) # Print out results of translation
window['error_text'].update("") # clear error message
if display_telsis_flag:
window['telsis_text'].update(translator.results['tgt_text']) # Print out results of translation in Telsis font
else:
window['telsis_text'].update("")
def on_press_display_button(window, values):
if values['source_text'] == '':
window['telsis_text'].update("")
window['error_text'].update("Nothing to display")
else:
window['telsis_text'].update(values['source_text'])
window['error_text'].update("")
def run_app(translator):
# Uncomment the following two lines to make the GUI:
# - On Linux systems, check if TelsisTyped.otf has been installed
# and attempt to install in .fonts directory if not
# - On Windows, register TelsisTyped.otf in the font system (untested)
#from install_fonts import install_fonts
#install_fonts()
sg.theme("Default")
sg.change_look_and_feel('DefaultNoMoreNagging')
avail_languages_list = []
for lang_code in avail_languages.keys():
avail_languages_list.append(avail_languages[lang_code])
source_label = sg.Text("Source language: ")
target_label = sg.Text("Target language: ")
source_list = sg.Combo(avail_languages_list, enable_events=True, key='source_lang')
target_list = sg.Combo(avail_languages_list, enable_events=True, key='target_lang')
source_text_label = sg.Text("Source text:")
source_text_input = sg.Multiline("", size=(45,5), key="source_text")
translate_button = sg.Button("Translate", key="_TRANSLATE_BUTTON_")
display_button = sg.Button("Display", key="_DISPLAY_BUTTON_")
target_text_label = sg.Text("Target text:")
target_text_input = sg.Multiline("", size=(45,5), key="target_text")
telsis_text_input = sg.Multiline("", size=(45,5), font=(TELSIS_FONT, UI_FONT_SIZE),
key="telsis_text")
error_text_label = sg.Multiline("", size=(45,1), key="error_text")
layout = [[source_label, source_list],
[target_label, target_list],
[source_text_label],
[source_text_input],
[translate_button, display_button],
[target_text_label],
[target_text_input],
[telsis_text_input],
[error_text_label]]
if sys.platform.startswith('win'):
iconfile = 'TelsisTranslatorIcon.ico'
else:
iconfile = 'TelsisTranslatorIcon.png'
window = sg.Window('Telsis Translator', layout,
icon=iconfile,
font=(DEFAULT_FONT, UI_FONT_SIZE))
while True: # Event Loop
event, values = window.read()
#print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == '_TRANSLATE_BUTTON_':
on_press_translate_button(window, values, translator)
if event == '_DISPLAY_BUTTON_':
on_press_display_button(window, values)
window.close()
def run_gui(trans):
if trans == "":
from telsistrans import telsis_translator
translator = telsis_translator()
else:
translator = trans
run_app(translator)
if __name__ == '__main__':
run_gui("")