使用来自Tkinter GUI的read_csv打开和读取csv文件

时间:2016-11-30 16:52:23

标签: python tkinter

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

我想点击一个按钮打开一个数据集(CSV文件)并使用pd.read_csv()函数读取它我遇到了一些错误

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

请帮助我,我是Tkinter的新手

我已经完成了第一部分,现在我有另一个问题

1.我浏览了一个文件 2.我将使用list(df)获取数据框的列,我希望它在选项菜单中显示它我正在使用以下代码

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)

但是python发出以下错误

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'

3 个答案:

答案 0 :(得分:4)

正如@Kevin所建议的那样,您需要将一些功能放入按下按钮时调用的函数中。 我提供了一个例子(我没有安装pandas,因此pandas部分已被注释掉)。 你也应该使用askopenfilename而不是askopenfile。 我也修了你的关闭按钮,注意我已经把它改成root.destroy而且我还没有把()放在最后。

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

答案 1 :(得分:1)

只需在print(df.head())之后的df = pd.read_csv(csv_file_path)中加入相同功能的下一行,即可得到答案。

答案 2 :(得分:0)

我相信这个问题已经得到了详细的解答,但这是我的两分钱,这是一种花哨的,功能相同的方法:

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)