frenchfry
frenchfry

Reputation: 77

How to display content of Pandas data frame in Tkinter GUI window

I managed to create a Pandas data frame with content I managed to create a simple window with TKinter.

Pandas data frame content prints/displays fine in console or iPython but I would like to have what I see there to appear in my Tkinter window instead. Is there an easy way to achieve this?

Upvotes: 1

Views: 13302

Answers (1)

frenchfry
frenchfry

Reputation: 77

Here is an example for other Noobs who might have the same question one day. Sorry, can't explain how it works. Somewhere I found this example using that class "PrintToT1" and it seems to do what I wanted.

import pandas as pd
import numpy as np

import sys 
from tkinter import * 

dates = pd.date_range('20160101', periods=6)
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))

root = Tk() 

t1 = Text(root) 
t1.pack() 

class PrintToT1(object): 
 def write(self, s): 
     t1.insert(END, s) 

sys.stdout = PrintToT1() 

print ('Hello, world!') 
print (df)

mainloop() 

I would have posted the link to where I originally found this but after searching... and searching... and searching... trying... etc. I can not find it.

P.S. Credits goes to "Jarad" for finding and correcting the missing "dates" definition.

Upvotes: 2

Related Questions