动态更新多个窗口设置中的元素

发布于 2025-01-21 11:26:43 字数 2449 浏览 1 评论 0原文

我有一个关于在多个窗口设置中动态更新字段的快速问题,例如上面的示例。我想要它,以便当用户选择自动填充时,值[saveInventoryPath]被打印到设置窗口中的“ testInputText”字段时,但是当我使用窗口时。如何将Pysimplegui引导到设置窗口?

编辑:感谢您的帮助,我已经编辑了代码,并且自动填充功能目前有效,autofill似乎可以冻结窗口,但我希望人们能够选择autofill,然后点击保存,然后settings窗口刷新,但是我不确定当前在哪里放置,而true代码>循环。再次感谢您为此提供帮助。

def create_settings_window(settings):
   sg.theme('LightGrey6')
   settings = load_settings(SETTINGS_FILE, DEFAULT_SETTINGS )
   def TextLabel(text): return sg.Text(text+':', justification='r', size=(20,1))


   layout = [  [sg.Text('Set Up Connection', font='Any 15')],
            [TextLabel('Inventory List'), sg.Input(key='SAVEINVENTORYPATH'), sg.FileBrowse(key='INVENTORYLIST')],
            [sg.Text(text='', key = 'testinputtext')],
            [sg.Button('Save'), sg.Button('Autofill'), sg.Button('Exit')]  ]

   window = sg.Window('Settings', layout, keep_on_top=True, finalize=True,element_padding = (3,3.5))

   test = window['testinputtext']
   event, values = window.read()

   if event == 'Autofill':
      test.update(value = 'hi')

   if event == 'Save':
      save_settings(SETTINGS_FILE, settings, values)
      sg.popup('Settings saved')
  else:
    print(event, values)

   for key in SETTINGS_KEYS_TO_ELEMENT_KEYS:  
    try:
        window[SETTINGS_KEYS_TO_ELEMENT_KEYS[key]].update(value=settings[key])
    except Exception as e:
        print(f'Problem updating PySimpleGUI window from settings. Key = {key}')
    
return window

def main():
  window, settings = None, load_settings(SETTINGS_FILE, DEFAULT_SETTINGS )
  while True:             # Event Loop
    #LAYOUT
    if window is None:
        settings = load_settings(SETTINGS_FILE, DEFAULT_SETTINGS )
        sg.theme('LightGrey6')
        

        layout = [[sg.Multiline(size=(180,10),key='Output',pad=(0,20))],
                  [sg.Button(button_text = 'Settings',key='Settings'),sg.Button(button_text = 'Load Output',key='LOAD_OUTPUT'),sg.Exit()]]
       
        window = sg.Window('Settings Dynamic Update Test', layout, element_justification='center', size= (600,300))
    
    event, values = window.read()


    if event == sg.WIN_CLOSED or event == 'Exit':
        break
        window.close()    
        
    if event == 'Settings':
        create_settings_window(settings)
            
    else:
        print(event, values)
    
  window.close()
main()
    

I have a quick question about updating fields dynamically in a multiple window setup such as the example above. I want it so that when the user selects autofill, values[SAVEINVENTORYPATH] is printed to the 'testinputtext' field in the settings window however when I use window.update, it searches for the keys in the main window rather than the settings window. How can I direct PySimpleGUI towards the settings window?

EDIT: Thank you for your help, I have edited the code and the autofill function works however currently, Autofill appears to freeze the window but I want people to be able to select Autofill and then hit Save and then the Settings window refreshes however I'm not sure where to currently place the while True loop. Thank you again for your assistance with this.

def create_settings_window(settings):
   sg.theme('LightGrey6')
   settings = load_settings(SETTINGS_FILE, DEFAULT_SETTINGS )
   def TextLabel(text): return sg.Text(text+':', justification='r', size=(20,1))


   layout = [  [sg.Text('Set Up Connection', font='Any 15')],
            [TextLabel('Inventory List'), sg.Input(key='SAVEINVENTORYPATH'), sg.FileBrowse(key='INVENTORYLIST')],
            [sg.Text(text='', key = 'testinputtext')],
            [sg.Button('Save'), sg.Button('Autofill'), sg.Button('Exit')]  ]

   window = sg.Window('Settings', layout, keep_on_top=True, finalize=True,element_padding = (3,3.5))

   test = window['testinputtext']
   event, values = window.read()

   if event == 'Autofill':
      test.update(value = 'hi')

   if event == 'Save':
      save_settings(SETTINGS_FILE, settings, values)
      sg.popup('Settings saved')
  else:
    print(event, values)

   for key in SETTINGS_KEYS_TO_ELEMENT_KEYS:  
    try:
        window[SETTINGS_KEYS_TO_ELEMENT_KEYS[key]].update(value=settings[key])
    except Exception as e:
        print(f'Problem updating PySimpleGUI window from settings. Key = {key}')
    
