1

我正在尝试编写一个 tkinter 版本的 Flappy Bird,但在使用 tkinter 时遇到了一个从未见过的错误。我到处搜索,并尝试了我能想到的一切。到目前为止,这是我的代码:

from tkinter import *
root = Tk()
canvas = Canvas(root, height=400, width=400)
canvas.pack()
bird = PhotoImage(root, file="L:\\Programming\\Python\\flappyBird\\bird.png")

这是错误:

Traceback (most recent call last):
  File "L:\Programming\Python\flappyBird\flappyBird.py", line 5, in <module>
    bird = PhotoImage(root, file="L:\\Programming\\Python\\flappyBird\\bird.png")
  File "C:\Python34\lib\tkinter\__init__.py", line 3384, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Python34\lib\tkinter\__init__.py", line 3340, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: images may not be named the same as the main window
>>> 
4

2 回答 2

2

查看 的文档PhotoImage,似乎不建议提供任何位置参数。尝试在没有root.

bird = PhotoImage(file="L:\\Programming\\Python\\flappyBird\\bird.png")
于 2015-03-02T14:13:35.293 回答
1

PhotoImage 构造函数中的第一个关键字参数是name. 通过root作为第一个位置参数传递,它与name关键字参数相关联。因此,您试图创建一个与根窗口同名的小部件,因此错误“图像可能与主窗口命名不同”

创建图像时省略root第一个参数,问题就会消失。

于 2015-03-02T21:23:38.890 回答