Skip to content

Commit

Permalink
refactor: use Enum and format style
Browse files Browse the repository at this point in the history
  • Loading branch information
hank9999 committed Mar 4, 2024
1 parent 839e7ea commit 55bba51
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 35 deletions.
25 changes: 24 additions & 1 deletion const_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,32 @@

FIRMWARE_VERSION_LIST = ['萝狮虎', '萝狮虎扩容', '其他']
EEPROM_SIZE = ['8KiB (原厂)', '128KiB (1M)', '256KiB (2M)', '384KiB (3M)', '512KiB (4M)']
LANGUAGE_LIST = ['简体中文', 'en-US']


class FontType(Enum):
GB2312_COMPRESSED = '压缩GB2312'
GB2312_UNCOMPRESSED = '未压缩GB2312'
LOSEHU_FONT = '萝狮虎字库'


class LanguageType(Enum):
SIMPLIFIED_CHINESE = '简体中文'
ENGLISH = 'en-US'

@staticmethod
def find_value(value: str) -> 'LanguageType':
for item in LanguageType:
if item.value == value:
return item
return LanguageType.SIMPLIFIED_CHINESE

@staticmethod
def find_name(name: str) -> 'LanguageType':
for item in LanguageType:
if item.name == name:
return item
return LanguageType.SIMPLIFIED_CHINESE

@staticmethod
def value_list():
return list(map(lambda i: i.value, LanguageType))
14 changes: 7 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import tkinter as tk
import ttkbootstrap as ttk
import configparser
from const_vars import *
from const_vars import FIRMWARE_VERSION_LIST, EEPROM_SIZE, FontType, LanguageType
from logger import log
from translations import *

Expand Down Expand Up @@ -44,11 +44,11 @@
if 'theme' not in config['Settings']:
config['Settings']['theme'] = 'darkly'
if 'language' not in config['Settings']:
config['Settings']['language'] = LANGUAGE_LIST[0]
config['Settings']['language'] = LanguageType.SIMPLIFIED_CHINESE.name

config_version = config.get('ConfigVersion', 'configversion')
style = ttk.Style(config.get('Settings', 'theme'))
language = config.get('Settings', 'language')
language = LanguageType.find_name(config.get('Settings', 'language'))


class Tooltip(object):
Expand Down Expand Up @@ -125,7 +125,7 @@ def make_readonly(_):

def on_closing():
config['Settings']['theme'] = style.theme.name
config['Settings']['language'] = language
config['Settings']['language'] = language.name
if not os.path.exists(config_dir):
os.mkdir(config_dir)
with open(config_path, 'w') as configfile:
Expand All @@ -141,7 +141,7 @@ def change_theme(_, theme_combo: ttk.Combobox):

def change_language(_, language_combo: ttk.Combobox):
global language
language = language_combo.get()
language = LanguageType.find_value(language_combo.get())


def main():
Expand Down Expand Up @@ -177,8 +177,8 @@ def main():
label2 = tk.Label(frame2, text=translations[language]['now_state_none_text'])
label2.pack(side='left')

