繁体   English   中英

将pandastable数据帧存储到变量中

[英]Store pandastable dataframe into variable

目前,我有以下脚本读取导入CSV文件,显示为pandastabletkinter GUI。

当文件被导入时,它会添加 x2 个额外的列self.table.addColumn("Current Status")self.table.addColumn("Assign Technician")

如何将更新的pandastable dataframe存储到class TestApp(tk.Frame):之外的变量中,以便稍后在我的代码中调用dataframe上的其他函数?

我之前使用过global变量,以便稍后可以调用从它外部的函数内部创建的variable ,但不确定这是否是我为此目的所需要的。

import csv
import tkinter as tk
import tkinter.ttk as tkrttk
from tkinter import *
from tkinter import filedialog

import pandas as pd
from pandastable import Table, TableModel



root = tk.Tk()
root.geometry("2000x1000")
root.title('Workshop Manager')


def select_input_file():
    global input_file_path
    input_file_path = filedialog.askopenfilename(
    filetypes=(("CSV files", "*.csv"),))
    app = TestApp(root, input_file_path)
    app.place(bordermode = INSIDE,height = 500, width = 2000, x =0, y=50)
    

class TestApp(tk.Frame):
     def __init__(self, parent, input_file_path, editable = True, enable_menus = True):
        super().__init__(parent)
        self.table = Table(self, showtoolbar=False, showstatusbar=False)
        self.table.importCSV(input_file_path)
        self.table.show(input_file_path)
        self.table.addColumn('Current Status')
        self.table.addColumn('Assign Technician')
        self.table.autoResizeColumns()

root.mainloop()

您在这里不一定需要global变量。 您可以通过使用对象本身直接访问类的成员属性。 所以在这种情况下,您可以使用app.table访问TestApp类的table属性,它看起来像这样,

def select_input_file():
    #...
    app = TestApp(root, input_file_path)
    app.place(bordermode = INSIDE,height = 500, width = 2000, x =0, y=50)

    df = app.table     # contains the updated table which can be passed to other functions
    newFunc( df )      # sample call

避免使用global来实现这一点。

目前,您的所有有状态变量都存在于模块(文件)中。 您可以在TestApp之外对您的表执行相同的操作,然后通过__init__传递它:

import csv
import tkinter as tk
import tkinter.ttk as tkrttk
from tkinter import *
from tkinter import filedialog

import pandas as pd
from pandastable import Table, TableModel


table = Table(showtoolbar=False, showstatusbar=False)

root = tk.Tk()
root.geometry("2000x1000")
root.title('Workshop Manager')


def select_input_file():
    global input_file_path
    input_file_path = filedialog.askopenfilename(
    filetypes=(("CSV files", "*.csv"),))
    app = TestApp(root, input_file_path)
    app.place(bordermode = INSIDE,height = 500, width = 2000, x =0, y=50)
    

class TestApp(tk.Frame):
     def __init__(self, parent, input_file_path, editable = True, enable_menus = True, table=table):
        super().__init__(parent)
        self.table = table
        self.table.importCSV(input_file_path)
        self.table.show(input_file_path)
        self.table.addColumn('Current Status')
        self.table.addColumn('Assign Technician')
        self.table.autoResizeColumns()

root.mainloop()

任何可以看到模块命名空间的对象现在都可以访问您的表对象。 这里table是一个可变对象,所有更改都将反映到访问该对象的任何函数。

一个建议:最好将您的定义(类、函数)与在运行时创建的有状态位分开。 这将极大地帮助澄清依赖关系。 通常使用if __name__ == "__main__":在文件底部启动应用程序的“脚本”部分,保留上面的所有定义。 我已经看到一些包(看着你,Flask!)有点违反这个约定,它可能会引起头痛。

select_input_file ,您的select_input_file函数存在一些问题。 没有充分的理由在那里创建应用程序实例。 例如,更好的选择是将其作为App类中的一个方法。

暂无
暂无

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

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