Reputation: 1186
I'm unable to find a way to place the a table in Ktinker. I'm using pandastable to display the table.
from pandastable import Table, TableModel
from tkintertable import TableCanvas
def display_csv_file(self,parent):
try:
self.file_name = filedialog.askopenfilename(initialdir = '/Desktop',
title = 'Select csv file',
filetypes = (('csv file','*.csv'),
('csv file','*.csv')))
df = pd.read_csv(self.file_name)
if (len(df)== 0):
msg.showinfo('No records', 'No records')
# Now display the DF in 'Table' object
# under'pandastable' module
self.f2 = tk.Frame(parent)
self.table = Table(self.f2, dataframe=df,read_only=True, height=300, width=600)
self.f2.pack(padx=4, pady = 100)
self.table.show()
except FileNotFoundError as e:
print(e)
msg.showerror('Error in opening file',e)
I'm getting it displayed like below.
[![enter image description here][1]][1]
I need to move this table along the y axis
Upvotes: 0
Views: 515
Reputation: 1432
As you told in the comment, It seems that display_csv_file is a class method, so I wrap that function with a simple class, and it works fine for me:
import pandas as pd
import tkinter as tk
from tkinter import messagebox as msg
from tkinter import filedialog
from pandastable import Table
class Main:
def __init__(self):
self.file_name = None
self.f2 = None
self.table = None
self.root = tk.Tk()
self.display_csv_file(self.root)
self.root.mainloop()
def display_csv_file(self, parent):
try:
self.file_name = filedialog.askopenfilename(initialdir='/Desktop',
title='Select csv file',
filetypes=(('csv file', '*.csv'),
('csv file', '*.csv')))
df = pd.read_csv(self.file_name)
if len(df) == 0:
msg.showinfo('No records', 'No records')
# Now display the DF in 'Table' object
# under'pandastable' module
self.f2 = tk.Frame(parent)
self.table = Table(self.f2, dataframe=df, read_only=True, height=300, width=600)
self.f2.pack(padx=4, pady=100)
self.table.show()
except FileNotFoundError as e:
print(e)
msg.showerror('Error in opening file', e.filename)
if __name__ == '__main__':
m = Main()
Upvotes: 1