直接从列表框和通过搜索框选择的列需要保存

发布于 2025-01-20 18:26:04 字数 2323 浏览 1 评论 0原文

在 PySimpleGUI 屏幕以及其他元素中,我有一个输入文本(搜索框)和列值列表框。我尝试为列表框添加搜索功能时遇到问题。 我刚刚开始尝试使用 PySimpleGUI,任何帮助都会很棒:-)

预期输出: 应该能够通过列表框上的搜索框提取选定的值,并从列表框中选择这些值

下面是我尝试过的代码:

form = sg.FlexForm('File split script')

layout = [
            [sg.Text('Please upload file & enter the remaining details and click "submit" button to execute the script')],
            [sg.Text('Input File', size=(10, 1), auto_size_text=False, justification='left')],
            [sg.InputText('Please upload either the CSV/XLSX input file'), sg.FileBrowse(key="-Input_Values-")],
            [sg.Text('Output File', size=(10, 1), auto_size_text=False, justification='left')],
            [sg.InputText('Please choose the output folder'), sg.FolderBrowse(key="-Output_Values-")],
            [sg.Text('No of records per file'), sg.InputText(key = '-no_of_records-'), sg.Text('in multiples of 1000')],
            [sg.Text(' ',size=(5, 1))],
            [sg.InputText('',key = '-Search_Box-', enable_events=True),sg.Button('Merge_Search_Fields_And_Listbox_Selected_Fields')],
            [sg.Text('Select the Required columns')],
            [sg.Listbox(values = (),select_mode= 'multiple', size=(30, 30), key='-ListBox-', enable_events=True),  sg.Button("Populate Columns"), sg.Text('Double click the button',font=('Arial', 13, 'bold')) ],
            [sg.Submit(), sg.Cancel()]
         ]

window = form.Layout(layout)
event, values = window.read()

while True:
    event, values = window.read()
    print(event, values)
    file_path = values[0]

    if event == sg.WIN_CLOSED or event == 'Cancel':
        break

    if event == 'Submit':
        break

    if event == 'Populate Columns':
        #extracts columns from selected input file - stores in 
        #column_names list and populates in ListBox

        column_names = list(df_full_content.columns)
        window.Element('-ListBox-').update(values= column_names)

    # My issue is with handling search box functionality
    if event == '-Search_Box-':
        text = values['-Search_Box-']
        if text in column_names:
            search_set.add(text)

        window.Element('-ListBox-').update(values=search_set)


上述代码的UI_Screenshot

In the PySimpleGUI screen along with other elements, I have an Input-text(Search box) and ListBox of column values. Facing an issue while I'm trying to add search functionality for List-Box.
I just started experimenting with PySimpleGUI, Any help would be great :-)

Expected Output: Should be able to pull the selected values through search box on list box and which were selected from Listbox

Below is the code which I have tried :

form = sg.FlexForm('File split script')

layout = [
            [sg.Text('Please upload file & enter the remaining details and click "submit" button to execute the script')],
            [sg.Text('Input File', size=(10, 1), auto_size_text=False, justification='left')],
            [sg.InputText('Please upload either the CSV/XLSX input file'), sg.FileBrowse(key="-Input_Values-")],
            [sg.Text('Output File', size=(10, 1), auto_size_text=False, justification='left')],
            [sg.InputText('Please choose the output folder'), sg.FolderBrowse(key="-Output_Values-")],
            [sg.Text('No of records per file'), sg.InputText(key = '-no_of_records-'), sg.Text('in multiples of 1000')],
            [sg.Text(' ',size=(5, 1))],
            [sg.InputText('',key = '-Search_Box-', enable_events=True),sg.Button('Merge_Search_Fields_And_Listbox_Selected_Fields')],
            [sg.Text('Select the Required columns')],
            [sg.Listbox(values = (),select_mode= 'multiple', size=(30, 30), key='-ListBox-', enable_events=True),  sg.Button("Populate Columns"), sg.Text('Double click the button',font=('Arial', 13, 'bold')) ],
            [sg.Submit(), sg.Cancel()]
         ]

