这个python27 32位代码在IDLE中运行完美,但是当用py2exe打包时......不太好。这不是丢失的Tk()问题的重复。当运行py2exe生成的可执行文件时,其他东西似乎未初始化,ImageTk.PhotoImage()调用过早创建图像':
writeConfigParams
basic.py - 非常基本的示例,是的,Tk()已初始化。此外,模块版本似乎在IDLE()和可执行版本
中都匹配C:\python\python_ui\exe\dist>basic.exe
Traceback (most recent call last):
File "basic.py", line 7, in <module>
File "PIL\ImageTk.pyo", line 117, in __init__
File "Tkinter.pyo", line 3367, in __init__
File "Tkinter.pyo", line 3304, in __init__
RuntimeError: Too early to create image
Exception AttributeError: "'PhotoImage' object has no attribute '_PhotoImage__photo'" in <bound method PhotoImage.__del__ of <PIL.ImageTk.Ph
otoImage object at 0x02CA3A90>> ignored
setup.py - 这是我的py2exe设置,我运行from Tkinter import *
from PIL import Image, ImageTk
root = Tk()
image = Image.open("background.jpg")
photo = ImageTk.PhotoImage(image)
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
root.mainloop()
来获取可执行文件:
python setup.py py2exe
版本信息匹配,打印图像时从IDLE()运行时给出与可执行文件相同的值:
答案 0 :(得分:1)
当其他所有方法都失败时,重建一切......
有效!