程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了“PhotoImage Error”对象没有属性 PhotoImage error 当与类和对象一起使用时大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决“PhotoImage Error”对象没有属性 PhotoImage error 当与类和对象一起使用时?

开发过程中遇到“PhotoImage Error”对象没有属性 PhotoImage error 当与类和对象一起使用时的问题如何解决?下面主要结合日常开发的经验,给出你关于“PhotoImage Error”对象没有属性 PhotoImage error 当与类和对象一起使用时的解决方法建议,希望对你解决“PhotoImage Error”对象没有属性 PhotoImage error 当与类和对象一起使用时有所启发或帮助;

我正在使用 OOP 概念在 python 中开发名为 颜色检测 的项目。在这个项目中,用户必须通过从系统中选择图像或从网络摄像头拍照来输入图像。 我几乎完成了项目,但出现运行时错误。 这是我的代码块:

@H_874_7@self.root = Tk()
....
....
self.capture_frame = Frame(self.root,wIDth=750,height=550,bg='black')
....
....

def openCamera(self,capture_framE):
    self.hIDe_all_frames()
    self.capture_frame.pack()
    
    #button
    snapshoT_Btn = button(capture_frame,text="CAPTURE",wIDth=13,bg = '#1cbab5',fg = 'black',Font = ('dsrom 10',12,'bold'),command = self.snapshot).place(x=170,y=515)
    
    BACK_btn = button(capture_frame,text="BACK",bg = 'red',command = lambda :self.firstFrame(self.image_SELEction_framE)).place(x=400,y=515)
    
    #Labels
    self.cam_area = Label(capture_frame,bg = 'black')
    self.cam_area.place(x=50,y=20)  
    
    self.cap = cv.VIDeoCapture(0)
    
    #while to open camra 
    while True:
        self.img = self.cap.read()[1]
        self.img = cv.flip(self.img,1)
        self.img1 = cv.cvtcolor(self.img,cv.color_BGR2RGB)
        self.img = ImageTk.PhotoImage(image = PIl.Image.fromarray(self.img1))
        self.cam_area['image'] = self.img
        self.root.update()

    self.cap.release()
    cv.destroyAllwindows()

当单独运行而不创建任何函数或方法时,此代码运行良好。 当我尝试将它包含在我的项目中时它运行良好,甚至它捕获图像并将其保存在系统中。 但是当我想关闭相机并尝试重新打开它时出现错误:

@H_874_7@[ WARN:0] global /tmp/pip-req-build-j6tkfxml/opencv/modules/vIDeoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/vIDeo0): can't open camera by index
Exception in Tkinter callBACk

TraceBACk (most recent call last):
file "/usr/lib/python3.9/tkinter/__init__.py",line 1892,in __call__
return self.func(*args)
file "/home/safehouse/Desktop/colorDetection.py",line 145,in <lambda>
Font = ('dsrom 10',10,command=lambda 
:self.capturecmd(self.capture_framE)).place(x=450,y=130)
file "/home/safehouse/Desktop/colorDetection.py",line 205,in capturecmd
self.img1 = cv.cvtcolor(self.img,cv.color_BGR2RGB)
cv2.error: OpenCV(4.5.1) /tmp/pip-req-build-j6tkfxml/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion Failed) !_src.empty() in function 'cvtcolor'

当我捕获然后关闭应用程序而不重新启动相机时,我收到以下错误

@H_874_7@Exception in Tkinter callBACk
TraceBACk (most recent call last):
file "/usr/lib/python3.9/tkinter/__init__.py",in __call__
  return self.func(*args)
file "/home/safehouse/Desktop/colorDetection.py",in <lambda>
  Font = ('dsrom 10',line 206,in capturecmd
   self.img = ImageTk.PhotoImage(master = self.cam_area,image = Image.fromarray(self.img1))
file "/usr/lib/python3/dist-packages/PIL/ImageTk.py",line 112,in 
__init__
   self.__photo = tkinter.PhotoImage(**kw)
file "/usr/lib/python3.9/tkinter/__init__.py",line 4064,in __init__
   Image.__init__(self,'photo',name,cnf,master,**kw)
file "/usr/lib/python3.9/tkinter/__init__.py",line 4009,in __init__
   self.tk.call(('image','create',imgtype,) + options)
_   tkinter.TclError: can't invoke "image" command: application has been destroyed
Exception ignored in: <function PhotoImage.__del__ at 0x7f866f265ca0>
TraceBACk (most recent call last):
file "/usr/lib/python3/dist-packages/PIL/ImageTk.py",line 118,in __del__
   name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

当我使用 root = tk() 我得到 Photoimage() 属性错误但是当我使用

@H_874_7@root = toplevel()

我摆脱了上面的照片图像错误,但后来又出现了新错误:

@H_874_7@Exception in Tkinter callBACk
TraceBACk (most recent call last):
file "/usr/lib/python3.9/tkinter/__init__.py",command=lambda :self.capturecmd(self.capture_framE)).place(x=450,line 207,in capturecmd
  self.cam_area['image'] = self.img
file "/usr/lib/python3.9/tkinter/__init__.py",line 1657,in __setitem__
  self.configure({key: value})
file "/usr/lib/python3.9/tkinter/__init__.py",line 1646,in configure
  return self._configure('configure',kw)
file "/usr/lib/python3.9/tkinter/__init__.py",line 1636,in _configure
  self.tk.call(_flatten((self._w,cmd)) + self._options(cnf))
_tkinter.TclError: invalID command name ".!toplevel.!frame3.!label"

我已经尝试过并到处搜索并尝试使用不@R_718_11197@的软件包,但当时没有真正起作用。 我所有的项目都运行良好,相机也在拍照并保存。但是由于上述错误,我无法在不关闭整个项目的情况下重新启动相机。由于 Photoimage 错误,它删除了相机屏幕。

如果有人能帮忙,那将是一个很大的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的“PhotoImage Error”对象没有属性 PhotoImage error 当与类和对象一起使用时全部内容,希望文章能够帮你解决“PhotoImage Error”对象没有属性 PhotoImage error 当与类和对象一起使用时所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。