JPeroutek JPeroutek - 5 months ago 11
Python Question

Tkinter NameError only when running script from shell

I've got a Tkinter Python program, a reduced version of which can be found below:

from tkinter import *
from tkinter.ttk import *

filedialog.askopenfilename()


When I run this script from IDLE, I do not get any errors.

However, when run from PowerShell, using
python myscript.py
I get

NameError: could not find name 'filedialog'


Windows 10 x64 on a mid-2012 MacBook Pro

Answer

IDLE is probably importing it already, but in general since filedialog is a tkinter module it won't get imported with the bare:

from tkinter import *

Include an extra:

from tkinter import filedialog

and you should be good to go.