Python '图像“pyimage2”不存在'?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26097811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'image "pyimage2" doesn't exist'?
提问by user3112327
This is where the error is found:
这是发现错误的地方:
global backbuttonimg
backbuttonimg = PhotoImage(file="backbutton.gif")
C6 = tkinter.Button(W_CheckDates, image=backbuttonimg, command = CheckDatesBack)
C6.pack()
I don't understand why this isn't working. I have another image in my program here:
我不明白为什么这不起作用。我的程序中有另一个图像:
def Login():
global W_Menu
W_Menu = Tk()
W_Menu.geometry('160x310+600+200')
W_Menu.title("NSS DB")
A0 = Canvas(W_Menu, width='160', height='160')
A0.pack()
global img
img = PhotoImage(file="nsslogo.gif")
A0.create_image(80,80, image=img)
I also get a similar error when I try to call the above definition after it has already been initially called (for example when my program logs out) so I have readjusted so the window simply deiconifies instead of calling it again, and I do not get the error again. However I am confused as to why I get an error with the former section of code now, as the button simply does not show up whether it is called for the first time or not. Sorry if this is a bit vague, please ask if I have not explained in enough detail. Thanks in advance.
当我在最初调用上面的定义后尝试调用它时(例如当我的程序注销时)时,我也遇到了类似的错误,所以我已经重新调整,所以窗口只是去图标化而不是再次调用它,我没有得到又是这个错误。然而,我很困惑为什么我现在在前一段代码中出现错误,因为按钮根本不显示它是否第一次被调用。对不起,如果这有点含糊,请询问我是否解释得不够详细。提前致谢。
P.S. I have looked in other threads with similar problems but none apply to me.
PS 我查看了其他有类似问题的线程,但没有一个适用于我。
采纳答案by W1ll1amvl
Ok so you say that the loginfunction works once, then it can't work again. Here the problem can be solved using tk.Toplevel()instead of tk.Tk()see: why python photoimages don't exist?and tkinter.TclError: image "pyimage3" doesn't exist
好的,所以您说该login功能工作一次,然后就无法再次工作。在这里可以使用tk.Toplevel()而不是tk.Tk()查看来解决问题:为什么python photoimages不存在?和tkinter.TclError: 图像“pyimage3”不存在
These threads mention how you can't have two instances of Tk()running simultaneously, you have to use Toplevel()instead.
这些线程提到如何不能Tk()同时运行两个实例,Toplevel()而必须使用。
Why did these threads not apply to you (i think they do...)? But just a tip, if you state that they don't apply to you, then give reasons why, it helps make your question clearer. Also, add the full traceback when your question is about a particular error.
为什么这些线程不适用于您(我认为它们适用...)?但只是一个提示,如果您声明它们不适用于您,然后给出原因,这有助于使您的问题更清楚。此外,当您的问题与特定错误有关时,请添加完整的回溯。
Hope this helps a bit.
希望这个对你有帮助。
回答by Windy71
Adding this for anyone who has tried the above with no success. If you have an erroneous path when running the script in some environments the path to the file is retained. I commented out everything from where I first use PhotoImage up to the window mainloop, run the script, close resulting gui, uncomment the code, run, and it shows the image as expected.
为任何尝试过上述方法但未成功的人添加此内容。如果在某些环境中运行脚本时出现错误路径,则会保留文件路径。我注释掉了从第一次使用 PhotoImage 到窗口主循环的所有内容,运行脚本,关闭生成的 gui,取消注释代码,运行,它按预期显示图像。

