Reputation:
i want to create a simple GUI form, which asks user to browse file and then display result, i have wrote following code :
import numpy as np
import pandas as pd
from tkinter import *
from tkinter.filedialog import askopenfilename
def read_file():
filename =askopenfilename()
label1.insert(0,filename)
return
def display_file():
filename1 =label1.get()
data =pd.read_csv(filename1)
print(data.head())
root =Tk()
#root.withdraw()
label1 =Entry(root,width=100)
button =Button(root,text="Read csv file",command=read_file)
button1 =Button(root,text="Display file",command=display_file)
button.pack()
label1.pack()
button1.pack()
root.mainloop()
when i click read csv file, it gives me possibility to read file and result is like this :
now i need display part :
def display_file():
filename1 =label1.get()
data =pd.read_csv(filename1)
print(data.head())
this function just display files in working directory, but i need to show(5 rows of dataframe) in GUI form, let us suppose that file contains just two column - please tell me how to do? i have searched a lot but could not find exact solution (different solutions was presented and i was confused)
Upvotes: 1
Views: 563
Reputation: 1432
You have to add a Tkinter element where you want the data to be displayed (here, I choose Label) using a Variable element and set this element to hold your data from inside the display_file formula:
import numpy as np
import pandas as pd
from tkinter import *
from tkinter.filedialog import askopenfilename
def read_file():
filename = askopenfilename()
label1.insert(0, filename)
return
def display_file():
filename1 = label1.get()
data = pd.read_csv(filename1)
print(data.head())
pd_variable.set(data.head())
root = Tk()
# root.withdraw()
label1 = Entry(root, width=100)
button = Button(root, text="Read csv file", command=read_file)
button1 = Button(root, text="Display file", command=display_file)
pd_variable = Variable(root)
label2 = Label(root, textvariable=pd_variable)
button.pack()
label1.pack()
button1.pack()
label2.pack()
root.mainloop()
Upvotes: 1