简体   繁体   English

如何更改 PySimpleGUI 表的值?

[英]How can I change the values of a PySimpleGUI table?

I have been working with PySimpleGUI, where I'm trying to change from an initial empty table as presented in the figure 1, there can be observed how the initial table at the bottom is empty and has three columns.我一直在使用 PySimpleGUI,我试图从图 1 中所示的初始空表进行更改,可以观察到底部的初始表是如何为空的并且具有三列。

Figure 1:图1:

图1

In here I use a script where you imput your databases, and click on -GENERATE- to apply some statistcs and present it as an image on the rigth side and a table with statistical data on the bottom.在这里,我使用一个脚本来输入您的数据库,然后单击 -GENERATE- 以应用一些统计数据并将其显示为右侧的图像和底部带有统计数据的表格。

Here you can see the script:在这里你可以看到脚本:

Script (deleted irrelevant things):脚本(删除不相关的东西):

# TABLE DATA 

data= {
  ("Count", "-", "-"),
  ("Average", " -", "-"),
  ("Median", "-", "-"),
  ("Min", " -", "-"),
  ("Max", "-", "-"),
  ("StdDev", " -", "-"),
  ("Q1", "-", "-"),
  ("Q2", " -", "-"),
}

headings = ["STAT", "DATABASE A", "DATABASE B"] #list


# Table generation:
list_variables = ["Count", "Average", "Median", "Min", "Max", "StdDev", "Q1", "Q3"]
dicts = {}

def tablegen (imp_dict): #enter dictionary from -FOLDERS- 
        for k in imp_dict.items():
            del k[1]["survey"]
            v = [k[1].shape[0],np.average(k[1].iloc[:,0]) ,np.median(k[1].iloc[:,0]),min(k[1].iloc[:,0]),max(k[1].iloc[:,0]),np.std(k[1].iloc[:,0]),np.quantile(k[1].iloc[:,0],0.25),np.quantile(k[1].iloc[:,0],0.75)]
            final[k[0]]=v


#  LAYOUT

layout = [
        [sg.Button('GENERATE'),sg.Button('REMOVE')],
    
    [sg.Text('Generated table:')],
    [sg.Table(values=data, headings=headings, max_col_width=25, 
        auto_size_columns=True,
        display_row_numbers=False,
        justification='center',
        num_rows=5,
        alternating_row_color='lightblue',
        key='-TABLE-',
        selected_row_colors='red on yellow',
        enable_events=True,
        expand_x=False,
        expand_y=True,
        vertical_scroll_only=True,          
        tooltip='This is a table')]
]

window = sg.Window('Tool', layout)


# ------ Loops ------

while True:
    if event == 'GENERATE': #problems
        selection(file_list) #some functions blah blah, it generates a csv file called "minimum"

 #This is the archive (minimum.csv) that is produced after clicking -GENERATE- to make the desired table (it applies some functions).
        file_loc2 = (real_path + "/minimum.csv")

        try:
            archive = pd.read_csv(file_loc2, sep=",")
            df_names = pd.unique(archive["survey"]) #column names

            for name in df_names: #enter initial archive
                dicts[name] = pd.DataFrame(data= (archive.loc[archive["survey"] == name ]),columns=("Wavelength_(nm)", "survey")) #iteration blah blah
            tablegen(dicts) #this generates the statistical values for the table.
            final_df = pd.DataFrame(data= final, index=list_variables, columns=df_names)   
            final_df = final_df.round(decimals=1)
            final_lists = final_df.values.tolist()


# I tried using a DataFrame and produced the table in figure 2 (final_df), and a list of list (as recomended at PySimpleGUI webpage) final_lists and produced the table in figure 3.
            window["-TABLE-"].update(final_df) #or .update(final_lists)

        except Exception as E:
            print(f'** Error {E} **')
            pass        # if something weird happened making the full filename
  
window.close()

The issue is this:问题是这样的:

In the second and third figures present how this script uses the information from the folders (databases) selected in the left square, and generates the image in the left and supposedly would present DataFrame shown below.在第二张和第三张图中,展示了该脚本如何使用左侧方框中选择的文件夹(数据库)中的信息,并在左侧生成图像,并假定将呈现如下所示的 DataFrame。

GOAL TABLE TO PRESENT:目前的目标表:

final_df:
         13 MAR 2018  14 FEB 2018  16 FEB 2018  17 FEB 2018
Count           84.0         25.0         31.0         31.0
Average       2201.5       2202.1       2203.1       2202.9
Median        2201.0       2202.0       2204.0       2204.0
Min           2194.0       2197.0       2198.0       2198.0
Max           2208.0       2207.0       2209.0       2209.0
StdDev           4.0          3.0          3.5          3.5
Q1            2198.0       2199.0       2199.5       2199.5
Q3            2205.0       2205.0       2206.0       2206.0

Figure 2: This is using a DataFrame as imput in the loop -GENERATE-.图 2:这是使用 DataFrame 作为循环 -GENERATE- 中的输入。

在此处输入图像描述

Figure 3: This is using a list of lists as imput in the loop -GENERATE-.图 3:这是使用列表列表作为循环 -GENERATE- 中的输入。

在此处输入图像描述

As it is observed, the "-TABLE-" is not working the way that I intend.正如所观察到的那样,“-TABLE-”没有按照我想要的方式工作。 If I use a DataFrame it is just selecting the columns names, and if I use a list of list it ignores the index and column names from the intended goal table.如果我使用 DataFrame,它只是选择列名,如果我使用列表列表,它会忽略预期目标表中的索引和列名。

Also, the table is in both cases not generating more columns even there should be 5 including the index.此外,该表在这两种情况下都不会生成更多列,即使应该有 5 个包括索引。 And how can I change the column names from the initially provided ones?以及如何更改最初提供的列名?

In the PySimpleGUI demos and call references I can not find something to solve this, also I searched in the web and in older StackOverflow posts, but to be honest I do not find a similar case to this.在 PySimpleGUI 演示和调用参考中,我找不到解决这个问题的方法,我也在网上和旧的 StackOverflow 帖子中进行了搜索,但老实说,我没有找到与此类似的案例。

I'll be really greatful if somebody can help me to find what I am doing wrong!如果有人可以帮助我找出我做错了什么,我将非常感激!

Btw sorry for my english, Colombian here.顺便说一句,对不起我的英语,这里是哥伦比亚语。

The number of columns increased per records ?每条记录增加的列数? Most of time, the number of rows of a Table element increased per records.大多数情况下,每条记录的 Table 元素的行数都会增加。 Maybe you should take ['Date'] + list_variables as the headings of the Table element, and add each holder as one new row of the Table element.也许您应该将['Date'] + list_variables作为 Table 元素的标题,并将每个持有者添加为 Table 元素的一个新行。

import pandas as pd
import PySimpleGUI as sg

file = r'd:/Qtable.csv'
"""
Date,Count,Average,Median,Min,Max,StdDev,Q1,Q3
13-Mar-18,84,2201.5,2201,2194,2208,4,2198,2205
14-Mar-18,25,2202.1,2202,2197,2207,3,2199,2205
16-Mar-18,31,2203.1,2204,2198,2209,3.5,2199.5,2206
17-Mar-18,31,2202.9,2204,2198,2209,3.5,2199.5,2206
"""

df       = pd.read_csv(file, header=None)
values   = df.values.tolist()
headings = values[0]
data     = values[1:]

layout = [[sg.Table(data, headings=headings, key='-TABLE-')]]
sg.Window('Table', layout).read(close=True)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM