python - 当我尝试使用 tkinter 将图像加载到 Canvas 上时,是什么导致错误 'unknown option "pyimage 1"'?

标签 python tkinter tkinter-canvas

我一直在尝试使用 tkinter 通过 Python 创建 GUI,今天我决定在我的 GUI 中使用图像,但是在使用来自 tkinter 的 Canvas 时我遇到了这个错误:

    Traceback (most recent call last):
      

   File "C:\Users\37063\Desktop\pomodoro\main.py", line 25, in <module>
        canvas.create_image((100, 112), image_canvas)
   File "C:\Users\37063\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2786, in create_image
        return self._create('image', args, kw)
   File "C:\Users\37063\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2772, in _create
        return self.tk.getint(self.tk.call(
    _tkinter.TclError: unknown option "pyimage1" .

这是我的代码:

from tkinter import *
window = Tk()
window.title("Gui test")
canvas = Canvas(width=200, height=224)
image_canvas = PhotoImage(file='ygo.gif')
canvas.pack()
canvas.create_image((100, 112), image_canvas)

window.mainloop()

当我用谷歌搜索这个错误时,我看到有时它可以通过更改图像格式来解决,我尝试了 .png、.jpeg、.gif,但它们似乎都不适合我。感谢您的提前建议:)

最佳答案

您必须提供图像作为关键字参数 image 的值:

canvas.create_image((100, 112), image=image_canvas)
#                               ^^^^^^

关于python - 当我尝试使用 tkinter 将图像加载到 Canvas 上时,是什么导致错误 'unknown option "pyimage 1"'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66236056/

相关文章:

python - 列表理解 : Returning two (or more) items for each item

grid - 通过网格在Tkinter中显示Matplotlib导航工具栏

Python 2.7 - 如何在 Tkinter GUI 中使用 Observer,在框架之间切换?

python - Tkinter:如何在 Canvas 窗口中获取框架以扩展到 Canvas 的大小?

python - 如何使用Python读取命令行生成的 "error"内容?

python - 如何在 python 中获取 os.walk 的进度?

python lxml - 循环/遍历 excel 行并将每一行保存为一个 xml

python - 在 tkinter 中使用 sys.stdout.write ("\r...")

python - tkinter 中的冒泡排序可视化

python - Tkinter 框架展开仅显示我的一半窗口