【问题标题】:File browser GUI to read the filepath文件浏览器 GUI 读取文件路径
【发布时间】:2021-11-28 03:06:54
【问题描述】:

我必须通过从 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())

请帮我解决一下。

【问题讨论】:

  • [sg.Text("Choose a file: "), sg.Input(key="-IN-"), sg.FileBrowse()],,点击按钮filename = values['-IN-']后得到Submit,关闭窗口后,得到ds = pd.read_csv(filename)
  • 我已经尝试过了,但是显示未定义值的错误
  • 完整的错误信息是什么?
  • NameError: name 'values' is not defined
  • 可能与您发布的代码不同?

标签: python csv matplotlib pysimplegui


【解决方案1】:

获取文件名的示例代码,也可以直接调用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)

【讨论】:

    猜你喜欢
    • 2011-03-13
    • 2020-12-31
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    相关资源
    最近更新 更多