Python Forum
PySimpleGUI FilesBrowse enable event to update value in combo
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PySimpleGUI FilesBrowse enable event to update value in combo
#1
Photo 
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()
Reply
#2
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()
Reply
#3
Great, it is very useful. I appreciate for you to do that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PysimpleGUI window update dynamically SamLiu 6 4,062 Apr-05-2023, 02:32 PM
Last Post: SamLiu
Exclamation Update Point Coordinates from Mouse Event in a Plot Jguillerm 2 1,299 Jan-10-2023, 07:53 AM
Last Post: Jguillerm
  How to update the list of a combo box in a QTableView panoss 10 6,272 Feb-05-2022, 03:24 PM
Last Post: panoss
  Radio butto to enable/disable combo box in Tkinter cybertooth 5 5,562 Oct-09-2021, 07:30 AM
Last Post: cybertooth
  Referencing Combo Box MC2020 6 2,962 Feb-12-2020, 10:17 PM
Last Post: woooee
  [PySimpleGUI]How to insert values that were gotten from FilesBrowse into ListBox? trigchen 0 2,887 Dec-30-2019, 06:58 AM
Last Post: trigchen
  Need help setting up a PySimpleGUI progress bar that I can maually update in script Soundtechscott 1 10,120 Jun-10-2019, 06:14 AM
Last Post: Soundtechscott
  [PySimpleGUI] update listbox not working zappfinger 2 12,108 Nov-12-2018, 08:33 PM
Last Post: zappfinger
  Need help adding a sql List to a combo box PyQt5 jimmyvegas29 1 8,631 Jul-20-2018, 07:28 AM
Last Post: Alfalfa
  [Tkinter] disable/enable button | stopwatch proj robertofreemano 1 6,312 Jul-18-2018, 09:52 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020