python - 在 Python Tkinter 中使用函数时添加图像

标签 python image user-interface tkinter error-handling

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

相关文章:

javascript - 改变不透明度会破坏图像的定位

javascript - 为什么 Google Chrome 9 不显示来自 Amazon S3 的图像?

user-interface - 用户是否应该实时查看自己的性能统计数据

java - Swing - 使用 UpdateUI 更改面板的内容

java - 围绕基于命令行的程序编写 gui 包装器

python - 在 Pygame 中绘制箭头

python - 如何找到两个轮廓之间的距离

python - 在 Pandas 中计算同现的更好方法

Python Telethon 获取组的所有管理员

android - 更新 ListView ?