language_combo = ttk.Combobox(frame2, width=10, state='readonly', values=LANGUAGE_LIST)
language_combo.current(LANGUAGE_LIST.index(language))
language_combo = ttk.Combobox(frame2, width=10, state='readonly', values=LanguageType.value_list())
language_combo.current(LanguageType.value_list().index(language.value))
language_combo.pack(side='right', padx=(1, 3), pady=2)
language_combo.bind(
'<<ComboboxSelected>>',
Expand Down
54 changes: 27 additions & 27 deletions translations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from const_vars import LANGUAGE_LIST
from const_vars import LanguageType

translations = {
LANGUAGE_LIST[0]: {
LanguageType.SIMPLIFIED_CHINESE: {
# UI
'tool_name': 'K5/K6 小工具集',
'now_state_none_text': '当前操作: 无',
Expand All @@ -11,22 +11,22 @@
'firmware_label_text': '固件版本',

# Buttons
'clean_eeprom_button_text' : '清空EEPROM',
'auto_write_font_button_text' : '自动写入字库',
'read_calibration_button_text' : '读取校准参数',
'write_calibration_button_text' : '写入校准参数',
'read_config_button_text' : '读取配置参数',
'write_config_button_text' :'写入配置参数',
'write_font_conf_button_text' : '写入字库配置',
'write_tone_options_button_text' :'写入亚音参数',
'write_font_compressed_button_text' : '写入压缩字库',
'write_font_uncompressed_button_text' : '写入全量字库',
'write_font_old_button_text' : '写入字库 (旧)',
'write_pinyin_old_index_button_text' : '写入拼音表(旧)',
'write_pinyin_new_index_button_text' : '写入拼音表(新)',
'backup_eeprom_button_text' : '备份EEPROM',
'restore_eeprom_button_text' : '恢复EEPROM',
'todo_button_text' : '敬请期待',
'clean_eeprom_button_text': '清空EEPROM',
'auto_write_font_button_text': '自动写入字库',
'read_calibration_button_text': '读取校准参数',
'write_calibration_button_text': '写入校准参数',
'read_config_button_text': '读取配置参数',
'write_config_button_text': '写入配置参数',
'write_font_conf_button_text': '写入字库配置',
'write_tone_options_button_text': '写入亚音参数',
'write_font_compressed_button_text': '写入压缩字库',
'write_font_uncompressed_button_text': '写入全量字库',
'write_font_old_button_text': '写入字库 (旧)',
'write_pinyin_old_index_button_text': '写入拼音表(旧)',
'write_pinyin_new_index_button_text': '写入拼音表(新)',
'backup_eeprom_button_text': '备份EEPROM',
'restore_eeprom_button_text': '恢复EEPROM',
'todo_button_text': '敬请期待',

# Tooltip
'eeprom_size_combo_tooltip_text': 'EEPROM芯片容量,若自动检测正确则无需修改',
Expand All @@ -45,12 +45,12 @@
'write_font_old_button_tooltip_text': '117版本及之前版本使用,旧字库',
'write_pinyin_old_index_button_tooltip_text': '123版本拼音索引,如果不使用自动写入,请在执行完字库写入后点击',
'write_pinyin_new_index_button_tooltip_text': '124及以上版本拼音索引,如果不使用自动写入,请在执行完字库写入后点击',
'backup_eeprom_button_tooltip_text' : '备份EEPROM中的数据,使用EEPROM下拉框可以选择所要备份的大小',
'restore_eeprom_button_tooltip_text' : '恢复EEPROM中的数据,使用EEPROM下拉框可以选择所要恢复的大小',
'todo_button_tooltip_text' : '敬请期待',
'language_combo_tooltip_text':'更改语言,重启程序生效'
'backup_eeprom_button_tooltip_text': '备份EEPROM中的数据,使用EEPROM下拉框可以选择所要备份的大小',
'restore_eeprom_button_tooltip_text': '恢复EEPROM中的数据,使用EEPROM下拉框可以选择所要恢复的大小',
'todo_button_tooltip_text': '敬请期待',
'language_combo_tooltip_text': '更改语言,重启程序生效'
},
LANGUAGE_LIST[1]: {
LanguageType.ENGLISH: {
'tool_name': 'K5/K6 Tools',
'now_state_none_text': 'Now state: none',

Expand All @@ -71,8 +71,8 @@
'write_font_old_button_text': 'Write old font',
'write_pinyin_old_index_button_text': 'Write old index',
'write_pinyin_new_index_button_text': 'Write new index',
'backup_eeprom_button_text' : 'Backup EEPROM',
'restore_eeprom_button_text' : 'Restore EEPROM',
'backup_eeprom_button_text': 'Backup EEPROM',
'restore_eeprom_button_text': 'Restore EEPROM',
'todo_button_text': 'Coming soon',

'eeprom_size_combo_tooltip_text': 'EEPROM chip capacity, no need to modify if automatically detected correctly',
Expand All @@ -94,6 +94,6 @@
'backup_eeprom_button_tooltip_text': 'Backup data in EEPROM. Use the EEPROM dropdown to select the size to be backed up.',
'restore_eeprom_button_tooltip_text': 'Restore data in EEPROM. Use the EEPROM dropdown to select the size to be restored.',
'todo_button_tooltip_text': 'Coming soon',
'language_combo_tooltip_text':'Change language, take effect after restart.'
'language_combo_tooltip_text': 'Change language, take effect after restart.'
}
}
}

0 comments on commit 55bba51

Please sign in to comment.