【问题标题】:GUI with input csv and output table and plot png file带有输入 csv 和输出表以及绘图 png 文件的 GUI
【发布时间】: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


    【解决方案1】:

    窗口高度太小无法显示按钮Beginn!,设置为size=(600, 300)

    没有为 sg.Input 设置密钥,因此您的 sg.Input 的密钥将是 0 和 1。 您可以通过

    获取事件循环中的文件名和目录
    if event == "Beginn!":
        filename, directory = values[0], values[1]    # 0 and 1 are keys of dictionary `values`
        table_example(filename, directory)
    

    也许您可以查看演示程序如何绘制 png 文件,可能是 PIL Image、ImageDraw 或 Matplotlib 或其他。

    https://github.com/PySimpleGUI/PySimpleGUI/tree/master/DemoPrograms

    【讨论】:

    • 谢谢,我更改了代码并获得了表格,但绘图和将两者保存在一个文件中仍然存在问题。你能帮忙吗?
    • 图形绘制完成后,可以调用matplotlib.pyplot.savefig将图形保存为png matplotlib.org/stable/api/_as_gen/…
    猜你喜欢
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    相关资源
    最近更新 更多