Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.6 - AttributeError: module 'tkinter' has no attribute 'filedialog'

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.')
like image 764
Bartek Malysz Avatar asked Aug 06 '17 16:08

Bartek Malysz


2 Answers

Explicitly import of filedialog can solve the problem. So, you just need to add this line to your codes:

import tkinter.filedialog

You can find more information at Why tkinter module raises attribute error when run via command line but not when run via IDLE?

like image 72
Makan Avatar answered Oct 17 '22 23:10

Makan


the following code didn't work for me:

import tkinter as tk
import tkinter.filedialog

But the following did work:

import tkinter
import tkinter.filedialog

and also this:

import tkinter.filedialog
import tkinter as tk

Hope this helps

Note

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.

like image 45
Hank W Avatar answered Oct 18 '22 00:10

Hank W