【发布时间】:2020-07-24 16:54:24
【问题描述】:
我是 python GUI 新手,在运行这段代码时遇到错误,我正在寻找如何在使用函数时在 tkinter 中插入图像
def do(self):
self.img = PhotoImage(file=r"file/ball1.png")
self.canvas = Canvas(root, width =500, height = 500)
self.canvas.pack()
self.canvas.create_image(50,50, anchor=NW, image=self.img)
self.canvas.image = self.img
我收到了这个错误
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\SONY\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\SONY\Desktop\SPEECH\speech - Copy.py", line 46, in <lambda>
but1=Button(root,text="START",font=("potra",45),image=photoimage,fg="white",bg="white",compound="center",activeforeground="#13D678",command=lambda:do(1))
File "C:\Users\SONY\Desktop\SPEECH\speech - Copy.py", line 11, in do
self.img = PhotoImage(file=r"file/ball1.png")
AttributeError: 'int' object has no attribute 'img'
【问题讨论】:
-
你的函数没有名字,你的缩进也不行。
-
好的对不起,我很着急,所以我在这里写错了代码,但我现在已经编辑了问题,所以现在看看解决方案
-
不,这根本不起作用
-
请发布一个最小的例子。
-
你调用了
do(1),所以do()的self将传递整数1。所以self.img = PhotoImage(...)行将与1.img = PhotoImage(...)相同,从而导致异常。跨度>
标签: python image user-interface tkinter error-handling