简体   繁体   English

与 ListBox 同时使用 InputText | PySimpleGUI | Python

[英]Using a InputText at same time as ListBox | PySimpleGUI | Python

Click here to see my problem ,点击这里查看我的问题

1: I want to see the filtered list after typing in the InputText immediately, for some reason it only updates on the second character 1:我想在输入 InputText 后立即查看过滤后的列表,由于某种原因,它只更新第二个字符

2: I want to be able to use the search box even after selecting an option in the list 2:即使在列表中选择了一个选项后,我也希望能够使用搜索框

Here's a snippet of my code:这是我的代码片段:

if event == '-search-term-':
    SearchTerm = values['-search-term-']
    SearchTerm.replace("['", "")
    SearchTerm.replace("']", "")
  
    if values[event]:
    
      for i in alllist:
        if SearchTerm in i:
          if i not in matchlist:
            matchlist.append(i)
            outlist = sorted(matchlist)
            window.Element('-scr-lib-list-').Update(values=outlist)
              
        if SearchTerm not in i:
          if i in matchlist:
            matchlist.remove(i)
            outlist = matchlist
            window.Element('-scr-lib-list-').Update(values=outlist)
          
          outlist = sorted(matchlist)
          window.Element('-scr-lib-list-').Update(values=outlist)
          
    else:
      out = sorted(alllist)
      window.Element('-scr-lib-list-').Update(values=out)

Here's the demo code to update the values of a Listbox element under event from a Input element.下面是从 Input 元素更新事件下Listbox元素值的演示代码。 It filter the items in list by the content of the Input element.它通过 Input 元素的内容过滤列表中的项目。

import PySimpleGUI as sg

font = ('Courier New', 11)
sg.set_options(font=font)
sg.theme('DarkBlue4')

all_files = [
    '01 Whetting Your Appetite.pptx',
    '02 Using the Python Interpreter.pptx',
    '03 An Informal Introduction to Python.pptx',
    '04 More Control Flow Tools.pptx',
    '05 Data Structures.pptx',
    '06 Modules.pptx',
    '07 Input and Output.pptx',
    '08 Errors and Exceptions.pptx',
    '09 Classes.pptx',
    '10 Brief Tour of the Standard Library.pptx',
    '11 Virtual Environments and Packages.pptx',
    '12 What Now.pptx',
    '13 Interactive Input Editing and History Substitution.pptx',
    '14 Representation Error.日心pptx',
]

width = max(map(len, all_files))

layout = [
    [sg.Input('', expand_x=True, enable_events=True, key='-INPUT-')],
    [sg.Listbox(all_files, size=(width, 7), enable_events=True, key='-LISTBOX-')],
]
window = sg.Window('File Filter', layout, finalize=True)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    elif event == '-INPUT-':
        string = values['-INPUT-'].lower()
        if string:
            files = [file for file in all_files if string in file.lower()]
        else:
            files = all_files[:]
        window['-LISTBOX-'].update(files)
    elif event == '-LISTBOX-':
        filename = values['-LISTBOX-'][0]
        print(filename)

window.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM