Reputation: 151
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:
### 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
Upvotes: 3
Views: 16705
Reputation: 1642
I don't have enough rep to comment, otherwise I would, but I used this video to 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.
Even though you aren't likely doing the exact same thing, I think there might still be useful methods of interacting with your data and GUI gleamed from the videos.
If the link ever dies, you can search on YouTube "Tkinter tutorial sentdex"
Best of luck.
Upvotes: 4