gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:18:22 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com