Python Forum

Full Version: PySimpleGUI FilesBrowse enable event to update value in combo
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

I wanted to update combo value dynamically, when browse file.The executed procedure as below:

1)click Browse button, select what you want file.
2)Combo control in GUI update item automatically.

The problem is how to enable FileBrowse event.

Any comment will highly appreciate!!

import PySimpleGUI as psg
import pandas as pd
import os


working_directory = os.getcwd()

TestItem=[]

def read_file(csv_path):

    df=pd.read_csv(csv_path,header=1).drop([0,1])
    allitems=df.columns.tolist()
    for item in allitems:
        if item.startswith(tuple(['DUT','Ref'])):
          TestItem.append(item)
    return TestItem

psg.theme('SandyBeach')
# define layout
layout = [

    [sg.Text(""),sg.Text('Selected File')],
    [sg.Text(""),sg.InputText(key="-FILE_PATH-",size=(120, 20)),
     sg.FilesBrowse(initial_folder=working_directory,file_types=[("CSV Files","*.csv")],enable_events=True,key='-sel-')],
    [sg.Combo(TestItem,key='-newitem-')],
    [sg.Text("",size=(1,0)),sg.OK('Check Master',key='-OK-'),sg.Exit(size=(5,0))]]

window=psg.Window('Update Combo', layout)

while True:
    event,value=window.read()
    if event in (sg.WIN_CLOSED,'Exit'):
        break
    elif event=='-sel-':
        csv_add = value["-FILE_PATH-"]
        Item = read_file(csv_add)
        window['-newitem-'].update(Item)
    elif event=='-OK-':
        sg.popup("Demo")

window.close()
This generates an event for the browse button.
import PySimpleGUI as sg

layout = [[sg.FileBrowse(enable_events=True, key="-FILE-")]]
window = sg.Window("My window", layout)
while True:
    event, values = window.read()
    if event is None or event == "Cancel":
        break
    elif event == "-FILE-":
        print(values["-FILE-"])
window.close()
This does not:
import PySimpleGUI as sg

layout = [[sg.InputText(size=(60, 1)), sg.FileBrowse(enable_events=True, key="-FILE-")]]
window = sg.Window("My window", layout)
while True:
    event, values = window.read()
    if event is None or event == "Cancel":
        break
    elif event == "-FILE-":
        print(values["-FILE-"])
window.close()
What is the difference between them? The obvious difference is the layout in the second example includes an InputText widget. The more important difference is that in the second example the target for the FileBrowse button is the InputText widget, not the FileBrowse button.

But what to do if you want to be notified when a file is selected? One solution is to set the target of the FileBrowse button to be the FileBrowse button. Selecting a file will now generate an event for the button, but it no longer updates the InputText. To do that, add code to the event manager to update the InputText value.
import PySimpleGUI as sg

layout = [
    [
        sg.InputText(size=(60, 1), key="-EDIT-"),
        sg.FileBrowse(enable_events=True, key="-FILE-", target=(0, 1)),
    ]
]
window = sg.Window("My window", layout)
while True:
    event, values = window.read()
    if event is None or event == "Cancel":
        break
    elif event == "-FILE-":
        print(values["-FILE-"])
        window["-EDIT-"].update(value=values["-FILE-"])
window.close()
Another solution is to catch events for the InputText widget. Set the FileBrowse target back to the InputText widget and enable events for the InputText widget. When you select a file, the InputText changes, generating an event. In this example I disable typing in the InputText widget so the only way to generate an event is to use the FileBrowse button.
import PySimpleGUI as sg

layout = [
    [
        sg.InputText(size=(60, 1), disabled=True, enable_events=True, key="-FILE-"),
        sg.FileBrowse(),
    ]
]
window = sg.Window("My window", layout)
while True:
    event, values = window.read()
    print(event, values)
    if event is None or event == "Cancel":
        break
    elif event == "-FILE-":
        print(values["-FILE-"])
window.close()
Great, it is very useful. I appreciate for you to do that.