1

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()

result is following image : enter image description here

when i click read csv file, it gives me possibility to read file and result is like this : enter image description here

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)

1
  • Questions that ask for general guidance regarding a problem approach are typically too broad and are not a good fit for this site. People have their own method for approaching the problem and because of this there cannot be a correct answer. Please post your good faith effort at solving this problem and edit your question to a specific issue needing resolution Commented Apr 30, 2022 at 18:24

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.