如何在 Tkinter GUI 窗口中显示 Pandas 数据框的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26629695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:36:53  来源:igfitidea点击:

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

pythonpandastkinter

提问by frenchfry

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

我设法创建了一个包含内容的 Pandas 数据框,我设法使用 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?

Pandas 数据框内容可以在控制台或 iPython 中正常打印/显示,但我希望在那里看到的内容出现在我的 Tkinter 窗口中。有没有简单的方法来实现这一目标?

回答by frenchfry

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.

以下是其他有一天可能会遇到相同问题的新手的示例。抱歉,无法解释它是如何工作的。在某处我发现这个例子使用那个类“PrintToT1”,它似乎做我想做的。

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.

PS Credits 转到“Jarad”以查找和更正缺失的“日期”定义。