window = form.Layout(layout)
event, values = window.read()

while True:
    event, values = window.read()
    print(event, values)
    file_path = values[0]

    if event == sg.WIN_CLOSED or event == 'Cancel':
        break

    if event == 'Submit':
        break

    if event == 'Populate Columns':
        #extracts columns from selected input file - stores in 
        #column_names list and populates in ListBox

        column_names = list(df_full_content.columns)
        window.Element('-ListBox-').update(values= column_names)

    # My issue is with handling search box functionality
    if event == '-Search_Box-':
        text = values['-Search_Box-']
        if text in column_names:
            search_set.add(text)

        window.Element('-ListBox-').update(values=search_set)


UI_Screenshot of above-mentioned code

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

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

发布评论

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

评论(1

别念他 2025-01-27 18:26:04

在这里选择列

  • 模式1的两种方法:单击列表框中的项目
  • 模式2:输入列中输入元素中的输入元素,带有输入键,

您不能在模式1中使用,然后在模式2中使用,然后使用模式1以进行以下代码。
要从模式2返回模式1,您可以单击reset按钮。

当然,您可以通过编程代码为其设置任何规则,但不能遵循它的代码,而不是本期的问题。

import PySimpleGUI as sg

items = [f'Item {i}' for i in range(10)]

search = set()
layout = [
    [sg.Text('Sepcified Column')],
    [sg.Input(size=10, key='SEARCH')],
    [sg.Listbox(items, size=(8, 5), select_mode=sg.LISTBOX_SELECT_MODE_MULTIPLE, key='LISTBOX')],
    [sg.Button('Submit'), sg.Button('Reset'), sg.Button('Exit')],
]
window = sg.Window('Title', layout, finalize=True)
window['SEARCH'].bind("<Return>", "+RETURN")
while True:

    event, values = window.read()

    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    print(event, values)
    if event == 'SEARCH+RETURN':
        item  = values['SEARCH']
        print(item)
        if item in items:
            search.add(item)
            data = sorted(list(search))
            index = data.index(item)
            window['LISTBOX'].update(values=data, set_to_index=[i for i in range(len(data))], scroll_to_index=index)
            window['SEARCH'].update('')

    elif event == 'Reset':
        search = set()
        window['LISTBOX'].update(values=items)

    elif event == 'Submit':
        selected = values['LISTBOX']
        print(selected)

window.close()
['Item 1', 'Item 2', 'Item 3']

Two ways here to select columns

  • Mode 1: Click item in Listbox element diretly
  • Mode 2: Input column name in Input element with enter key

You cannot work in Mode 1, then to Mode 2, then Mode 1 for following code.
To back to Mode 1 from Mode 2, you can click Reset Button.

Of course, you can set any rule for it by your programming code, but not for following code for it is not the question in this issue.

import PySimpleGUI as sg

items = [f'Item {i}' for i in range(10)]

search = set()
layout = [
    [sg.Text('Sepcified Column')],
    [sg.Input(size=10, key='SEARCH')],
    [sg.Listbox(items, size=(8, 5), select_mode=sg.LISTBOX_SELECT_MODE_MULTIPLE, key='LISTBOX')],
    [sg.Button('Submit'), sg.Button('Reset'), sg.Button('Exit')],
]
window = sg.Window('Title', layout, finalize=True)
window['SEARCH'].bind("<Return>", "+RETURN")
while True:

    event, values = window.read()

    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    print(event, values)
    if event == 'SEARCH+RETURN':
        item  = values['SEARCH']
        print(item)
        if item in items:
            search.add(item)
            data = sorted(list(search))
            index = data.index(item)
            window['LISTBOX'].update(values=data, set_to_index=[i for i in range(len(data))], scroll_to_index=index)
            window['SEARCH'].update('')

    elif event == 'Reset':
        search = set()
        window['LISTBOX'].update(values=items)

    elif event == 'Submit':
        selected = values['LISTBOX']
        print(selected)

window.close()
['Item 1', 'Item 2', 'Item 3']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文