1
from tkinter import filedialog, Label, Button, Entry, StringVar
from tkinter.filedialog import askopenfile
import pandas as pd

root = tk.Tk()
Label(root, text='File Path').grid(row=0, column=0)
v = StringVar()
entry = Entry(root, textvariable=v).grid(row=0, column=1)
Button(root, text='Browse Data Set',command=lambda:   v.set(askopenfile())).grid(row=1, column=0)
Button(root, text='Close',command=root.quit()).grid(row=1, column=1)
root.file = v.get()
df = pd.read_csv(root.file)
root.mainloop()

I want to open a dataset (CSV file) on the click of a button and read it using pd.read_csv() function i am getting some errors

Traceback (most recent call last):
  File "/home/abishek/PycharmProjects/untitled1/temp.py", line 21, in <module>
    df = pd.read_csv(root.file)
  File "/usr/lib/python3/dist-packages/pandas/io/parsers.py", line 498, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/usr/lib/python3/dist-packages/pandas/io/parsers.py", line 275, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "/usr/lib/python3/dist-packages/pandas/io/parsers.py", line 590, in __init__
    self._make_engine(self.engine)
  File "/usr/lib/python3/dist-packages/pandas/io/parsers.py", line 731, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "/usr/lib/python3/dist-packages/pandas/io/parsers.py", line 1103, in __init__
    self._reader = _parser.TextReader(src, **kwds)
  File "pandas/parser.pyx", line 353, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:3246)
  File "pandas/parser.pyx", line 591, in pandas.parser.TextReader._setup_parser_source (pandas/parser.c:6111)
OSError: File b'' does not exist

Process finished with exit code 1

Please Help me with this one i am new to Tkinter

I Have done the first part now i have an other problem

1.I browsed a file 2.I will get the columns of the data Frame using list(df) and i wanted it to displayed it in an option menu i am doing it with the following code

import tkinter as tk
from tkinter.filedialog import askopenfilename
import pandas as pd

root = tk.Tk()
v = tk.StringVar(root)
v1 = tk.StringVar(root)
v2 = tk.StringVar(root)
v3 = tk.StringVar(root)
df = pd.DataFrame()
col = []
ss = ['a','b','c','d','e']


def get_data_frame():
    global v
    global df
    global col
    file_name = askopenfilename()
    v.set(file_name)
    df = pd.read_csv(file_name)
    col = list(df)
    print(col)


def fill():
    return list(df)


tk.Label(root, text='File Path').grid(row=0, column=0)
entry = tk.Entry(root, textvariable=v).grid(row=0, column=1)
tk.Button(root, text='Browse Data Set', command=get_data_frame).grid(row=0, column=3)
tk.Label(root, text='Class LabelAttribute').grid(row=1, column=0)
tk.OptionMenu(root,v1,*col).grid(row=1, column=1)
v1.set('Nil')
tk.Label(root, text='Row Counter Attribute').grid(row=2, column=0)
v2.set('Nil')
tk.OptionMenu(root,v2,*col).grid(row=2, column=1)
tk.Button(root, text='Close', command=root.destroy).grid(row=5, column=3)
tk.Entry(root, textvariable=v3).grid(row=6, column=0)
tk.Button(root, text='Setter', command=lambda: v3.set(type(col[0]))).grid(row=6, column=1)
v3.set(col)
root.mainloop()
print(col)

but python is giving the following error

Traceback (most recent call last):
  File "/home/abishek/PycharmProjects/untitled1/GUI.py", line 34, in <module>
    tk.OptionMenu(root,v1,*col).grid(row=1, column=1)
TypeError: __init__() missing 1 required positional argument: 'value'
Abishek Kumaresan
  • 107
  • 1
  • 4
  • 15
  • 2
    any code coming before `root.mainloop` executes instantaneously after the program starts, so the user has no time whatsoever to enter data into the Entry or click any of your buttons. `root.file = v.get()` will assign an empty string to `root.file`. If you want code to execute after the user has entered input, you need to put it into a function and register it as a callback using `command=` or `.bind`. – Kevin Nov 30 '16 at 16:56

3 Answers3

5

As suggested by @Kevin, you need to put some of the functionality in to a function that is called when the button is pressed. I've provided an example (I don't have pandas installed, so the pandas part is commented out). You also should be using askopenfilename not askopenfile. I've also fixed your close button, note I've changed it to root.destroy and I haven't put () at the end.

import tkinter as tk
from tkinter.filedialog import askopenfilename
#import pandas as pd


def import_csv_data():
    global v
    csv_file_path = askopenfilename()
    print(csv_file_path)
    v.set(csv_file_path)
    #df = pd.read_csv(csv_file_path)

root = tk.Tk()
tk.Label(root, text='File Path').grid(row=0, column=0)
v = tk.StringVar()
entry = tk.Entry(root, textvariable=v).grid(row=0, column=1)
tk.Button(root, text='Browse Data Set',command=import_csv_data).grid(row=1, column=0)
tk.Button(root, text='Close',command=root.destroy).grid(row=1, column=1)
root.mainloop()
scotty3785
  • 6,763
  • 1
  • 25
  • 35
  • 1.I read a file using File Dialog 2. parsed it into dataframe 3. Now i want an option menu with the column names of a dataframe See the question for the code Please tell me how to do it – Abishek Kumaresan Dec 02 '16 at 07:36
  • Raise a new question. I've already answered what you originally asked. – scotty3785 Dec 06 '16 at 10:16
  • I can't ask new questions because of many down votes Will you please add the link to your answer – Abishek Kumaresan Dec 07 '16 at 13:00
  • No sorry. You are probably getting so many down votes because you are expecting people to write the code for you. Take some tutorials and learn the basics yourself. Then come back with well formed questions to solve genuine problems rather than just "please write for me" – scotty3785 Dec 08 '16 at 09:49
  • No Sir I don't expect people to write code for me and I know nobody will do it for me, I am working in data mining with Python and totally new to Tkinter, my level of understanding is less(In the mathematical background of mining) that is y i got down votes and I am working on it. – Abishek Kumaresan Dec 09 '16 at 10:45
1

Just include print(df.head()) after df = pd.read_csv(csv_file_path), in next line within same function and you will get the answer.

adiga
  • 34,372
  • 9
  • 61
  • 83
Rishi S
  • 11
  • 2
  • try to highlight the keywords and be clear with the format it will help to reach out your answer for others. [how to answer](https://stackoverflow.com/help/how-to-answer) – Agilanbu Dec 07 '18 at 08:57
0

I believe this question has been answered in great detail, but here's my two cents to what is a less-fancy, equally functional approach:

import tkinter as tk
from tkinter.filedialog import askopenfilename
import pandas as pd

root = tk.Tk()
root.withdraw() #Prevents the Tkinter window to come up
exlpath = askopenfilename()
root.destroy()
print(exlpath)
df = pd.read_excel(exlpath)