Some objects missing from tkinter

Peter Otten __peter__ at web.de
Mon Apr 26 13:50:43 EDT 2010


Shane wrote:

> I'm new to Python, so I'll try to be clear about my problem.
> 
> I'm using Python 3.1 (latest stable version from python.org) on
> Windows 7.
> I have a program using tkinter for UI, and it works properly from both
> pything GUI shell, and running from command prompt, EXCEPT that I have
> a menu command to invoke tkinter.filedialog.askopenfile, and it fails
> because it says:
> 
> file = tkinter.filedialog.askopenfilename()
> AttributeError: 'module' object has no attribute 'filedialog'
> 
> I made a simple test program:
> 
> import tkinter
> print (dir(tkinter))
> 
> when I run this from the GUI shell, the results include filedialog,
> but from the command prompt, it does not (also missing other
> attributes, such as messagebox).
> 
> All the UI widgets work properly.
> My best hypothesis at this point is that from the GUI shell its using
> the source code under lib\tkinter (where there is a filedialog.py),
> but from the command shell it is using the compiled dll, and that
> doesn't export filedialog for some reason.


It's not that complicated; idle and your module share the same python 
interpreter and the same tkinter package. Idle needs a file dialog, too, and 
somewhere in its code there must be a

import tkinter.filedialog

statement which of course isn't executed when you run your script from the 
command line. To fix your script simply add the above import statement.

It is a bit unfortunate that your editor has side effects on your program, 
and I recommend that you never trust the result of importing a module from 
within idle's shell completely.

Peter




More information about the Python-list mailing list