gpt4 book ai didi

python - 如何使用 tkinter 正确显示 pandas 数据框?

转载 作者:太空宇宙 更新时间:2023-11-03 20:25:50 25 4
gpt4 key购买 nike

我对 python 相当陌生,我正在尝试创建一个工具来显示文件夹中 Excel 工作簿所有工作表的行数和列数。我希望使用 tkinter 将数据框显示为最终结果,但是由于数据框的最后两列出现在新行上,因此显示无法正确显示。我想知道如何纠正这个问题。我尝试过使用 PyQT5,但这一直使我的内核崩溃,我尝试过使用 Treeviews,但无法弄清楚如何将此数据框正确写入 Treeview。以下是我当前的代码:

import pandas as pd
import tkinter as tk
import glob
import os
import xlrd

def folder_row_count():
folder_path = f_path_entry.get()
file_extension = file_ext_var.get()
window = tk.Tk()
t1 = tk.Text(window)
t1.grid()
if file_extension == "xlsx":
filenames = []
sheetnames = []
sheetrows = []
sheetcols = []
for fname in glob.glob(os.path.join(folder_path, f"*.{file_extension}")):
wb = xlrd.open_workbook(fname)
filename = []
sheetname = []
sheetrow = []
sheetcol = []
for sheet in wb.sheets():
filename.append(os.path.basename(fname))
sheetname.append(sheet.name)
sheetrow.append(sheet.nrows)
sheetcol.append(sheet.ncols)
filenames.append(filename)
sheetnames.append(sheetname)
sheetrows.append(sheetrow)
sheetcols.append(sheetcol)
flat_filenames = [item for filename in filenames for item in filename]
flat_sheetnames = [item for sheetname in sheetnames for item in sheetname]
flat_sheetrows = [item for sheetrow in sheetrows for item in sheetrow]
flat_sheetcols = [item for sheetcol in sheetcols for item in sheetcol]
df = pd.DataFrame({'File Name': flat_filenames,
'Sheet Name': flat_sheetnames,
'Number Of Rows': flat_sheetrows,
'Number Of Columns': flat_sheetcols
})
main_df = df.append(df.sum(numeric_only = True).rename('Total'))
t1.insert(tk.END, main_df)
window.mainloop()

file_ext_list = ["xlsx"]

window = tk.Tk()

window.title("Row Counter")

tk.Label(window, text = "Choose File Type:").grid(row = 1, column = 0)

file_ext_var = tk.StringVar(window)
file_ext_dd = tk.OptionMenu(window, file_ext_var, *file_ext_list)
file_ext_dd.config(width = 10)
file_ext_dd.grid(row = 1, column = 1)

tk.Label(window, text = "Folder Path:").grid(row = 2, column = 0)

f_path_entry = tk.Entry(window)
f_path_entry.grid(row = 2, column = 1)

tk.Button(window, text = "Count Rows", command = folder_row_count).grid(row = 4, column = 1)

window.mainloop()

其次,我将非常感谢任何有关如何改进此代码并使其更加高效的评论。

提前致谢。

最佳答案

您只需通过 iterrows 迭代您的 df 并将它们插入到您的 Treeview 中。下面是一个基本示例:

import tkinter as tk
from tkinter import ttk
import pandas as pd

root = tk.Tk()

sample = {"File Name":[f"file_{i}" for i in range(5)],
'Sheet Name': [f"sheet_{i}" for i in range(5)],
'Number Of Rows': [f"row_{i}" for i in range(5)],
'Number Of Columns': [f"col_{i}" for i in range(5)]
}
df = pd.DataFrame(sample)
cols = list(df.columns)

tree = ttk.Treeview(root)
tree.pack()
tree["columns"] = cols
for i in cols:
tree.column(i, anchor="w")
tree.heading(i, text=i, anchor='w')

for index, row in df.iterrows():
tree.insert("",0,text=index,values=list(row))

root.mainloop()

此外,我发现您正在使用 xlrd 首先读取 Excel,然后再将其转换为 Dataframe。你为什么不使用 pandas.read_excel相反?

关于python - 如何使用 tkinter 正确显示 pandas 数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57829917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com