【发布时间】:2021-04-18 22:46:48
【问题描述】:
我正在创建一个 GUI,其中输入是 csv file,输出应该是 csv 输入文件中的表和表值中的图。用户应该能够选择输入文件和保存输出的位置。
到目前为止,我做到了:
#GUI CSV TO TABLE/PLOT PNG FILE
import PySimpleGUI as sg
import csv
#import pandas as pd
#import plotly.express as px
def table_example(filename, directory): #define the function that creates a table from an input file
if filename == '':
return
data = []
header_list = []
button = sg.popup_yes_no('Does this file have column names already?') #check if input file already has column names
if filename is not None:
with open(filename, "r") as infile:
reader = csv.reader(infile)
if button == 'Yes': #if yes, take the column names from the file
header_list = next(reader)
try:
data = list(reader)
if button == 'No': #if not, name the columns as column0,column1 etc.
header_list = ['column' + str(x) for x in range(len(data[0]))]
except:
sg.popup_error('Error reading file')
return
#create a plot
#df=pd.read_csv(filename)
#fig=px.line(df,x = header_list[1],y = header_list[2], title = 'Plot Example')
#fig.show() #FIX THIS PART TO GET THE PLOT
#create a window to show the table
layout = [[sg.Table(values=data,
headings=header_list,
max_col_width=25,
auto_size_columns=True,
justification='right',
num_rows=min(len(data), 20))]]
window = sg.Window('Table', layout, grab_anywhere=False)
event, values = window.read()
window.close()
#create a widow for choosing and input file and output folder
sg.theme('SystemDefault')
layout = [[sg.T("")], [sg.Text("Input File Path: ")], [sg.Input(), sg.FileBrowse()],
[sg.T("")], [sg.Text("Output File Path: ")], [sg.Input(),sg.FolderBrowse()],
[sg.T("")], [sg.Button('Begin!')]
]
window = sg.Window('File Browser', layout, size=(600,250))
while True:
event, values = window.read()
#end program if user closes window
if event == sg.WIN_CLOSED:
break
#start with function table_example() to create a table from an input is user clicks on Begin!
if event == "Begin!":
filename, directory = values[0], values[1] # 0 and 1 are keys of dictionary `values`
table_example(filename,directory) #take input data and create a table
window.close()
#TO DOS:
#create plot
#save table(left side) and plot(right side) in the file and in the output folder from users choice
现在的问题是我无法得到情节,也不知道我必须做什么。另外,如何获取保存在一个文件中的表和文件,该文件将保存在用户选择作为输出文件路径的目录中?
提前致谢
【问题讨论】:
标签: python plotly pysimplegui