_tkinter.TclError:image" Horseshoe.gif"不存在

时间:2015-09-29 09:01:31

标签: python tkinter

您好我正在使用Tkinter在Python中创建一个老虎机。 代码文件和图像都包含在一个文件夹中:

enter image description here

然而,当我运行代码并调用图像时,我收到错误:

int main()
{
    std::unique_ptr<action::Chrome>chrome(new action::Chrome());
    const std::vector<uint_16>xLocation = { 1155, 1165, 1205, 1245, 1285 };
    std::vector<uint_16>yLocation;

    //Fill yLocation
    //Yada yada, other code

    std::thread task[6];
    for(uint_8 i = 0; i < 6; i++)task[i] = std::thread((chrome->click, xLocation, yLocation[i]));
    for(uint_8 i = 0; i < 6; i++)task[i].join();
}

这是我的整个代码:

_tkinter.TclError: image "imagename.gif" doesn't exist

1 个答案:

答案 0 :(得分:0)

考虑以下代码:

photoList = ["7.gif","Apple.gif","Bell.gif",...]
...
slot1 = photoList[randint(0,8)]
...
canvas1.create_image(26,26, image = slot1)

photoList是文件名列表。创建图像时,您将为其指定文件名。这不是您在画布上创建图像的方式。您必须使用文件名创建PhotoImage实例,然后将该实例传递给create_image方法。

由于您不止一次使用这些图像,因此首先将所有文件名转换为图像是有意义的,并将图像存储在photoList中,而不是将图像存储到图像中。这样,您就不必更改创建图像的代码。

注意:除了这个问题之外,你还得到了一个程序,你可以在画布上的同一个地方创建图像,将它们叠加在一起。虽然这需要一段时间,但最终你会耗尽内存。您应该销毁旧图像,或者重复使用它们。