转换为 py2exe 可执行文件后缺少 tkinter 属性 [英] Missing tkinter attributes after converting to py2exe executable
问题描述
我有一个 Python 3.3 脚本,它使用 tkinter
和 tkinter.filedialog
.后者用于其中一个类的这一特定行:
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))
代码在 IDLE 中运行良好.但是,在使用 py2exe
转换为二进制可执行文件后,程序运行,但在尝试调用命名行时引发以下异常:
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'
setup.py
:
from distutils.core import setup
import py2exe
setup()
setup(
console=['analyzer.py'],
options={
'py2exe':
{'includes': ['lxml.etree', 'lxml._elementpath', 'tkinter', 'tkinter.filedialog'],
}
}
)
我检查了 IDLE 和二进制版本中 tkinter.__dict__
的内容.可执行文件确实缺少 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.
附注.尝试 cx_Freeze
时似乎也出现了同样的问题.
PS. The very same problem seems to appear also when trying cx_Freeze
.
推荐答案
你必须导入 from tkinter import filedialog
我不知道为什么,但如果你只是 from tkinter import *
或 import tkinter
它不起作用.
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.
这篇关于转换为 py2exe 可执行文件后缺少 tkinter 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!