如何根据在 sg.Combo 中选择的内容更改 PySimpleGui 中的 windows?

How do I change windows in PySimleGui depending on what is selected in sg.Combo?

我需要为一个翻译游戏模组的程序创建一个图形界面。翻译器有多种模式,输入变量也不同。我希望界面根据 sg.Combo 中的 select 改变。我写了这样的代码: class 布局:

list_commands = [COMMANDS.ADDITIONAL_ENGLISH, COMMANDS.ADDITIONAL_RUSSIAN, COMMANDS.TRANSFER_FILE,
                 COMMANDS.ALL_TRANSLATE_DIRECTRY, COMMANDS.TRANSLATE_FILE, COMMANDS.ALL_TRANSFER_DIRECTORY,
                 COMMANDS.STREAMLINE_FILE, COMMANDS.STREAMLINE_DIRECTORY, COMMANDS.SEARCH_UPDATE_STRING_FILE,
                 COMMANDS.SEARCH_UNTRANS_STRING_FILE]

class INTERFACE:
    ADD_FILE_ENG = [[sg.Combo(values=list_commands, key='MODE', default_value=COMMANDS.ADDITIONAL_ENGLISH, size=(85, 1), enable_events=True)],
        [sg.Text('Оригинальный файл:', size=(20, 1))],
        [sg.InputText(key='GENERAL_PATH', size=(55, 1)), sg.FileBrowse(size=(10, 1), button_text='Обзор'), ],
        [sg.Text('Ваш файл:', size=(20, 1))],
        [sg.InputText(key='ADDITIONAL_FILE', size=(55, 1)), sg.FileBrowse(size=(10, 1), button_text='Обзор'), ],
        [sg.Button(button_text='Выполнить')]]

    ADD_FILE_RU = [[sg.Combo(values=list_commands, key='MODE', default_value=COMMANDS.ADDITIONAL_RUSSIAN, size=(85, 1), enable_events=True)],
        [sg.Text('Оригинальный файл:', size=(20, 1))],
        [sg.InputText(key='GENERAL_PATH', size=(55, 1)), sg.FileBrowse(size=(10, 1), button_text='Обзор'), ],
        [sg.Text('Ваш файл:', size=(20, 1))],
        [sg.InputText(key='ADDITIONAL_FILE', size=(55, 1)), sg.FileBrowse(size=(10, 1), button_text='Обзор'), ],
        [sg.Button(button_text='Выполнить')]]

接口变更

def change_interfase(self, mode):
        if mode == COMMANDS.ADDITIONAL_ENGLISH:
            window = sg.Window('LTA (localization translator assistant)', INTERFACE.ADD_FILE_ENG)
        elif mode == COMMANDS.ADDITIONAL_RUSSIAN:
            window = sg.Window('LTA (localization translator assistant)', INTERFACE.ADD_FILE_RU)

一个设置初始界面的函数

def get_default_interface(self):
        return sg.Window('LTA (localization translator assistant)', INTERFACE.DEFAULT, size=(700, 100))

切换windows

interface = work_with_interface.get_default_interface()

while True:
    try:
        event, values = interface.read()
        #print(event, values) #debug
        if event in (None, 'Exit', 'Cancel'):
            break
        elif event =='MODE':
            interface.Close()
            interface = work_with_interface.change_interfase(values['MODE'])

此代码有效,直到我 select sg.Combo 中的相同选项。然后代码崩溃并抛出以下错误:enter image description here 请告诉我如何修复它

INTERFACE.ADD_FILE_ENGADD_FILE_RUINTERFACE.DEFAULT中的元素初始化一次,只能使用一次。所以你需要创建函数来重新初始化所有元素。

如果你想通过调用函数获取布局

...
    def add_file_eng(self):
        return [
            [sg.Combo(values=list_commands, key='MODE', default_value=COMMANDS.ADDITIONAL_ENGLISH, size=(85, 1), enable_events=True)],
            [sg.Text('Оригинальный файл:', size=(20, 1))],
            [sg.InputText(key='GENERAL_PATH', size=(55, 1)), sg.FileBrowse(size=(10, 1), button_text='Обзор'), ],
            [sg.Text('Ваш файл:', size=(20, 1))],
            [sg.InputText(key='ADDITIONAL_FILE', size=(55, 1)), sg.FileBrowse(size=(10, 1), button_text='Обзор'), ],
            [sg.Button(button_text='Выполнить')],
        ]
...
    def change_interfase(self, mode):
            if mode == COMMANDS.ADDITIONAL_ENGLISH:
                window = sg.Window('LTA (localization translator assistant)', INTERFACE().add_file_eng())
...

或按属性

...
    @property
    def add_file_eng(self):
        return [
            [sg.Combo(values=list_commands, key='MODE', default_value=COMMANDS.ADDITIONAL_ENGLISH, size=(85, 1), enable_events=True)],
            [sg.Text('Оригинальный файл:', size=(20, 1))],
            [sg.InputText(key='GENERAL_PATH', size=(55, 1)), sg.FileBrowse(size=(10, 1), button_text='Обзор'), ],
            [sg.Text('Ваш файл:', size=(20, 1))],
            [sg.InputText(key='ADDITIONAL_FILE', size=(55, 1)), sg.FileBrowse(size=(10, 1), button_text='Обзор'), ],
            [sg.Button(button_text='Выполнить')],
        ]
...
    def change_interfase(self, mode):
            if mode == COMMANDS.ADDITIONAL_ENGLISH:
                window = sg.Window('LTA (localization translator assistant)', INTERFACE().add_file_eng)
...