简体   繁体   中英

Adding image while using function in Python Tkinter

I am new to python GUI and I am getting error while running this block of code,I am searching for how to insert image in tkinter while using functions

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 

I got this error

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'

Your function has no name and your indentation isn't ok either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM