创建imag为时过早

2024-04-20 14:06:30 发布

您现在位置:Python中文网/ 问答频道 /正文

每当我运行以下代码时,我都会遇到这个错误。我使用的是python3可能是python2的代码。在

Traceback (most recent call last):
  File "Python Fun", line 238, in <module>
    platform1 = PlatformSprite(g, tk.PhotoImage(file="platform1.gif"), 0, 480,100, 10)
  File "C:\Users\ ------ Com\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 3394, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\ ------ Com\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 3335, in __init__
    raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image
Press any key to continue . . .

我该怎么办?

^{pr2}$

Tags: to代码incominittkinterliblocal
1条回答
网友
1楼 · 发布于 2024-04-20 14:06:30

我认为问题是Tk()对象的作用域是专门针对Game()类的。尝试将root = tk.Tk()放在python的最顶端,并使用root/pass-it作为Tkinter工作的句柄。另外,即使在将Tkinter作为tk导入之后,也使用了一个名为“tk”的变量。请考虑为该变量使用不同的名称,否则可能会遇到一些问题。在

相关问题 更多 >