Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python Tkinter中使用函数时添加图像_Python_Image_User Interface_Tkinter_Error Handling - Fatal编程技术网

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

在Python Tkinter中使用函数时添加图像,python,image,user-interface,tkinter,error-handling,Python,Image,User Interface,Tkinter,Error Handling,我是PythonGUI新手,在运行这段代码时遇到错误,我正在搜索如何在使用函数时在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

我是PythonGUI新手,在运行这段代码时遇到错误,我正在搜索如何在使用函数时在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'
Tkinter回调中出现异常 回溯(最近一次呼叫最后一次): 文件“C:\Users\SONY\AppData\Local\Programs\Python36\lib\tkinter\\ uuuuu init\uuuuuu.py”,第1705行,在调用中__ 返回self.func(*args) 文件“C:\Users\SONY\Desktop\SPEECH\SPEECH-Copy.py”,第46行,在 但是1=按钮(root,text=“START”,font=(“potra”,45),image=photoimage,fg=“white”,bg=“white”,component=“center”,activeforeground=“#13D678”,command=lambda:do(1)) 文件“C:\Users\SONY\Desktop\SPEECH\SPEECH-Copy.py”,第11行,在do中 self.img=PhotoImage(file=r“file/ball1.png”) AttributeError:“int”对象没有属性“img”
函数没有名称,缩进也不正常

你的函数没有名字,缩进也不正常。好吧,对不起,我很匆忙,所以我在这里写了错误的代码,但我现在编辑了问题,所以现在看看解决方案。不,这根本不起作用。请发布一个最简单的示例。你调用了
do(1)
,所以
do()
self
将传递整数1。因此,导致异常的行
self.img=PhotoImage(…)
将与
1.img=PhotoImage(…)
相同。