尝试在GUI中实施脚本时收到错误消息(NoneType'to_csv')

时间:2019-01-10 00:06:35

标签: python pandas tkinter

我正在尝试构建一个包含csv文件的小程序。我制作了一个GUI,用户可以在其中选择csv文件位置的目录,并在其中希望输出最终的组合csv文件。我目前正在使用此脚本合并csv文件。

from pathlib import Path
import pandas as pd


def add_dataset(old, new, **kwargs):
    if old is None:
        return new
    else:
        return pd.merge(old, new, **kwargs)


combined_csv = None

for csv in Path(r'C:\Users\Personal\Python\Combine').glob('*.csv'):
    dataset = pd.read_csv(csv, index_col=0, parse_dates=[0])
    combined_csv = add_dataset(combined_csv, dataset, on='DateTime', how='outer')

combined_csv.to_csv(r'C:\Users\Personal\Python\Combine\combined.csv')

我为GUI构建的脚本是这样的:     从tkinter导入*     从tkinter导入文件对话框     从pathlib导入路径     将熊猫作为pd导入     导入操作系统     根= Tk()     root.geometry(“ 400x200”)

# Setting up the 'Browse Directory' dialogs
def selectDirectory():
    global dirname
    global folder_path
    dirname = filedialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')
    folder_path.set(dirname)
    print(dirname)

def selectOutputDirectory():
    global dirname_combine
    global folder_pathcombine
    dirname_combine = filedialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')
    folder_pathcombine.set(dirname_combine)
    print(dirname_combine)


# Printing the locations out as a label
folder_path = StringVar()
lbl1 = Label(master=root, textvariable = folder_path)
lbl1.grid(row=0,column=2)

folder_pathcombine = StringVar()
lbl2 = Label(master=root, textvariable = folder_pathcombine)
lbl2.grid(row=1,column=2)


def add_dataset(old, new, **kwargs):
    if old is None:
        return new
    else:
        return pd.merge(old, new, **kwargs)

def runscript():
    combined_csv = None

    path = r'%s' % folder_path
    combine = r'%s' % folder_pathcombine

    for csv in Path(r'%s' % path).glob('*.csv'):
        dataset = pd.read_csv(csv, index_col = 0, parse_dates=[0], delimiter = ',')
        combined_csv = add_dataset(combined_csv, dataset, on='DateTime', how='inner')
    combined_csv.to_csv(r'%s\combined.csv' % combine)


# Assigning commands to buttons to select folders
selectFolder = Button(root, text = "Select directory", command = selectDirectory)
selectFolder.grid(row=0,column=0)

selectcombine = Button(root, text = "Select output directory", command = selectOutputDirectory)
selectcombine.grid(row=1, column=0)

run = Button(root, text = "Run script", command = runscript)
run.grid(row=3, column=0)


root.mainloop()

我遇到的问题是正确实现用于GUI脚本合并的脚本。合并脚本本身可以正常工作,但是当我将其实现到GUI脚本中时,出现错误“ AttributeError:'NoneType'对象没有属性'to_csv'”。我认为我的功能已在GUI中正确设置,因此我正在阅读以下文档。 https://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/functions.html

我读到没有返回值时会发生“ None”错误。因此,在这种情况下,我认为它不是将变量“ Combined”写入到csv,因为其中不存在任何内容。

完整的错误消息是这样的:

runfile('C:/Users/Personal/Python//test.py', wdir='C:/Users/Personal/Python/Combine')

C:/Users/Personal/Python/Combine

C:/Users/Personal/Python/Combine

Exception in Tkinter callback

Traceback (most recent call last):

File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1702, in __call__

return self.func(*args)

File "C:/Users/Personal/Python/Combine/gui.py", line 54, in runscript

combined_csv.to_csv(r'%s\combined.csv' % combine)

AttributeError: 'NoneType' object has no attribute 'to_csv'

任何解决错误的帮助,以及有关如何改进我的代码的任何建议,将不胜感激。我是Python的新手,正在寻求改进。谢谢!

1 个答案:

答案 0 :(得分:2)

问题是您在StringVar内的以下语句中使用runscript()

path = r'%s' % folder_path
combine = r'%s' % folder_pathcombine

因此,在以上语句下方的for循环中找不到文件,并且combine_csv未更新。

您应在.get()上使用StringVar,如下所示:

path = r'%s' % folder_path.get()
combine = r'%s' % folder_pathcombine.get()
相关问题