从 Panda 获取 Treeview 的行数据
Get row data for Treeview from Panda
我有一个 .xlsx
文件,我想使用 TKinter 的 Treeview 在 GUI 中重新创建该文件 table。我在下面有一个解决方案,可以提供我想要的输出,但它很长,我不确定是否有更好的方法。对于我的应用程序,性能是一个问题,因为我没有那么大的能力,而且我认为使用更大的 .xlsx
文件我会开始看到性能下降。
我还必须假设我不知道标题和行数,但列数小于 15
import numpy as np
import pandas as pd
xls = pd.ExcelFile('file.xlsx');
sheetData = pd.read_excel(xls, 'Sheet-1')
# Get column headings
headings = sheetData.columns
# Convert headings to list
data = list(headings.values.tolist())
# Get row count
rows = len(sheetData)
# Create tree with the the number of columns
# equal to the sheet, the id of the column
# equal to the column header and disable
# the 'treeview'
tree = ttk.Treeview(self, columns=data, show=["headings"],selectmode='browse')
# Create column headings on tree
for heading in headings:
heading = str(heading) # convert to string for processing
tree.column(heading, width=125, anchor='center')
tree.heading(heading, text=heading)
# Populate rows --The part that concerns me
for rownumber in range(rows):
rowvalue = sheetData.values[rownumber] # Get row data
rowvalue = np.array2string(rowvalue) # Convert from an np array to string
rowvalue = rowvalue.strip("[]") # Strip the string of square brackets
rowvalue = rowvalue.replace("'",'') # Replace all instances of ' with no character
tree.insert('', 'end', values= rowvalue) # Append the row to table
是否有更简单的方法来获取行数据并将其附加到树视图?
我创建了一个简单的示例来执行此操作:
import tkinter as tk
from tkinter import ttk
import pandas as pd
# Maybe This is what you want
def Start():
fp = pd.read_excel("./test.xlsx") # Read xlsx file
for _ in range(len(fp.index.values)): # use for loop to get values in each line, _ is the number of line.
tree.insert('','end',value=tuple(fp.iloc[_,[1,2]].values)) # [_,[1,2]] represents that you will get the values of second column and third column for each line.
win = tk.Tk()
win.wm_attributes('-topmost',1)
# win.geometry("+1300+0")
ttk.Button(win,text="Import it",command=Start).pack()
columns = ("name","gender")
tree = ttk.Treeview(win,show="headings",columns=columns)
tree.column("name",width=100,anchor='center')
tree.column("gender",width=50, anchor="center")
tree.heading("name",text="name")
tree.heading("gender",text="gender")
tree.pack()
win.mainloop()
这是我的示例 excel 文件:
结果:
我有一个 .xlsx
文件,我想使用 TKinter 的 Treeview 在 GUI 中重新创建该文件 table。我在下面有一个解决方案,可以提供我想要的输出,但它很长,我不确定是否有更好的方法。对于我的应用程序,性能是一个问题,因为我没有那么大的能力,而且我认为使用更大的 .xlsx
文件我会开始看到性能下降。
我还必须假设我不知道标题和行数,但列数小于 15
import numpy as np
import pandas as pd
xls = pd.ExcelFile('file.xlsx');
sheetData = pd.read_excel(xls, 'Sheet-1')
# Get column headings
headings = sheetData.columns
# Convert headings to list
data = list(headings.values.tolist())
# Get row count
rows = len(sheetData)
# Create tree with the the number of columns
# equal to the sheet, the id of the column
# equal to the column header and disable
# the 'treeview'
tree = ttk.Treeview(self, columns=data, show=["headings"],selectmode='browse')
# Create column headings on tree
for heading in headings:
heading = str(heading) # convert to string for processing
tree.column(heading, width=125, anchor='center')
tree.heading(heading, text=heading)
# Populate rows --The part that concerns me
for rownumber in range(rows):
rowvalue = sheetData.values[rownumber] # Get row data
rowvalue = np.array2string(rowvalue) # Convert from an np array to string
rowvalue = rowvalue.strip("[]") # Strip the string of square brackets
rowvalue = rowvalue.replace("'",'') # Replace all instances of ' with no character
tree.insert('', 'end', values= rowvalue) # Append the row to table
是否有更简单的方法来获取行数据并将其附加到树视图?
我创建了一个简单的示例来执行此操作:
import tkinter as tk
from tkinter import ttk
import pandas as pd
# Maybe This is what you want
def Start():
fp = pd.read_excel("./test.xlsx") # Read xlsx file
for _ in range(len(fp.index.values)): # use for loop to get values in each line, _ is the number of line.
tree.insert('','end',value=tuple(fp.iloc[_,[1,2]].values)) # [_,[1,2]] represents that you will get the values of second column and third column for each line.
win = tk.Tk()
win.wm_attributes('-topmost',1)
# win.geometry("+1300+0")
ttk.Button(win,text="Import it",command=Start).pack()
columns = ("name","gender")
tree = ttk.Treeview(win,show="headings",columns=columns)
tree.column("name",width=100,anchor='center')
tree.column("gender",width=50, anchor="center")
tree.heading("name",text="name")
tree.heading("gender",text="gender")
tree.pack()
win.mainloop()
这是我的示例 excel 文件:
结果: