我是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'

最佳答案

您的函数没有名称,缩进也不行。 –

关于python - 在Python Tkinter中使用函数时添加图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63078094/

10-16 14:53