RuntimeError: Too early to create image: no default root window

Viewed 180

I was trying to make a Tkinter application with a video and a live matplotlib graph. The programs runs fine while using a webcam but stops responding at the end of video file. It raise the below errors after terminating the program.

raise RuntimeError(f"Too early to {what}: no default root window")

RuntimeError: Too early to create image: no default root window

Exception ignored in: <function PhotoImage.del at 0x000001F9EB491AF0>

The creation of window and widgets is done using the following code:

#Set up GUI
window = tk.Tk()  #Makes main window
window.wm_title("Social distancing detector")
window.config(background="#FFFFFF")

#Graphics window
imageFrame = tk.Frame(window, width=600, height=1000)
imageFrame.grid(row=0, column=0, padx=10, pady=2)

display1 = tk.Label(imageFrame)
display1.grid(row=1, column=0, padx=10, pady=2)  #Display 1

# matplotlib part
x=[]
y=[]

fig = Figure(figsize=(3, 2), dpi=100)
# fig = plt.axes()

a = fig.add_subplot()
a.set_xlabel('Frame no.')
a.set_ylabel('Number of violations')

canvas = FigureCanvasTkAgg(fig, master=window)
canvas.draw()
canvas.get_tk_widget().grid(row=3, column=0, ipadx=40, ipady=20)

# inside the while loop
if args["display"] > 0:
            # show the output frame
            frame1=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
            frame1=cv2.resize(frame1, (500,350), interpolation=cv2.INTER_AREA)
            img_update = ImageTk.PhotoImage(Image.fromarray(frame1))
            display1.configure(image=img_update)
            display1.image=img_update
            display1.update()

Link for the complete code: https://pastebin.com/GFUKvXYu Complete list of errors: https://pastebin.com/xTF4Zhjv How do I correct these errors and what is the right syntax? Thank you for your reply

0 Answers
Related