return window

def main():
  window, settings = None, load_settings(SETTINGS_FILE, DEFAULT_SETTINGS )
  while True:             # Event Loop
    #LAYOUT
    if window is None:
        settings = load_settings(SETTINGS_FILE, DEFAULT_SETTINGS )
        sg.theme('LightGrey6')
        

        layout = [[sg.Multiline(size=(180,10),key='Output',pad=(0,20))],
                  [sg.Button(button_text = 'Settings',key='Settings'),sg.Button(button_text = 'Load Output',key='LOAD_OUTPUT'),sg.Exit()]]
       
        window = sg.Window('Settings Dynamic Update Test', layout, element_justification='center', size= (600,300))
    
    event, values = window.read()


    if event == sg.WIN_CLOSED or event == 'Exit':
        break
        window.close()    
        
    if event == 'Settings':
        create_settings_window(settings)
            
    else:
        print(event, values)
    
  window.close()
main()
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欲拥i 2025-01-28 11:26:44

最好处理自己的事件循环中的每个窗口,将窗户混合在一起可能会使事情变得困难或复杂。

以下示例显示了要走的路。

import PySimpleGUI as sg

def popup():
    sg.theme('DarkBlue4')
    layout = [
        [sg.Text("You name"), sg.Input("", key='Name')],
        [sg.Text("", size=0, key='Test')],
        [sg.Button('Save'), sg.Button('Auto Fill'), sg.Button('Cancel')],
    ]
    window = sg.Window('Settings', layout, modal=True)
    while True:
        event, values = window.read()
        if event in (sg.WIN_CLOSED, 'Cancel'):
            flag = False
            break
        elif event == 'Save':
            settings['Name'] = values['Name']
            flag = True
            break
        elif event == 'Auto Fill':
            window['Test'].update(values['Name'])
    window.close()
    return flag

def main_window():
    sg.theme('DarkBlue3')
    name = settings['Name']
    value = name if name else 'None'
    layout = [
        [sg.Text("User Name:"), sg.Text(value, key='Name')],
        [sg.Button('Settings'), sg.Button('Exit')],
    ]
    return sg.Window('Main', layout)

settings = {'Name':None}
window = main_window()
while True:
    event, values = window.read()
    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    elif event == 'Settings':
        flag = popup()
        if flag:
            window.close()
            window = main_window()
            """ or just update element
            name = settings['Name']
            value = name if name else 'None'
            window['Name'].update(value)
            """
window.close()

It' better to handle each window in it's own event loop, mix windows together may get things mush difficult or complex.

Following example show the way to go.

import PySimpleGUI as sg

def popup():
    sg.theme('DarkBlue4')
    layout = [
        [sg.Text("You name"), sg.Input("", key='Name')],
        [sg.Text("", size=0, key='Test')],
        [sg.Button('Save'), sg.Button('Auto Fill'), sg.Button('Cancel')],
    ]
    window = sg.Window('Settings', layout, modal=True)
    while True:
        event, values = window.read()
        if event in (sg.WIN_CLOSED, 'Cancel'):
            flag = False
            break
        elif event == 'Save':
            settings['Name'] = values['Name']
            flag = True
            break
        elif event == 'Auto Fill':
            window['Test'].update(values['Name'])
    window.close()
    return flag

def main_window():
    sg.theme('DarkBlue3')
    name = settings['Name']
    value = name if name else 'None'
    layout = [
        [sg.Text("User Name:"), sg.Text(value, key='Name')],
        [sg.Button('Settings'), sg.Button('Exit')],
    ]
    return sg.Window('Main', layout)

settings = {'Name':None}
window = main_window()
while True:
    event, values = window.read()
    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    elif event == 'Settings':
        flag = popup()
        if flag:
            window.close()
            window = main_window()
            """ or just update element
            name = settings['Name']
            value = name if name else 'None'
            window['Name'].update(value)
            """
window.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文