Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法将图像与tkinter标签关联_Python_User Interface_Tkinter_Photoimage - Fatal编程技术网

Python 无法将图像与tkinter标签关联

Python 无法将图像与tkinter标签关联,python,user-interface,tkinter,photoimage,Python,User Interface,Tkinter,Photoimage,我正在尝试使用tkinter.Label()小部件向tkinter GUI显示图像。这个过程看起来简单明了,但这段代码不起作用 代码: 当我们执行此代码时,它不会编译,并给出一个错误: TclError: image "pyimage9" doesn't exist 当我定义标签时,如果没有其父根,则不会发生编译错误,但GUI不会显示任何图像 有人能确定问题出在哪里吗?在调用任何其他tkinter函数之前,您需要创建根小部件。将根目录的创建移动到创建图像之前。我在tkinter中显示图像的一般

我正在尝试使用tkinter.Label()小部件向tkinter GUI显示图像。这个过程看起来简单明了,但这段代码不起作用

代码:

当我们执行此代码时,它不会编译,并给出一个错误:

TclError: image "pyimage9" doesn't exist
当我定义
标签
时,如果没有其父
,则不会发生编译错误,但GUI不会显示任何图像


有人能确定问题出在哪里吗?

在调用任何其他tkinter函数之前,您需要创建根小部件。将
根目录的创建移动到创建图像之前。

我在tkinter中显示图像的一般方法是:

import Tkinter as tk
root = tk.Tk()
image1 = tk.PhotoImage(file = 'name of image.gif')
# If image is stored in the same place as the python code file,
# otherwise you can have the directory of the image file.
label = tk.Label(image = image1)
label.image = image1 # yes can keep a reference - good!
label.pack()
root.mainloop()
在上述情况下,它是有效的,但您有如下情况:

import Tkinter as tk
image = tk.PhotoImage(file = 'DreamPizzas.gif') #here this is before root = tk.Tk()
root = tk.Tk()
# If image is stored in the same place as the python code file,
# otherwise you can have the directory of the image file.
label = tk.Label(image = image)
label.image = image
label.pack()
root.mainloop()
这给了我一个运行时错误:创建映像太早。


但是你说你的错误是
image pyimage9
不存在,这很奇怪,因为在顶部你将
filename
设置为'AP_icon.gif',所以你会认为你得到了不同的错误,因为我不知道
pyimage9
来自哪里。这让我觉得你可能在什么地方把文件名弄错了?您还需要将
root=tk.tk()
移动到imports下的顶部。

当我们尝试在Ipython中运行上述代码时,会出现此问题。这可以通过改变线路来解决

root = tk.Tk() to

root = tk.Toplevel()

尝试以下代码,因为我能够纠正相同的错误:

window=Tk()
c=Canvas(window,height=2000,width=2000)
p=PhotoImage(file='flower1.gif',master = c)
c.create_image(500,500,image=p)

重新启动内核以消除错误“TclError:image“pyimage9”不存在”

如果我交换
root=tk.tk()
tkim=ImageTk…
语句的顺序,您的代码对我来说是有效的。谢谢。但我尝试交换语句,但同样的错误再次出现。还有其他可能的原因吗?你是否绝对肯定上面的代码给出了你所说的错误?通常,内部图像的编号从零开始,但您声称即使您的代码试图创建单个图像,错误也会提到“pyimage9”。您问得好。事实上,我的编号从1开始。但是当我再次运行时(我在ubuntu 12.04中使用ipython),数字会更新为2,3,等等。我复制的错误是我第九次尝试运行类似的代码。谢谢。但是我在一开始就尝试创建根,但是同样的错误再次出现。还有其他可能的原因吗?上面的@unutbu似乎让代码与开关一起工作。您所说的为您工作的代码(上面的代码)给了我一个空白GUI,可能是因为
标签
是在没有父
的情况下定义的。但是当我用父
定义它时,相同的错误
TclError:image“pyimage1”不存在
再次出现。即使在定义
root
之前定义
image1
时,也会得到相同的错误,而不是您指定的
运行时错误。文件名是正确的,因为正如代码中提到的,
im.show()
确实显示了图像。这很奇怪,定义标签时没有父级
root
是可以的,除非有其他tkinter窗口同时运行,否则您要这样做吗?也许要解决这个问题,你应该额外增加windows Toplevel()…?是的,很奇怪,不是吗?我刚才试过
Toplevel(root)
。这没用。同样的错误!:(我不知道,你在使用什么IDE?python 2.7.3,使用IPython 0.12.1,os Ubuntu 12.04这不是一个真正的修复程序,因为你依赖于在创建第一个小部件时创建的合法根窗口。你最终得到的变量
root
,实际上不是真正的根窗口。@BryanOakley,谢谢你的反馈。现在,我是ma以这种方式解决这个问题。有没有更好的解决方案或适当的修复方法?我已经给出了一个简单的答案:在调用任何其他tkinter命令之前,您需要显式地创建根窗口。
window=Tk()
c=Canvas(window,height=2000,width=2000)
p=PhotoImage(file='flower1.gif',master = c)
c.create_image(500,500,image=p)