在 tkinter 中显示 Pandas 数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31057730/
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
Displaying Pandas dataframe in tkinter
提问by grlaer
I'm creating a tkinter gui that will take user input for a variable that then gets passed to SQL and the queried data (in this case a single column data frame and boxplot). However, at this moment I can not find a means of displaying my pandas dataframe in the tk gui. I have not found any module or means of displaying this, and I've spent hours going through possible solutions to no avail. Only thing I need is for the dataframe to display in the gui and be re-rendered each time I change the dataframe through the user input function. My code atm is:
我正在创建一个 tkinter gui,它将接受用户输入的变量,然后将其传递给 SQL 和查询的数据(在本例中为单列数据框和箱线图)。但是,此时我找不到在 tk gui 中显示我的 Pandas 数据框的方法。我没有找到任何模块或方法来显示这个,我花了几个小时来研究可能的解决方案但无济于事。我唯一需要的是让数据框显示在 gui 中,并在每次我通过用户输入函数更改数据框时重新渲染。我的代码自动取款机是:
### Starting first GUI/Tkinter Script
##First load libs
import pyodbc
import numpy as np
import pandas.io.sql as sql
import pandas
import matplotlib.pyplot as plt
import pylab
import matplotlib as mpl
from tkinter import *
import sys
plt.ion()
def userinput():
global PartN, pp, df
##a = raw_input(v.get())
a = E1.get()
##print a
PartN = a
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=my server;
DATABASE=PackingList;UID=myid;PWD=mypword')
sqlr = "SELECT partmadeperhour FROM Completions WHERE PartNumber = ?
AND endtime > '2012-12-31 23:59:00' ORDER BY partmadeperhour"
df = pandas.read_sql_query(sqlr, conn, params=[PartN])
conn.close()
print(df)
stats = df['partmadeperhour'].describe()
print(stats)
print("Part Name is %s" %PartN)
##df.plot(kind='box', fontsize='20')
pp = df.plot(kind='box', fontsize='20')
plt.show(block=True)
def clear_textbox():
E1.delete(0, END)
master = Tk()
master.title('Title')
v = StringVar()
PartN = None
L1 = Label(master, text = 'Name')
L1.pack(side = LEFT)
E1 = Entry(master, textvariable = v, bd = 5)
E1.pack(side = RIGHT)
b = Button(master, text = 'Submit', command = userinput)
b.pack(side = TOP)
b2 = Button(master, text = 'Clear', command=clear_textbox)
b2.pack(side=BOTTOM)
master.mainloop()
An example of my data frame
我的数据框的一个例子
rate
0 [0.25]
1 [0.67]
2 [0.93]
... ...
1474 [5400.00]
If someone could just point me in the right direction, I don't even need code corrections just yet, I just need to hear someone to say yes it is possible (which I know it is), and to give me some kind of example. Thanks
如果有人能指出我正确的方向,我什至不需要更正代码,我只需要听到有人说是的,这是可能的(我知道这是可能的),并给我一些例子. 谢谢
采纳答案by Cameron Gagnon
I don't have enough rep to comment, otherwise I would, but I used this videoto get a grasp of interacting with pandas/numpy/matplotlib and tkinter. He ends up building a large application with it if you stick through all the videos.
我没有足够的代表来评论,否则我会,但我使用这个视频来了解与 pandas/numpy/matplotlib 和 tkinter 的交互。如果您坚持浏览所有视频,他最终会使用它构建一个大型应用程序。
Even though you aren't likely doing the exactsame thing, I think there might still be useful methods of interacting with your data and GUI gleamed from the videos.
即使您不太可能做完全相同的事情,我认为仍然有一些有用的方法可以与视频中的数据和 GUI 进行交互。
If the link ever dies, you can search on YouTube "Tkinter tutorial sentdex"
如果链接失效,您可以在 YouTube 上搜索“Tkinter 教程 sentdex”
Best of luck.
祝你好运。

