It's my first try at a pysimplegui application. Before I was using tkinter a lot but interface doesn't look that pretty and modern looking.
What I want to do is to have a window with 3 button, 2 to select different files and one to launch comparison function. After selecting the file I just want to interface to display the filename. If I add a sg.Text() it displays the full filepath which I don't want to because I only need to display the filename to the user.
Here is the minimal code to reproduce what I experience :
import PySimpleGUI as sg
import os
# layout = [
# [sg.InputText(size=(45, 1), key="File_1", disabled=True), sg.FileBrowse(enable_events=True, key="File_path_1", target=(0, 1))],
# [sg.InputText(size=(45, 1), key="File_2", disabled=True), sg.FileBrowse(enable_events=True, key="File_path_2", target=(1, 1))],
# [sg.Submit()]
# ]
layout = [
[sg.Text('Old_file '), sg.InputText(size=(45, 1), key="File_1", disabled=True), sg.FileBrowse(enable_events=True, key="File_path_1", target=(0, 1))],
[sg.Text('New_file'), sg.InputText(size=(45, 1), key="File_2", disabled=True), sg.FileBrowse(enable_events=True, key="File_path_2", target=(1, 1))],
[sg.Submit()]
]
window = sg.Window("File compare", layout)
while True:
event, values = window.read()
if event is None or event == "Cancel":
break
elif event == "File_path_1":
filepath = values.get('File_path_1')
window['File_1'].update(os.path.basename(filepath))
elif event == "File_path_2":
filepath = values.get('File_path_2')
window['File_2'].update(os.path.basename(filepath))
elif event == 'Submit':
print("hello")
Here is the picture of what I get with text-label in front of it in layout :
My question is how to make it only display the filename even with the text-label in front of the filename text cell. When I add a sg.Text() element before the sg.InputText() it shows the filepath instead of the filename like I wanted initially. I thought that the key would allow to update only some part of the UI but it doesn't work that way when a sg.Text() elem is before the InputText that I want to update with the filename and NOT the filepath.
Edit : I used PySimpleGUI-4-foss==4.60.4.1 package instead of PysimpleGUI v5.0.3 Thanks Mimo for the quick answer.
really sorry for the issue not sure why first time it worked. this is an updated version.
import PySimpleGUI as sg
import os
layout = [
[sg.Text('Old_file '), sg.Input(size=(45, 1), key="File_1", readonly=True), sg.FileBrowse(target='Hidden_Browse_1', key='Browse_1', enable_events=True), sg.In(visible=False, enable_events=True, key='Hidden_Browse_1')],
[sg.Text('New_file'), sg.Input(size=(45, 1), key="File_2", readonly=True), sg.FileBrowse(target='Hidden_Browse_2', key='Browse_2', enable_events=True), sg.In(visible=False, enable_events=True, key='Hidden_Browse_2')],
[sg.Submit()]
]
window = sg.Window("File compare", layout)
while True:
event, values = window.read()
if event is None or event == "Cancel":
break
elif event == "Hidden_Browse_1":
filepath = values.get('Hidden_Browse_1')
window['File_1'].update(os.path.basename(filepath))
elif event == "Hidden_Browse_2":
filepath = values.get('Hidden_Browse_2')
window['File_2'].update(os.path.basename(filepath))
elif event == 'Submit':
print("hello")
I apologize for the confusion. The sg.popup_get_file()
function opens a file dialog, which is why you're seeing two file dialogs when you click the Browse button.
To fix this, you can use the sg.FileBrowse
button with the target
parameter set to a hidden sg.Input
field. Then, in your event loop, you can update the visible sg.Input
field with the filename.
Here's how you can do it:
import PySimpleGUI as sg
import os
layout = [
[sg.Text('Old_file '), sg.Input(size=(45, 1), key="File_1", readonly=True), sg.FileBrowse(target='Hidden_Browse_1', key='Browse_1', enable_events=True), sg.In(visible=False, enable_events=True, key='Hidden_Browse_1')],
[sg.Text('New_file'), sg.Input(size=(45, 1), key="File_2", readonly=True), sg.FileBrowse(target='Hidden_Browse_2', key='Browse_2', enable_events=True), sg.In(visible=False, enable_events=True, key='Hidden_Browse_2')],
[sg.Submit()]
]
window = sg.Window("File compare", layout)
while True:
event, values = window.read()
if event is None or event == "Cancel":
break
elif event == "Hidden_Browse_1":
filepath = values.get('Hidden_Browse_1')
window['File_1'].update(os.path.basename(filepath))
elif event == "Hidden_Browse_2":
filepath = values.get('Hidden_Browse_2')
window['File_2'].update(os.path.basename(filepath))
elif event == 'Submit':
print("hello")
the sg.FileBrowse
buttons target hidden sg.Input
fields. When a file is selected, the hidden sg.Input
field is updated with the full file path and an event is triggered. Then, in your event loop, you can retrieve the file path from the hidden sg.Input
field, get the filename using os.path.basename()
, and update the visible sg.Input
field with the filename.