繁体   English   中英

如何更改 PySimpleGUI 表的值?

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

我一直在使用 PySimpleGUI,我试图从图 1 中所示的初始空表进行更改,可以观察到底部的初始表是如何为空的并且具有三列。

图1:

图1

在这里,我使用一个脚本来输入您的数据库,然后单击 -GENERATE- 以应用一些统计数据并将其显示为右侧的图像和底部带有统计数据的表格。

在这里你可以看到脚本:

脚本(删除不相关的东西):

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

问题是这样的:

在第二张和第三张图中,展示了该脚本如何使用左侧方框中选择的文件夹(数据库)中的信息,并在左侧生成图像,并假定将呈现如下所示的 DataFrame。

目前的目标表:

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

图 2:这是使用 DataFrame 作为循环 -GENERATE- 中的输入。

在此处输入图像描述

图 3:这是使用列表列表作为循环 -GENERATE- 中的输入。

在此处输入图像描述

正如所观察到的那样,“-TABLE-”没有按照我想要的方式工作。 如果我使用 DataFrame,它只是选择列名,如果我使用列表列表,它会忽略预期目标表中的索引和列名。

此外,该表在这两种情况下都不会生成更多列,即使应该有 5 个包括索引。 以及如何更改最初提供的列名?

在 PySimpleGUI 演示和调用参考中,我找不到解决这个问题的方法,我也在网上和旧的 StackOverflow 帖子中进行了搜索,但老实说,我没有找到与此类似的案例。

如果有人可以帮助我找出我做错了什么,我将非常感激!

顺便说一句,对不起我的英语,这里是哥伦比亚语。

每条记录增加的列数? 大多数情况下,每条记录的 Table 元素的行数都会增加。 也许您应该将['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