forked from ils94/LOTROChatTranslator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_window.py
177 lines (134 loc) · 6.58 KB
/
create_window.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import tkinter as tk
from tkinter import tix
from tkinter import ttk, filedialog
import quick_translator
import json
import os
import supported_languages
import translate_chat
import threading
import center_windows
lotro_folder = os.path.join(os.path.expanduser("~"), "Documents", "The Lord of the Rings Online")
def create_window(window_name):
language_names = {v: k for k, v in supported_languages.languages.items()}
settings_file = "selected_languages.json"
stop_event = threading.Event()
translation_thread = None
def create_tooltip(widget, text):
balloon = tix.Balloon(widget, initwait=100)
balloon.bind_widget(widget, balloonmsg=text)
def save_settings():
settings = {
"chat_language": chat_var.get(),
"translator_language": translator_var.get(),
}
with open(settings_file, 'w') as f:
json.dump(settings, f)
def load_settings():
if os.path.exists(settings_file):
with open(settings_file, 'r') as f:
settings = json.load(f)
chat_var.set(settings.get("chat_language", list(language_names.keys())[0]))
translator_var.set(settings.get("translator_language", list(language_names.keys())[0]))
def on_enter(event):
selected_language = language_names[translator_var.get()]
x = threading.Thread(target=quick_translator.translate_my_message, args=(text2, selected_language))
x.setDaemon(True)
x.start()
return "break"
def open_file():
file_path = filedialog.askopenfilename(initialdir=lotro_folder, filetypes=[("Text Files", "*.txt")])
if file_path:
file_entry.delete(0, tk.END)
file_entry.insert(0, file_path)
def clear_chat():
text1.configure(state='normal')
text1.delete(1.0, tk.END)
text1.configure(state='disabled')
def save_chat_settings():
nonlocal translation_thread
clear_chat()
if translation_thread and translation_thread.is_alive():
stop_event.set()
translation_thread.join()
stop_event.clear()
selected_language = language_names[chat_var.get()]
translation_thread = threading.Thread(target=translate_chat.translate, args=(
window_name, text1, file_entry.get(), selected_language, stop_event))
translation_thread.setDaemon(True)
translation_thread.start()
def on_closing():
stop_event.set()
if translation_thread and translation_thread.is_alive():
translation_thread.join()
toplevel.destroy()
toplevel = tk.Toplevel()
toplevel.geometry("500x500")
toplevel.attributes("-topmost", True)
toplevel.title(f"LOTRO Chat Translator: {window_name}")
menu_bar = tk.Menu(toplevel)
toplevel.config(menu=menu_bar)
file_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Menu", menu=file_menu)
file_menu.add_command(label="Clear Chat", command=clear_chat)
file_menu.add_command(label="Save Settings", command=save_chat_settings)
center_windows.center_window(toplevel, 500, 500)
toplevel.grid_rowconfigure(0, weight=0)
toplevel.grid_rowconfigure(1, weight=0)
toplevel.grid_rowconfigure(2, weight=1)
toplevel.grid_columnconfigure(0, weight=1)
entry_frame = tk.Frame(toplevel)
entry_frame.grid(row=0, column=0, padx=5, pady=5, sticky="ew")
create_tooltip(entry_frame,
"The path to your chat log file.txt. You can paste the full path in the text box, or use the Browse button to open it.")
path_label = tk.Label(entry_frame, text="Path to the log file")
path_label.grid(row=0, column=0, padx=(0, 5), pady=5, sticky="w")
file_entry = tk.Entry(entry_frame)
file_entry.grid(row=0, column=1, padx=(0, 5), pady=5, sticky="ew")
file_button = tk.Button(entry_frame, text="Browse", width=10, command=open_file)
file_button.grid(row=0, column=2, padx=(0, 5), sticky="ew")
combobox_frame = tk.Frame(toplevel)
combobox_frame.grid(row=1, column=0, padx=5, pady=5, sticky="ew")
chat_label = tk.Label(combobox_frame, text="Chat")
chat_label.grid(row=0, column=0, padx=(0, 5))
chat_var = tk.StringVar()
chat_combobox = ttk.Combobox(combobox_frame, textvariable=chat_var, values=list(language_names.keys()),
state="readonly")
chat_combobox.grid(row=0, column=1, padx=(0, 5), sticky="ew")
create_tooltip(chat_combobox, "The language you want to translate the Chat.")
translator_label = tk.Label(combobox_frame, text="Translator")
translator_label.grid(row=0, column=2, padx=(0, 5))
translator_var = tk.StringVar()
translator_combobox = ttk.Combobox(combobox_frame, textvariable=translator_var, values=list(language_names.keys()),
state="readonly")
translator_combobox.grid(row=0, column=3, sticky="ew")
create_tooltip(translator_combobox, "The language you want to translate what you type in the Translator.")
text_frame = tk.Frame(toplevel)
text_frame.grid(row=2, column=0, padx=5, pady=5, sticky="nsew")
chat_text_label = tk.Label(text_frame, text="Chat")
chat_text_label.grid(row=0, column=0, sticky="w")
text1 = tk.Text(text_frame, height=10)
text1.grid(row=1, column=0, padx=5, pady=5, sticky="nsew")
text1.configure(state='disabled')
create_tooltip(text1, "Your translated chat will appear here.")
translator_text_label = tk.Label(text_frame, text="Translator")
translator_text_label.grid(row=2, column=0, sticky="w")
text2 = tk.Text(text_frame, height=5)
text2.grid(row=3, column=0, padx=5, pady=5, sticky="nsew")
create_tooltip(text2,
"Type something here, and press ENTER. The typed text will be changed to it's translated version. You can then just paste it (Ctrl + V) on LOTRO chat!")
text2.bind("<Return>", on_enter)
load_settings()
chat_combobox.bind("<<ComboboxSelected>>", lambda e: save_settings())
translator_combobox.bind("<<ComboboxSelected>>", lambda e: save_settings())
entry_frame.grid_columnconfigure(0, weight=0)
entry_frame.grid_columnconfigure(1, weight=1)
entry_frame.grid_columnconfigure(2, weight=0)
combobox_frame.grid_columnconfigure(0, weight=0)
combobox_frame.grid_columnconfigure(1, weight=1)
combobox_frame.grid_columnconfigure(2, weight=0)
combobox_frame.grid_columnconfigure(3, weight=1)
text_frame.grid_rowconfigure(1, weight=1)
text_frame.grid_rowconfigure(3, weight=0)
text_frame.grid_columnconfigure(0, weight=1)
toplevel.protocol("WM_DELETE_WINDOW", on_closing)