[go: up one dir, main page]

Menu

[ee0719]: / kakagui.py  Maximize  Restore  History

Download this file

94 lines (70 with data), 3.3 kB

 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
#!/usr/bin/python3
from tkinter import Tk, Label, Button, Checkbutton, messagebox, IntVar, PhotoImage, END, WORD, INSERT
from tkinter.scrolledtext import ScrolledText
from subprocess import run
# from threading import Thread
from pathlib import Path
import json
root = Tk()
# read configuration
config_path = Path(Path.home(), '.config', 'kakagui')
conf = json.loads(config_path.read_text()) if config_path.exists() else {}
# set options
furigana = IntVar(value=conf.get('furigana', 1))
romaji = IntVar(value=conf.get('romaji', 0))
dark = IntVar(value=conf.get('dark', 1))
# convert from Standard Japanese to kana/romaji #
def convert(evt=None):
# clear target textbox #
txt_tar.delete('1.0', END)
# Split continuous text to individual words #
word_split = run(["kakasi", "-w", "-i", "utf8", "-o", "utf8"], text=True,
input=txt_src.get('1.0', END), capture_output=True).stdout.strip()
txt_tar.insert(INSERT, word_split)
# Convert split text to furigana if checked #
if furigana.get():
txt_tar.insert(INSERT, "\n\n"+run(["kakasi", "-JH", "-i", "utf8", "-o", "utf8"],
input=word_split, text=True, capture_output=True).stdout)
# Convert split text to romaji if checked #
if romaji.get():
txt_tar.insert(INSERT, "\n\n"+run(["kakasi", "-Ja", "-Ha", "-Ka", "-i", "utf8", "-o", "utf8"],
input=word_split, text=True, capture_output=True).stdout)
def save_config():
# save config #
config_path.write_text(json.dumps({'furigana': furigana.get(), 'romaji': romaji.get(),
'dark': dark.get()}))
def clear(evt):
evt.widget.delete('1.0', END)
# Set Theme #
def set_theme():
if dark.get():
root.tk_setPalette(background="gray20", selectColor="gray15", selectBackground="gray40")
else:
root.tk_setPalette(background="gray90", selectColor="gray95", selectBackground="gray75")
## Defining gui elements ##
root.configure()
root.title("KakaGui")
root.iconphoto(True, PhotoImage(file=Path(Path(__file__).parent, "kakagui.png")))
set_theme()
# Create Text Boxes
txt_src = ScrolledText(root, width=50, height=20, wrap=WORD)
txt_tar = ScrolledText(root, width=50, height=20, wrap=WORD)
txt_src.grid(row=2, column=0, padx=10, pady=10, )
txt_tar.grid(row=2, column=2, padx=10, pady=10, )
# Clear text on ctrl+z and #
txt_src.bind('<Control-z>', clear)
txt_tar.bind('<Control-z>', clear)
# Unbind ctrl-a from jump to line start and bind to select all #
txt_src.event_delete('<<LineStart>>', '<Control-a>')
txt_src.event_add('<<SelectAll>>', '<Control-a>')
txt_tar.event_delete('<<LineStart>>', '<Control-a>')
txt_tar.event_add('<<SelectAll>>', '<Control-a>')
# Translate text from the window in focus on ctrl+return #
txt_src.bind('<Control-Return>', convert)
# Buttons and checkboxes #
Checkbutton(root, text="Kana", variable=furigana).grid(row=3, column=0)
Checkbutton(root, text="Romaji", variable=romaji).grid(row=4, column=0)
Checkbutton(root, text="Dark Mode", variable=dark, command=set_theme).grid(row=3, column=2)
Button(root, text=">>", command=convert).grid(row=2, column=1)
Button(root, text="Save Current Configuration", command=save_config).grid(row=4, column=2, pady=10)
root.mainloop()