Python 将可扩展数据帧存储到变量中
我目前有下面的脚本,它读取导入的csv文件,并在Python 将可扩展数据帧存储到变量中,python,pandas,tkinter,Python,Pandas,Tkinter,我目前有下面的脚本,它读取导入的csv文件,并在tkinterGUI中显示为pandastable 导入文件时,它会添加2个附加列self.table.addColumn(“当前状态”)和self.table.addColumn(“分配技术人员”) 如何将更新后的pandastable dataframe存储到类TestApp(tk.Frame):之外的变量中,以便稍后在代码中调用dataframe上的其他函数 我以前使用过global变量,以便以后可以调用从其外部的函数中创建的变量,但不确定这
tkinter
GUI中显示为pandastable
导入文件时,它会添加2个附加列self.table.addColumn(“当前状态”)
和self.table.addColumn(“分配技术人员”)
如何将更新后的pandastable dataframe
存储到类TestApp(tk.Frame):
之外的变量中,以便稍后在代码中调用dataframe
上的其他函数
我以前使用过global
变量,以便以后可以调用从其外部的函数中创建的变量,但不确定这是否是我需要的
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()
这里不一定需要全局
变量。可以使用对象本身直接访问类的成员属性。因此,在本例中,您可以使用app.table
访问类TestApp
的表
attr,如下所示
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
之外对您的表执行相同的操作,然后通过\uuuu init\uuuu
传递它:
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
是一个可变对象,所有更改都将反映到访问该对象的任何函数中
一个建议是:最好将定义(类、函数)从运行时创建的有状态位中分离出来。这将大大有助于澄清依赖关系。通常使用文件底部的行来启动应用程序的“脚本”部分,保留上面的所有定义。我看到一些软件包(看着你,Flask!)稍微打破了这个惯例,它会引起头痛
注意,您的select\u input\u file
函数有一些问题。没有理由在那里创建应用程序实例。例如,一个更好的选择是在App
类中使用此方法。如果我使用df=App.table
,那么print(df)
我会得到输出。!第页。!table
哪一个是对象名?是的,这是表对象,如果要打印其中的数据帧,可以从df=app.table.model.df
获取,然后执行打印(df)