用于读取文件路径的文件浏览器 GUI

File browser GUI to read the filepath

我必须通过从 pandas 读取 CSV 文件来使用 matplotlib 绘制图表。此外,我需要在 python 代码之外输入文件路径,而不是在 pd.read_csv('file path) 内部输入。因此,为此,我需要使用 PySimpleGUI 模块创建一个 GUI。但是我被困在不在程序中获取文件名的过程中。完整代码如下:

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-"])

这是对话框的代码

我的绘图代码如下:

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())

请帮我想出解决办法。

获取文件名的示例代码,或者您可以直接调用filename = sg.popup_get_file("Choose a file: ", file_types=(("ALL CSV Files", "*.csv"), ("ALL Files", "*.*"), ))

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)