为什么不存在照片图像? [英] Why photoimages don't exist?

查看:108
本文介绍了为什么不存在照片图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将展示减少部分代码,这给我带来了麻烦.

I will show a reduced portion of the code that gives me a problem.

_tkinter.TclError: image "pyimageN" doesn't exist-其中N停留1、2、3等...

_tkinter.TclError: image "pyimageN" doesn't exist - where N stays for 1, 2, 3, etc...

第一类使用背景图像显示菜单.

There is a first class that shows a menu using an image in the background.

class MenuWindow():    #in this class we show the main part of the program
    def __init__(self):
        self.Menu=Tk()
        self.MCanvas=Canvas(self.Menu)
        self.MCanvas.bind("<ButtonPress-1>",self.MenuClick)

        #unuseful lines that configure the window and the canvas#

        self.Background=PhotoImage(height=600,width=700)#a simple tkinter.PhotoImage object

        #other unuseful lines that draw the photoimage ( without reading any file, with the method put())#

        self.MCanvas.create_image((x,y),image=self.Background,state="normal")

        #unuseful lines that continue the drawing of the canvas#

第二类显示另一个窗口,在后台使用另一个图像.此类由第一类通过函数self.MenuClick的单击绑定启动.

And a second class that shows another window, using another image in the background. This class is launched by the first class via click binding of the function self.MenuClick.

class EditorWindow():    #in this class we show the main part of the program
    def __init__(self):
        self.Eenu=Tk()
        self.ECanvas=Canvas(self.Eenu)

        #unuseful lines that configure the window and the canvas#

        self.Background=PhotoImage(height=600,width=700)

        #other unuseful lines that draw the photoimage ( without reading any file , with the method put() )#

        self.ECanvas.create_image((x,y),image=self.Background,state="normal")#in this line i get the error

        #unuseful lines that continue the drawing of the canvas#

完整的回溯如下:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py", line 1399, in __call__
    return self.func(*args)
  File "/Users/albertoperrella/Desktop/slay.py", line 70, in MenuClick
    EditorWindow(self)
  File "/Users/albertoperrella/Desktop/slay.py", line 85, in __init__
    self.ECanvas.create_image((3,3),image=self.Background,state="normal",anchor="nw")
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py", line 2140, in create_image
    return self._create('image', args, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py", line 2131, in _create
    *(args + self._options(cnf, kw))))
_tkinter.TclError: image "pyimage2" doesn't exist

这两个类的制作方法相似,所以我不知道为什么第二个类会出错.我确定这不是写错误,例如(用conttruct代替construct),并且我使用的图像确实存在.

The two classes are made in a similar way, so I don't know why I get the error with the second one. I am sure that it isn't a writing error e.g.(conttruct instead of construct) and that the images I am using actually exist.

所以我认为:

  • 我在犯一些概念错误,

  • I am making some concept mistakes,

或者它是python中的错误(或Tkinter的细微行为).

or it is a bug (or subtle behaviour of Tkinter) in python.

推荐答案

我为自己解决了这个问题:

I solved myself the problem :

我定义的第二个类是问题,因为它使用了另一个根窗口,别名为Tk().与普通Tk()窗口等效的是Toplevel(),它与根相同,但没有自己的解释器上下文.

The second class I defined was the problem cause it used another root window, alias Tk(). An equivalent to the normal Tk() window is the Toplevel() that is the same as a root but hasn't its own interpreter context.

很快,要解决该问题,我不得不将EditorWindow类的 init ()方法的第一行更改为

Shortly, to solve the problem I had to change the first line of the init() method of the EditorWindow class from

        self.Eenu=Tk()

        self.Eenu=Toplevel() 

这篇关于为什么不存在照片图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