Python 3.6 - AttributeError:模块“tkinter"没有属性“filedialog" [英] Python 3.6 - AttributeError: module 'tkinter' has no attribute 'filedialog'

查看:48
本文介绍了Python 3.6 - AttributeError:模块“tkinter"没有属性“filedialog"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几分钟前,我的函数运行良好.没有修改代码,只是安装了PyAudio.我根据主题收到错误.无论是从命令行还是 IDE 运行它都没有关系,同样的错误.有什么想法吗?

My function was working perfectly fine few minutes ago. Did not modify the code, just installed PyAudio. I get the error as per subject. It doesn't matter if run it from command line or IDE, same error. Any ideas?

def DataFinder():
    #imports and initialize
    import pandas as pd
    import tkinter as tk

    finder = tk.Tk()
    finder.withdraw()

    __dataFlag = False
    #ask user to select a file
    __path = tk.filedialog.askopenfilename()
    #check the extension to handle reader
    #for csv files
    if __path.endswith('.csv')==True:
        df = pd.read_csv(__path,header=0)
        return df
        __dataFlag = True
    #and for excel files
    elif __path.endswith('.xls')==True:
        df = pd.read_excel(__path,header=0)
        return df
        __dataFlag = True
    #if a file is not a supported type display message and quit
    else:
        __dataFlag = False

    #check if there is a data to be returned
    if __dataFlag==True:
        return df
    else:
        print('The file format is not supported in this version.')

推荐答案

以下代码对我不起作用:

the following code didn't work for me:

import tkinter as tk
import tkinter.filedialog

但以下确实有效:

import tkinter
import tkinter.filedialog

还有这个:

import tkinter.filedialog
import tkinter as tk

希望能帮到你

正如 Vaidøtas 所提到的,您不能导入 filedialog 来自 tkinter.因为你没有导入原始的 tkinter 而是一个别名版本 tk.

As mentioned by Vaidøtas I., you can't import filedialog from tkinter. Because you did not import the original tkinter but an aliased version tk.

这篇关于Python 3.6 - AttributeError:模块“tkinter"没有属性“filedialog"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