TxT2Docx/gui_config.py
2025-09-21 19:01:40 +08:00

183 lines
8.7 KiB
Python

"""
GUI配置窗口模块
提供配置设置的图形界面。
"""
import PySimpleGUI as sg
from config import config
def show_config_window():
"""显示配置窗口"""
# 创建标签页布局
tab_file_layout = [
[sg.Text('文件处理设置', font=('bold', 12))],
[sg.HSeparator()],
[sg.Text('TXT编码:', size=(12, 1)),
sg.Combo(['utf-8', 'gbk', 'utf-16'], default_value=config.txt_encoding, key='txt_encoding', size=(15, 1))],
[sg.Text('匹配模式:', size=(12, 1))],
[sg.Radio('完全匹配(文件名与文件夹名相同)', 'match', default=config.match_pattern == "exact",
key='match_exact')],
[sg.Radio('前缀匹配', 'match', default=config.match_pattern == "prefix", key='match_prefix')],
[sg.Radio('包含匹配', 'match', default=config.match_pattern == "contains", key='match_contains')],
[sg.HSeparator()],
[sg.Text('输出位置:', size=(12, 1))],
[sg.Radio('输出到TXT文件所在文件夹', 'output_loc', default=config.output_location == "txt_folder",
key='output_txt_folder')],
[sg.Radio('输出到指定文件夹', 'output_loc', default=config.output_location == "custom", key='output_custom')]
]
tab_text_layout = [
[sg.Text('文字处理设置', font=('bold', 12))],
[sg.HSeparator()],
[sg.Checkbox('转换文字顺序', key='-REVERSE_TEXT-', default=config.reverse_text_order)],
[sg.Checkbox('替换标点符号(句号转逗号,保留结尾句号)', key='-REPLACE_PUNCTUATION-',
default=config.replace_punctuation)],
[sg.HSeparator()],
[sg.Text('错别字处理', font=('bold', 11), text_color='darkblue')],
[sg.Checkbox('启用错别字处理', key='-ENABLE_CHAR_ERRORS-', default=config.enable_char_errors,
enable_events=True)],
[sg.Text('错误强度:', size=(10, 1)),
sg.Slider(range=(0.0, 1.0), default_value=config.char_error_intensity, resolution=0.1,
orientation='h', size=(20, 15), key='char_error_intensity', disabled=not config.enable_char_errors)],
[sg.Text('错别字库路径:', size=(12, 1)),
sg.InputText(config.char_error_db_path, key='char_error_db_path', size=(30, 1),
disabled=not config.enable_char_errors),
sg.FileBrowse('浏览', file_types=(("JSON Files", "*.json"),), disabled=not config.enable_char_errors)],
[sg.HSeparator()],
[sg.Checkbox('添加免责声明', key='-ADD_DISCLAIMER-', default=config.add_disclaimer)]
]
tab_image_layout = [
[sg.Text('图片处理设置', font=('bold', 12))],
[sg.HSeparator()],
[sg.Text('图片排序方式:', size=(12, 1))],
[sg.Radio('按名称', 'sort', default=config.image_sort_by == "name", key='sort_name'),
sg.Radio('按修改时间', 'sort', default=config.image_sort_by == "time", key='sort_time')],
[sg.HSeparator()],
[sg.Text('图片尺寸调整:', size=(12, 1))],
[sg.Radio('不调整', 'resize', default=config.image_resize == "none", key='resize_none')],
[sg.Radio('按宽度:', 'resize', default=config.image_resize == "width", key='resize_width'),
sg.InputText(str(config.image_width), size=(8, 1), key='image_width'),
sg.Text('英寸')],
[sg.HSeparator()],
[sg.Text('图片对齐方式:', size=(12, 1))],
[sg.Radio('左对齐', 'align', default=config.image_alignment == "left", key='align_left'),
sg.Radio('居中', 'align', default=config.image_alignment == "center", key='align_center'),
sg.Radio('右对齐', 'align', default=config.image_alignment == "right", key='align_right')],
[sg.HSeparator()],
[sg.Text('图片不足时策略:', size=(12, 1))],
[sg.Radio('循环使用', 'strategy', default=config.image_strategy == "cycle", key='strategy_cycle')],
[sg.Radio('忽略多余标题', 'strategy', default=config.image_strategy == "truncate", key='strategy_truncate')],
[sg.Radio('重复最后一张', 'strategy', default=config.image_strategy == "repeat_last", key='strategy_repeat')]
]
tab_format_layout = [
[sg.Text('文档格式设置', font=('bold', 12))],
[sg.HSeparator()],
[sg.Text('行间距:', size=(12, 1)),
sg.InputText(str(config.line_spacing), size=(8, 1), key='line_spacing')],
[sg.Text('最大标题层级:', size=(12, 1)),
sg.Combo([1, 2, 3, 4, 5, 6], default_value=config.title_levels, key='title_levels', size=(8, 1))]
]
layout = [
[sg.TabGroup([
[sg.Tab('文件处理', tab_file_layout, key='tab_file')],
[sg.Tab('文字处理', tab_text_layout, key='tab_text')],
[sg.Tab('图片处理', tab_image_layout, key='tab_image')],
[sg.Tab('文档格式', tab_format_layout, key='tab_format')]
])],
[sg.HSeparator()],
[sg.Button('确定', size=(10, 1)), sg.Button('取消', size=(10, 1)), sg.Button('重置为默认', size=(12, 1))]
]
window = sg.Window('转换设置', layout, modal=True, resizable=True, size=(500, 450))
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, '取消'):
break
# 处理错别字启用/禁用事件
if event == '-ENABLE_CHAR_ERRORS-':
enabled = values['-ENABLE_CHAR_ERRORS-']
window['char_error_intensity'].update(disabled=not enabled)
window['char_error_db_path'].update(disabled=not enabled)
if event == '重置为默认':
# 重置为默认值
from config import Config
default_config = Config()
window['txt_encoding'].update(default_config.txt_encoding)
window['match_exact'].update(True)
window['output_txt_folder'].update(True)
window['-REVERSE_TEXT-'].update(default_config.reverse_text_order)
window['-REPLACE_PUNCTUATION-'].update(default_config.replace_punctuation)
window['-ENABLE_CHAR_ERRORS-'].update(default_config.enable_char_errors)
window['char_error_intensity'].update(default_config.char_error_intensity)
window['char_error_db_path'].update(default_config.char_error_db_path)
window['-ADD_DISCLAIMER-'].update(default_config.add_disclaimer)
window['sort_name'].update(True)
window['resize_none'].update(True)
window['image_width'].update(str(default_config.image_width))
window['align_center'].update(True)
window['strategy_cycle'].update(True)
window['line_spacing'].update(str(default_config.line_spacing))
window['title_levels'].update(default_config.title_levels)
if event == '确定':
# 保存配置
config.txt_encoding = values['txt_encoding']
if values['match_exact']:
config.match_pattern = "exact"
elif values['match_prefix']:
config.match_pattern = "prefix"
else:
config.match_pattern = "contains"
config.output_location = "txt_folder" if values['output_txt_folder'] else "custom"
config.image_sort_by = "name" if values['sort_name'] else "time"
config.image_resize = "none" if values['resize_none'] else "width"
config.reverse_text_order = values['-REVERSE_TEXT-']
config.replace_punctuation = values['-REPLACE_PUNCTUATION-']
config.add_disclaimer = values['-ADD_DISCLAIMER-']
# 错别字处理配置
config.enable_char_errors = values['-ENABLE_CHAR_ERRORS-']
config.char_error_intensity = values['char_error_intensity']
config.char_error_db_path = values['char_error_db_path']
try:
config.image_width = float(values['image_width'])
except:
pass
if values['align_left']:
config.image_alignment = "left"
elif values['align_right']:
config.image_alignment = "right"
else:
config.image_alignment = "center"
if values['strategy_cycle']:
config.image_strategy = "cycle"
elif values['strategy_truncate']:
config.image_strategy = "truncate"
else:
config.image_strategy = "repeat_last"
try:
config.line_spacing = float(values['line_spacing'])
config.title_levels = int(values['title_levels'])
except:
pass
from config import CONFIG_FILE_PATH
config.save_to_file(CONFIG_FILE_PATH)
break
window.close()