python-3.x - Python 3.6 - 属性错误 : module 'tkinter' has no attribute 'filedialog'

标签 python-3.x tkinter

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

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.')

最佳答案

以下代码对我不起作用:

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 .

关于python-3.x - Python 3.6 - 属性错误 : module 'tkinter' has no attribute 'filedialog' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45533932/

相关文章:

python - 如何使用Python Tkinter在一行中显示多个标签?

python - 带文本的 Tkinter 基本布局

python - 类(class)评估顺序?

python3 - super() 在多重继承上的行为

python - 程序阻止鼠标移动

python 3 项目结构(单机与单机)多个模块

python - 如何在 tkinter 标签中将文本居中,如 Microsoft Word 居中打字模式

python-3.x - selenium.common.exceptions.WebDriverException : Message: File not found:/path/to/file

Python + Tkinter Windows 7 任务栏进度

python - (Pyinstaller) 'tclError: can' t find package tkdnd' 如何修复此错误?