当我尝试使用 tkinter 将图像加载到 canvas 时,是什么导致了错误 'unknown option "pyimage1"'?
What causes the error 'unknown option "pyimage1"' while I'm trying to load image onto canvas with tkinter?
我一直在尝试使用 tkinter 通过 Python 创建 GUI,今天我决定在我的 GUI 中使用图像,但是在使用来自 tkinter 的 Canvas 时我 运行 进入此错误:
Traceback (most recent call last):
File "C:\Users063\Desktop\pomodoro\main.py", line 25, in <module>
canvas.create_image((100, 112), image_canvas)
File "C:\Users063\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2786, in create_image
return self._create('image', args, kw)
File "C:\Users063\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,其中 none 似乎对我有用。感谢您的提前建议:)
您必须提供图像作为关键字参数的值 image
:
canvas.create_image((100, 112), image=image_canvas)
# ^^^^^^
我一直在尝试使用 tkinter 通过 Python 创建 GUI,今天我决定在我的 GUI 中使用图像,但是在使用来自 tkinter 的 Canvas 时我 运行 进入此错误:
Traceback (most recent call last):
File "C:\Users063\Desktop\pomodoro\main.py", line 25, in <module>
canvas.create_image((100, 112), image_canvas)
File "C:\Users063\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2786, in create_image
return self._create('image', args, kw)
File "C:\Users063\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,其中 none 似乎对我有用。感谢您的提前建议:)
您必须提供图像作为关键字参数的值 image
:
canvas.create_image((100, 112), image=image_canvas)
# ^^^^^^