James Cook
James Cook

Reputation: 344

Import pandas table into tkinter project

I have used tkinter and its treeview widget thus far in my project to import and view some data from a csv file. However Im finding its functions limited as to what Im trying to achieve.

I have read in other SO questions that a Pandas data-frame can be imported to Tkinter project and display in the tkinter GUI. I have found some code online @ https://gist.github.com/gugat/7cf57eb628f3bb0a3d54b3f8d0023b63 but I cant work out how to migrate this into my existing tkinter code.

import tkinter as tk
from tkinter import *
import tkinter.ttk as tkrttk
from PIL import Image, ImageFont, ImageTk
import csv
from tkinter import filedialog
import pandas as pd
from pandastable import Table, TableModel

root = tk.Tk()
root.geometry("2000x1000")
filepath = (r"C:/Users\James\Desktop\test_data.csv")
root.title('Workshop Manager')
style = tkrttk.Style()
style.configure("Treeview.Heading", foreground='Red', font=('Helvetica', 10))

df = pd.read_csv(filepath)

pt = Table(parent)
class TestApp(Frame):
        """Basic test frame for the table"""
        def __init__(self, parent=root):
            self.parent = parent
            Frame.__init__(self)
            self.main = self.master
            self.main.geometry('600x400+200+100')
            self.main.title('Table app')
            f = Frame(self.main)
            f.pack(fill=BOTH,expand=1)
            df = TableModel.getSampleData()
            self.table = pt = Table(f, dataframe=df,
                                    showtoolbar=True, showstatusbar=True)
            pt.show()
            return

app = TestApp()

root.mainloop()

I get an error NameError name parent is not defined im assuming this pt = Table(parent) is my issue. I have tried pt = Table(root) as I thought this would place it on the tkinter root window. But this didnt work.

Upvotes: 2

Views: 5517

Answers (1)

acw1668
acw1668

Reputation: 46687

Part of your code is from the example used in the document of pandastable, but it is not a good example.

If you just want to show your CSV file using pandastable, below is a simple example:

import tkinter as tk
from pandastable import Table, TableModel

filepath = 'C:/Users/James/Desktop/test_data.csv'

root = tk.Tk()
root.geometry('1600x900+10+10')
root.title('Workshop Manager')

class TestApp(tk.Frame):
    def __init__(self, parent, filepath):
        super().__init__(parent)
        self.table = Table(self, showtoolbar=True, showstatusbar=True)
        self.table.importCSV(filepath)
        self.table.show()

app = TestApp(root, filepath)
app.pack(fill=tk.BOTH, expand=1)

root.mainloop()

Upvotes: 3

Related Questions