【问题标题】:py2exe gives RuntimeError: Too early to create imagepy2exe 给出 RuntimeError: Too early to create image
【发布时间】:2016-12-08 04:30:15
【问题描述】:

这个python27 32bit代码在IDLE中完美运行,但是用py2exe打包时...不太好。这不是缺少的 Tk() 问题的重复。运行 py2exe 生成的可执行文件时,其他东西似乎未初始化,ImageTk.PhotoImage() 处的调用因“创建图像太早”而中断:

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

basic.py - 非常基本的示例,是的,Tk() 已初始化。此外,模块版本在 IDLE() 和可执行版本中似乎都匹配

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()

setup.py - 这是我的 py2exe 设置,我运行 python setup.py py2exe 来获取可执行文件:

import py2exe, sys, os
from distutils.core import setup
from glob import glob

sys.path.append("C:\\Program Files\\Microsoft Visual Studio 9.0\\VC\\redist\\x86\\Microsoft.VC90.CRT")
sys.argv.append('py2exe')
setup( 
  data_files = [
      ("Microsoft.VC90.CRT", glob(r'C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\*.*')),
      ("background.jpg"),
      ],
  options = {         
    'py2exe' : {
        'compressed': 1, 
        'optimize': 2,
        'bundle_files': 3, 
        'dist_dir': 'dist',  
        'dll_excludes': ["MSVCP90.dll"]
        }
    },                   
  zipfile=None, 
  console = [{'script':'user_code.py'}, {'script':'basic.py'}],
)

版本信息匹配,并且从 IDLE() 运行时打印图像会给出与执行文件相同的值:

  • pil 3.4.2
  • tkinter $Revision: 81008 $
  • PIL.JpegImagePlugin.JpegImageFile 图像模式=RGB 大小=179x119 at 0x3DF6A50

【问题讨论】:

  • 试过回滚枕头几个版本,停在 2.5... 不走运

标签: python tkinter py2exe photoimage


【解决方案1】:

当一切都失败时,重建一切......

  1. 卸载32位python27.12
  2. 安装 32bit python27.10 // 12 可能也可以工作
  3. 点安装点
  4. 点安装枕头
  5. 安装 MSVC9
  6. 点安装py2exe
  7. 从旧的 py2exe 中删除所有以前的分发版本
  8. 重新生成可执行文件

有效!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 2021-07-13
    • 2021-08-03
    • 2022-12-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多