简体   繁体   中英

How to display a data frame read into python through Pandas in a window using Tkinter on Python 3.8?

A bit of background to my question: I need to make a GUI using Python Tkinter. I was successful in obtaining the initial page with an entry field and a browser button. Once we click the browser button the file explorer will open and the selected file path gets populated in the entry field. Now, the file we select is a.xlsx file that has the required data. Using that location populated in the entry field I have imported the.xlsx file as a pandas data frame. Now that I have the.xlsx file as a data frame I need to display this data frame as a table in a new window.

I am currently using Python 3.8. Any kind of suggestions/comments are welcome. Thank you so much:).

I have provided the code below with comments on how to do this, as mentioned one of the comments one way of doing this is by using the TreeView widget.

To demo the code below create a excel file (.xlsx) on your PC with the following data

表数据

Then in the Tkinter GUI (code provided below). Browse to get this file. Then click the load button to load this file into the Treeview widget. Comments provided in the code snippet below.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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