Tkinter CSV Viewer using Pandas DataFrame and Treeview

Tkinter file browser loading CSV data into a Pandas DataFrame

This project uses a Tkinter file browser to select a CSV file and Pandas read_csv() to create a DataFrame.

After the CSV file is loaded, the program displays the number of rows and columns using the DataFrame shape attribute. The records are then shown in a scrollable Treeview.

Browse CSV file
      |
      v
pd.read_csv()
      |
      v
Pandas DataFrame
      |
      +--> df.shape
      |
      +--> column names
      |
      +--> rows
             |
             v
         Treeview

Select a CSV File with Tkinter Top ↑

The file dialog lets the user browse the computer instead of hardcoding a CSV filename.

from tkinter import filedialog

file_path=filedialog.askopenfilename(
    title='Select CSV file',
    filetypes=[
        ('CSV files','*.csv'),
        ('All files','*.*')
    ]
)

If the user closes the dialog without selecting a file, askopenfilename() returns an empty string.

if not file_path:
    status_var.set(
        'No file selected.'
    )
    return

Create a Pandas DataFrame from CSV Top ↑

The selected filename is passed directly to Pandas.

df=pd.read_csv(
    file_path
)

The DataFrame now contains the CSV columns and rows.

Because a CSV may be empty, incorrectly formatted or use an unexpected text encoding, the revised application also handles common read errors.

try:
    df=pd.read_csv(
        file_path
    )

except (
    OSError,
    UnicodeDecodeError,
    pd.errors.ParserError,
    pd.errors.EmptyDataError
) as e:
    status_var.set(
        f'Unable to read CSV: {e}'
    )
    return

Display DataFrame Rows and Columns Top ↑

The DataFrame shape attribute returns:

(number_of_rows, number_of_columns)

For example:

rows=df.shape[0]
columns=df.shape[1]

The information can be displayed in a Tkinter Label:

info_var.set(
    f'Rows: {df.shape[0]}   Columns: {df.shape[1]}'
)

Display DataFrame Rows in Treeview Top ↑

Pandas DataFrame displayed in Tkinter Treeview

A Treeview is useful for displaying DataFrame data in rows and columns.

Because different CSV files can contain different column names, this project creates the Treeview headings dynamically. See also our dynamic Treeview columns and headings tutorial.

Create Dynamic Treeview Columns Top ↑

It is better not to use the actual DataFrame column names as Treeview internal identifiers. Column names can contain spaces, symbols or other values that are inconvenient as widget identifiers.

Instead, create safe internal IDs:

column_ids=[
    f'c{i}'
    for i in range(
        len(df.columns)
    )
]

If the CSV contains:

Name
Class
Mark
Admission Date

Treeview internally uses:

c0
c1
c2
c3

while the original DataFrame headings remain visible to the user.

for column_id,column_name in zip(
    column_ids,
    df.columns
):
    tree.heading(
        column_id,
        text=str(column_name)
    )

Insert DataFrame Rows Safely Top ↑

The older example used the first value in each CSV row as the Treeview iid.

iid=v[0]

This is unreliable because the first CSV column may contain duplicate values.

The revised program lets Treeview create its own unique item IDs:

tree.insert(
    '',
    tk.END,
    values=values
)

Rows can be read efficiently from the DataFrame using:

for row in df.itertuples(
    index=False,
    name=None
):
    ...

Missing values are displayed as empty Treeview cells:

values=[
    '' if pd.isna(value)
    else value
    for value in row
]

Add Vertical and Horizontal Scrollbars Top ↑

A CSV file can contain many rows and many columns, so both scroll directions are useful.

y_scroll=ttk.Scrollbar(
    table_frame,
    orient='vertical',
    command=tree.yview
)

x_scroll=ttk.Scrollbar(
    table_frame,
    orient='horizontal',
    command=tree.xview
)

tree.configure(
    yscrollcommand=y_scroll.set,
    xscrollcommand=x_scroll.set
)

The vertical scrollbar handles additional rows while the horizontal scrollbar is useful when the DataFrame contains several columns.

Complete Tkinter CSV Viewer Top ↑

The complete program lets the user select different CSV files without recreating the Treeview widget. The same Treeview is cleared and reconfigured whenever another file is loaded.

Why the Treeview Is Created Only Once Top ↑

The earlier version created a new Treeview inside trv_refresh() every time a CSV file was selected.

That can leave old widgets in the interface when several files are loaded.

The revised structure is:

Create Treeview once
        |
        v
Select CSV
        |
        +--> delete old rows
        +--> change columns
        +--> insert new rows

This is cleaner and makes repeated file selection easier to manage.

Working with Large CSV Files Top ↑

This beginner example reads the complete CSV file into memory and displays every DataFrame row.

For very large files, a later version can use:

  • read_csv() with chunksize;
  • Treeview pagination;
  • row limits for previews;
  • background loading for slow files.

For normal learning datasets, loading the complete DataFrame keeps the example easier to understand.

Next Project: Store the CSV Data in SQLite Top ↑

This page completes the first stage of our Tkinter + Pandas workflow:

CSV
 |
 v
Pandas DataFrame
 |
 v
Treeview

The next project continues from the same DataFrame and stores its data in SQLite:

CSV
 |
 v
Pandas DataFrame
 |
 v
SQLite database
Store Selected CSV Data in SQLite using Pandas

You can also search a DataFrame and display matching records in Treeview after the data has been loaded.

Frequently Asked Questions Top ↑

Q1: How does Tkinter select the CSV file?

The program uses filedialog.askopenfilename() to open a file browser and return the path selected by the user.

Q2: How is the CSV converted to a Pandas DataFrame?

The selected path is passed to pd.read_csv(), which reads the CSV data and creates a DataFrame.

Q3: How can I find the number of rows and columns?

Use df.shape[0] for the number of rows and df.shape[1] for the number of columns.

Q4: Why not use the first CSV column as the Treeview iid?

The first column may contain duplicate values. Letting Treeview generate its own item identifiers avoids duplicate iid errors.

Q5: Can Treeview display CSV files with different columns?

Yes. The program creates safe internal column IDs dynamically and uses the DataFrame column names as the visible Treeview headings.

Q6: Why are both vertical and horizontal scrollbars used?

The vertical scrollbar handles many DataFrame rows, while the horizontal scrollbar helps when the CSV contains more columns than fit inside the window.


Tkinter Projects Tkinter Pandas Projects CSV to SQLite


Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer