如何使用Python3.8上的Tkinter在窗口中显示通过Pandas读入python的数据帧?

如何使用Python3.8上的Tkinter在窗口中显示通过Pandas读入python的数据帧?,python,pandas,dataframe,user-interface,tkinter,Python,Pandas,Dataframe,User Interface,Tkinter,我的问题有一点背景知识:我需要使用PythonTkinter制作一个GUI。我成功地获得了带有输入字段和浏览器按钮的初始页面。单击浏览器按钮后,文件资源管理器将打开,并在输入字段中填充选定的文件路径。现在,我们选择的文件是一个包含所需数据的.xlsx文件。使用输入字段中填充的位置,我将.xlsx文件作为数据帧导入。现在我有了.xlsx文件作为数据帧,我需要在一个新窗口中以表格的形式显示这个数据帧 我目前正在使用Python 3.8。欢迎提出任何建议/意见。非常感谢:)。我在下面的代码中提供了有关

我的问题有一点背景知识:我需要使用PythonTkinter制作一个GUI。我成功地获得了带有输入字段和浏览器按钮的初始页面。单击浏览器按钮后,文件资源管理器将打开,并在输入字段中填充选定的文件路径。现在,我们选择的文件是一个包含所需数据的.xlsx文件。使用输入字段中填充的位置,我将.xlsx文件作为数据帧导入。现在我有了.xlsx文件作为数据帧,我需要在一个新窗口中以表格的形式显示这个数据帧


我目前正在使用Python 3.8。欢迎提出任何建议/意见。非常感谢:)。

我在下面的代码中提供了有关如何执行此操作的注释,正如前面提到的注释之一,执行此操作的一种方法是使用TreeView小部件

要演示下面的代码,请在电脑上创建一个excel文件(.xlsx),其中包含以下数据

然后在Tkinter GUI中(下面提供的代码)。浏览以获取此文件。然后单击load按钮将此文件加载到Treeview小部件中。下面的代码片段中提供的注释

import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import pandas as pd

# initialize the tkinter GUI
root = tk.Tk()

root.geometry("500x500")
root.pack_propagate(0)
root.resizable(0, 0)

# This is the frame for the Treeview
frame1 = tk.LabelFrame(root, text="Excel Data")
frame1.place(height=250, width=500)

# This is the frame for the Open File dialog
file_frame = tk.LabelFrame(root, text="Open File")
file_frame.place(height=100, width=400, rely=0.65, relx=0)

button1 = tk.Button(file_frame, text="Browse A File", command=lambda: File_dialog())
button1.place(rely=0.65, relx=0.50)
button2 = tk.Button(file_frame, text="Load File", command=lambda: Load_excel_data())
button2.place(rely=0.65, relx=0.30)

label_file = ttk.Label(file_frame, text="No File Selected")
label_file.place(rely=0, relx=0)

####
# THIS IS YOUR TABLE aka the TreeView widget
####
tv1 = ttk.Treeview(frame1)  # This is the Treeview Widget
column_list_account = ["Name", "Age", "Location"]  # These are our headings
tv1['columns'] = column_list_account  # We assign the column list to the widgets columns
tv1["show"] = "headings"  # this hides the default column..

for column in column_list_account:  # foreach column
    tv1.heading(column, text=column)  # let the column heading = column name
    tv1.column(column, width=50)  # set the columns size to 50px
tv1.place(relheight=1, relwidth=1)  # set the height and width of the widget to 100% of its container (frame1).
treescroll = tk.Scrollbar(frame1)  # create a scrollbar
treescroll.configure(command=tv1.yview)  # make it vertical
tv1.configure(yscrollcommand=treescroll.set)  # assign the scrollbar to the Treeview Widget
treescroll.pack(side="right", fill="y")  # make the scrollbar fill the yaxis of the Treeview widget

####


def File_dialog():
    """This function will open the file explorer"""
    filename = filedialog.askopenfilename(initialdir="/",
                                          title="Select A File",
                                          filetype=(("xlsx files", "*.xlsx"), ("all files", "*.*")))
    label_file.configure(text=filename)


def Load_excel_data():
    """ if your file is valid this will load the file into the treeview"""
    try:
        excel_filename = r"{}".format(label_file['text'])
        df = pd.read_excel(excel_filename)
    except ValueError:
        tk.messagebox.showerror("Information", "The File you have entered is invalid")
        return None

    df_rows = df.to_numpy().tolist()  # turns the dataframe into a list of lists
    for row in df_rows:
        tv1.insert("", "end", values=row)  # inserts each list into the treeview


root.mainloop()  # The mainloop for our tkinter Gui

您可以使用
ttk.Treeview
显示表格数据。@acw1668,谢谢您的评论。当然,我会调查的。我在我的问题中添加了一些背景知识,使之更具信息性。请检查一下。非常感谢。您上面共享的代码运行得非常好。我能够在单独的窗口中显示表格。非常感谢你。我试图在新窗口中将数据帧的片段作为表发布,但未能做到这一点。但是上面的代码完全按照我想要的方式工作。我们可以使用Treeview或任何其他方法在单个tkinter窗口中显示多个数据帧吗?任何形式的评论和建议都非常有用。TYSMhi@santoshdavuluri是的,我相信你能。查看“Tkinter PanedWindow小部件”。然后,您可以使用它在每个窗格窗口中显示树状视图。谢谢@RamWill的评论。有了Tkinter Panned Window小部件,在一个Tkinter窗口中添加3个数据帧会更好。但是,如果我必须在一个Tkinter窗口上查看excel工作簿中的所有excel工作表(10张),如何做到这一点?