咨询PySimpleGUI中实现位置层级嵌套下拉菜单的可行方案
Does PySimpleGUI Support Freely Placed Nested Dropdowns?
PySimpleGUI doesn’t have a built-in, freely placeable nested dropdown component. The standard Menu is fixed to the window top, and Combo can’t natively render hierarchical structures (it flattens all entries into a single list). Below are practical workarounds to implement your location hierarchy.
Solution 1: Cascading PopupMenus (Click-to-Reveal Nested Options)
Use buttons that trigger popup_menu with nested entries. This mimics a dropdown structure that can be placed anywhere in your layout.
import PySimpleGUI as sg layout = [ [sg.Button('Select Location', key='-LOCATION_BTN-')] ] window = sg.Window('Nested Location Selector', layout) while True: event, values = window.read() if event == sg.WIN_CLOSED: break elif event == '-LOCATION_BTN-': # Define your hierarchical menu structure location_menu = [ ['Rack 1', ['Shelf 1', ['Bin A', 'Bin B', 'Bin C', 'Bin D', 'Bin E']], ['Shelf 2', ['Bin F', 'Bin G']], 'Shelf 3' ], ['Rack 2', ['Shelf 1', ['Bin H']]] ] # Show menu at the button's screen position button_pos = window['-LOCATION_BTN-'].get_screen_rect()[0:2] selected = sg.popup_menu(location_menu, title='Pick a Location', location=button_pos) if selected: sg.popup(f'Selected: {selected}') window.close()
Solution 2: Linked Cascading Combos (Step-by-Step Selection)
Use a series of Combo components that update dynamically based on previous selections. This breaks the hierarchy into sequential, dependent dropdowns.
import PySimpleGUI as sg # Define your hierarchical location data location_data = { 'Main Location': { 'Rack 1': { 'Shelf 1': ['Bin A', 'Bin B', 'Bin C'], 'Shelf 2': ['Bin D', 'Bin E'] }, 'Rack 2': { 'Shelf 1': ['Bin F'] } }, 'Secondary Location': { 'Rack 3': { 'Shelf 3': ['Bin G'] } } } layout = [ [sg.Text('Location:'), sg.Combo(list(location_data.keys()), key='-LOC-', enable_events=True)], [sg.Text('Rack:'), sg.Combo([], key='-RACK-', enable_events=True, disabled=True)], [sg.Text('Shelf:'), sg.Combo([], key='-SHELF-', enable_events=True, disabled=True)], [sg.Text('Bin:'), sg.Combo([], key='-BIN-', disabled=True)] ] window = sg.Window('Linked Cascading Combos', layout) while True: event, values = window.read() if event == sg.WIN_CLOSED: break # Update rack options when location is selected elif event == '-LOC-': selected_loc = values['-LOC-'] window['-RACK-'].update(values=list(location_data[selected_loc].keys()), disabled=False) window['-SHELF-'].update(values=[], disabled=True) window['-BIN-'].update(values=[], disabled=True) # Update shelf options when rack is selected elif event == '-RACK-': selected_loc = values['-LOC-'] selected_rack = values['-RACK-'] window['-SHELF-'].update(values=list(location_data[selected_loc][selected_rack].keys()), disabled=False) window['-BIN-'].update(values=[], disabled=True) # Update bin options when shelf is selected elif event == '-SHELF-': selected_loc = values['-LOC-'] selected_rack = values['-RACK-'] selected_shelf = values['-SHELF-'] window['-BIN-'].update(values=location_data[selected_loc][selected_rack][selected_shelf], disabled=False) window.close()
Solution 3: TreeView Component (Visual Hierarchy Display)
Use TreeView to render the entire hierarchy in a collapsible, visual format. Users can expand nodes and select any level of the location structure.
import PySimpleGUI as sg # Build tree structure from your location data tree_data = sg.TreeData() # Add root location nodes tree_data.Insert('', 'loc1', 'Main Location', []) tree_data.Insert('', 'loc2', 'Secondary Location', []) # Add racks and shelves under Main Location tree_data.Insert('loc1', 'rack1', 'Rack 1', []) tree_data.Insert('rack1', 'shelf1', 'Shelf 1', []) # Add bins to Shelf 1 for bin_name in ['Bin A', 'Bin B', 'Bin C', 'Bin D', 'Bin E']: tree_data.Insert('shelf1', bin_name.lower(), bin_name, []) tree_data.Insert('rack1', 'shelf2', 'Shelf 2', []) tree_data.Insert('loc1', 'rack2', 'Rack 2', []) # Add nodes for Secondary Location tree_data.Insert('loc2', 'rack3', 'Rack 3', []) layout = [ [sg.Tree(tree_data, headings=[''], auto_size_columns=True, num_rows=10, key='-TREE-', enable_events=True)], [sg.Text('Selected Item:'), sg.Text('', key='-SELECTED-')] ] window = sg.Window('TreeView Location Hierarchy', layout) while True: event, values = window.read() if event == sg.WIN_CLOSED: break elif event == '-TREE-': selected_item = values['-TREE-'][0] if values['-TREE-'] else '' window['-SELECTED-'].update(selected_item) window.close()
内容的提问来源于stack exchange,提问作者Robert Munroe

