问题
I have a Python 3.3 script which uses tkinter
and tkinter.filedialog
. The latter is being used in this particular line of one of the classes:
self.root_folder = os.path.realpath(tk.filedialog.askdirectory(**self.dir_opt))
The code runs well in IDLE. However, after being converted to a binary executable using py2exe
, the program runs, but raises the following exception when trying to call the named line:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
File "analyzer.py", line 403, in load_files
File "analyzer.py", line 388, in select_root
AttributeError: 'module' object has no attribute 'filedialog'
The setup.py
:
from distutils.core import setup
import py2exe
setup()
setup(
console=['analyzer.py'],
options={
'py2exe':
{'includes': ['lxml.etree', 'lxml._elementpath', 'tkinter', 'tkinter.filedialog'],
}
}
)
I checked the contents of tkinter.__dict__
in the IDLE and binary versions. The executable lacks indeed the filedialog
attribute, among some others. For instance:
**IDLE** **EXE**
_varnum _varnum
colorchooser
commondialog
constants constants
dialog
filedialog
font
getboolean getboolean
getdouble getdouble
getint getint
image_names image_names
image_types image_types
mainloop mainloop
messagebox
re re
What am I doing wrong? I would be really grateful for your help.
PS. The very same problem seems to appear also when trying cx_Freeze
.
回答1:
You have to import from tkinter import filedialog
I have no idea why, but if you just from tkinter import *
or import tkinter
it won't work.
来源:https://stackoverflow.com/questions/28933005/missing-tkinter-attributes-after-converting-to-py2exe-executable