0

I have looked up everywhere but not able to resolve the issue.
My error is AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
I have this error when I start the script.
I have this error when the script goes to the WhileIntro function.

I will show my full code:

import tkinter as tk
import cv2
from PIL import ImageTk, Image
from pynput import keyboard
from threading import Thread
from time import sleep
from pyautogui import position


class IntroApp:
    def __init__(self, master=None):
        # build ui
        self.intro = tk.Tk() if master is None else tk.Toplevel(master)
        self.IntroVideo = tk.Label(self.intro)
        self.IntroVideo.place(anchor='center', relx='0.5',
                              rely='0.5', x='0', y='0')
        self.enter = tk.Label(self.intro)
        self.enter.configure(
            background='#e74c3c', font='{Arial} 25 {}', foreground='#c0392b', justify='center')
        self.enter.configure(padx='10', pady='10',
                             text='Waiting')
        self.enter.place(anchor='nw', relx='0.025', rely='0.025', x='0', y='0')

        self.screen_width = self.intro.winfo_screenwidth()
        self.screen_height = self.intro.winfo_screenheight()

        self.intro.configure(background='#f6f6f6')
        self.intro.geometry(f'{self.screen_width}x{self.screen_height}+0+0')
        self.intro.resizable(False, False)
        self.intro.title('Locked - RED LINE')

        self.destroy = False
        self.intro.protocol("WM_DELETE_WINDOW", self.DestroyGui)

        self.intro.attributes('-fullscreen', True)

    def DestroyGui(x):
        pass

    def PressEnter(self, key):
        global video
        if key == keyboard.Key.enter:
            video.release()
            self.intro.destroy()

    def CheckEnter(self):
        sleep(1)
        self.enter.configure(text='Press ENTER to continue')

        self.listener = keyboard.Listener(on_press=self.PressEnter)
        self.listener.start()

    def Afk(self):
        OldX, OldY = position()
        while True:
            sleep(30)
            NewX, NewY = position()

            if OldX == NewX and OldY == NewY:
                print('Detected')
                self.listener.stop()
                self.destroy == True
                break

            OldX, OldY = NewX, NewY

        IntroApp().run()

    # Error fuction
    def WhileIntro(self):
        global video
        ret, frame = video.read()
        if ret == True:
            frame = cv2.resize(frame, (self.screen_width, self.screen_height))
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

            # Error line
            image = ImageTk.PhotoImage(Image.fromarray(frame))

            self.IntroVideo.configure(image=image)
            self.IntroVideo.image = image
            self.intro.after(10, self.WhileIntro)
        elif ret == False and self.destroy == False:
            video.release()
            self.IntroVideo.configure(text='Loading')
            video = cv2.VideoCapture('D:\\Lock\\RedLine.mp4')
            sleep(1)
            self.IntroVideo.configure(text='')
            self.WhileIntro()
        else:
            video.release()

    def LoadIntro(self):
        global video
        video = cv2.VideoCapture('D:\\Lock\\RedLine.mp4') # Show the video
        self.WhileIntro()

    def run(self):
        Thread(target=self.CheckEnter).start()
        Thread(target=self.LoadIntro, daemon=True).start()
        Thread(target=self.Afk).start()
        self.intro.mainloop()


if __name__ == '__main__':
    app = IntroApp()
    app.run()

Console:

Exception in thread Thread-6:
Traceback (most recent call last):
  File "C:\Users\KevinTranPRO\AppData\Local\Programs\Python\Python39\lib\threading.py", line 973, in _bootstrap_inner
    self.run()
  File "C:\Users\KevinTranPRO\AppData\Local\Programs\Python\Python39\lib\threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "d:\Lock\main.py", line 94, in LoadIntro
    self.WhileIntro()
  File "d:\Lock\main.py", line 76, in WhileIntro
    image = ImageTk.PhotoImage(Image.fromarray(frame))
  File "C:\Users\KevinTranPRO\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\ImageTk.py", line 112, in __init__
    self.__photo = tkinter.PhotoImage(**kw)
  File "C:\Users\KevinTranPRO\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 4064, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\KevinTranPRO\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 4009, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
RuntimeError: main thread is not in main loop
Exception ignored in: <function PhotoImage.__del__ at 0x000002C3D2AB1280>
Traceback (most recent call last):
  File "C:\Users\KevinTranPRO\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\ImageTk.py", line 118, in __del__
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

Sorry, my English is bad.

KevinTran
  • 25
  • 9

0 Answers0