Home > Enterprise >  File browser GUI to read the filepath
File browser GUI to read the filepath

Time:10-09

I have to plot a graph using matplotlib by reading a CSV file from pandas. Furthermore, I need to enter the file path outside the python code instead of entering inside the pd.read_csv('file path). So for that to happen, I need to create a GUI using the PySimpleGUI module. But I'm stuck in the middle of not taking the filename inside the program. The full code is given below:

import PySimpleGUI as sg
sg.theme("DarkTeal2")
layout = [[sg.T("")], [sg.Text("Choose a file: "), sg.Input(), sg.FileBrowse(key="-IN-")],[sg.Button("Submit")]]

###Building Window
window = sg.Window('My File Browser', layout, size=(600,150))
    
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event=="Exit":
        break
    elif event == "Submit":
        print(values["-IN-"])

This is the code for the dialogue box

The code for my plotting is as follows :

import pandas as pd
from matplotlib import pyplot as plt
#Reading the CSV file
ds = pd.read_csv(r"filename.csv")

#Input of required value of time
start_row = int(input('Please enter starting time(in ms): '))
end_row =int(input('Please enter ending time(in ms): '))

if start_row>end_row or start_row==end_row :
    print("Please Enter the end time greater than the start time!")


else:
#Plotting of the graph
    print(plt.plot(ds.iloc[start_row:(end_row 1)]))
    plt.xlabel('Milliseconds')
    plt.ylabel('TCMD')
    plt.grid()
    plt.savefig(r"path")    #To save the plot as a jpeg file
    plt.show()
    ds.describe()

#Detailed insights on the data
print(ds.iloc[start_row:(end_row 1)].describe())

Please help me out with a solution.

CodePudding user response:

Example code to get filename, or you can call filename = sg.popup_get_file("Choose a file: ", file_types=(("ALL CSV Files", "*.csv"), ("ALL Files", "*.*"), )) directly.

import PySimpleGUI as sg


sg.theme("DarkTeal2")

layout = [
    [sg.T("")],
    [sg.Text("Choose a file: "),
     sg.Input(key="-IN-"),
     sg.FileBrowse(file_types=(("ALL CSV Files", "*.csv"), ("ALL Files", "*.*"), ))],
    [sg.Button("Submit")],
]

window = sg.Window('My File Browser', layout, size=(600,150))

filename = ""
while True:
    event, values = window.read()
    if event in (sg.WIN_CLOSED, "Exit"):
        break
    elif event == "Submit":
        filename = values['-IN-']
        break

window.close()
print(filename)
  • Related