使用 PySimpleGUI 搜索 pandas 数据帧

searching a pandas dataframe using PySimpleGUI

提问人:mw00847 提问时间:11/1/2022 更新时间:11/1/2022 访问量:53

问:

如何使用 pysimple gui 更新我的列表框,如下所示

所以用户输入是 AP,列表框建议“APPLE”

我看到的问题是在用户输入 pandas 数据帧时描述更新的列表框

import PySimpleGUI as sg 
import pandas as pd 

sg.theme('Default1')

pd.set_option('display.max_rows',None)

formulation =pd.read_csv('FORMULATIONS.csv')

names=formulation["PRODUCT"]



#names=product.values.tolist()



#names=formulation["PRODUCT"].to_string

#left side search 

left_col=[[sg.Text('SEARCH')],
       [sg.Input(size=(20,1),enable_events=True,key='-INPUT-',do_not_clear=True)],
       [sg.Listbox(names,size=(50,len(names)),key='-LIST-',enable_events=True)]]

#right side batch sheet

right_col=[[sg.Text('Product : \n \n ITEM | RAW MATERIAL |')],
        [sg.Text(size=(40,1),key='-TOUT-')]]



#together 

layout=[[sg.Column(left_col,element_justification='c'),sg.VSeperator(),sg.Column(right_col)]]

#create window 

window =sg.Window('BF-2.1',layout,resizable=True)


#event loop

while True:
    event, values =window.Read()
    if event in (sg.WIN_CLOSED,'Exit'):
        break
    if values['-INPUT-'] != '' :
        search =values['-INPUT-']
        new_values=[formulation["PRODUCT"]==['-INPUT-']]  #how to use the input to navigate
        window.Element('-LIST-').Update(new_values)
    else:
        window.Element('-LIST-').Update(names)
    if event =='-LIST-' and len(values['-LIST-']):
        sg.popup('Selected',values['-LIST-'])

window.close()  

试过了

new_values=[如果在 x 中搜索,则名称中的 x 表示 x]

熊猫 pysimplegui

评论


答:

0赞 Jason Yang 11/1/2022 #1

用于获取与 .df.loc[df["PRODUCT"]==text]["KIND"]text

以下代码演示了如何从 Input 元素获取事件以筛选 DataFrame 以更新 List 元素。尝试输入 'car'、'motorcycleship' 以更新 List 元素,或清空 List 元素以获取所有元素。or

import pandas as pd
import PySimpleGUI as sg

data = [
    ["car", item] for item in ('SUV', 'Hatchback', 'Crossover', 'Convertible', 'Sedan', 'Sports Car', 'Coupe', 'Minivan', 'Station Wagon', 'Pichup Truck')] + [
    ["motorcycle", item] for item in ('standard', 'cruiser', 'touring', 'sports', 'off-road', 'dual-purpose')] + [
    ["ship", item] for item in ('Container', 'Bulk Carrier', 'Tanker', 'Passenger', 'Naval', 'Offshore', 'Special Purpose')]
df = pd.DataFrame(data, columns=["PRODUCT", "KIND"])

left_col=[
    [sg.Text('SEARCH')],
    [sg.Input(size=20, enable_events=True, key='-INPUT-')],
    [sg.Listbox(df["KIND"], size=(50, 10), key='-LIST-', enable_events=True)],
]
right_col=[
    [sg.Text('Product : \n \n ITEM | RAW MATERIAL |')],
    [sg.Text(size=40, key='-TOUT-')],
]
layout=[
    [sg.Column(left_col,element_justification='c'),
     sg.VSeperator(),
     sg.Column(right_col)],
]
window = sg.Window('BF-2.1', layout, resizable=True)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    if event == '-INPUT-':
        if values[event]:
            text = values['-INPUT-'].lower()
            new_values= df.loc[df["PRODUCT"]==text]["KIND"]
            window['-LIST-'].update(new_values)
        else:
            new_values = df["KIND"]
            window['-LIST-'].update(new_values)
    if event =='-LIST-' and len(values['-LIST-']):
        sg.popup('Selected', values['-LIST-'])

window.close()