Some objects missing from tkinter

S

Shane

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.

Thanks in advance for any suggestions
 
P

Peter Otten

Shane said:
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
 
L

Lie Ryan

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.

In fact, never trust IDLE. IDLE is a nice IDE when the alternative is
Notepad; but for serious work, you need a real IDE or a programmer's
text editor (vim or emacs, whichever side you're in).

Always test the you write inside IDLE on a command line.
 
S

Shane

In fact, never trust IDLE. IDLE is a nice IDE when the alternative is
Notepad; but for serious work, you need a real IDE or a programmer's
text editor (vim or emacs, whichever side you're in).

Always test the you write inside IDLE on a command line.

Thank you both for the replies. importing tkinter.filedialog did the
trick, and I appreciate the advice about not depending on IDLE. I'm
not planning on doing any serious work, so I will probably continue to
rely on IDLE for now.
 
P

Peter Otten

Lie said:
In fact, never trust IDLE. IDLE is a nice IDE when the alternative is
Notepad; but for serious work, you need a real IDE or a programmer's
text editor (vim or emacs, whichever side you're in).

Some people, when confronted with a problem, think "I know, I'll use emacs."
Now they have two problems.

More seriously, the subset of emacs I am using is probably supported by
idle, too. That's why I sympathize with the OP's decision to stick with
idle.

Peter
 
L

Lie Ryan

Some people, when confronted with a problem, think "I know, I'll use emacs."
Now they have two problems.

More seriously, the subset of emacs I am using is probably supported by
idle, too. That's why I sympathize with the OP's decision to stick with
idle.

It's not about IDLE's text editing capability, which is perfectly fine.
It's about IDLE mangling your program's execution due to IDLE being
written in Python itself. It's often the case your program would run
fine inside IDLE but crashes badly outside; that's why if you need to
use IDLE, test your program in command line as well. Otherwise you're
going to be surprised.
 
P

Paul Rudin

Some people, when confronted with a problem, think "I know, I'll use emacs."
Now they have two problems.

Probably you know this ... but the original form of this saying had
"regular expressions" in place of "emacs".

Since Jamie Zawinski coined this saying and he was a significant
contributor to XEmacs it's quite ironic to see it in the form you give
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top