I have a MainWindow gui of 800x480 size with Python Tkinter. The gui has a create_image background and two create_window=button buttons.

One of the buttons launches an Opencv4 CamStream canvas deleting the MainWindow canvas. After the user clicks mouse 1 anywhere on the video, the video canvas gets destroyed and video device is released.

My problem is "returning" back to the original canvas. The original canvas MainWindow is defined in the main program and CamStream is imported. When I try to call MainWindow inside a CamStream method, I get an error saying MainWindow is not defined (because im calling it inside an imported class method?).

Maybe there is a better way of doing this? I thought imported code is still ran in the main program and therefore main program classes would be known to it.

Simplified main program:

import tkinter as tk
from PIL import Image, ImageTk
import numpy as np
import cv2
from videocapture import CamStream

root = tk.Tk()

class MainWindow:
    def __init__(self, window):
        self.window = window
        self.canvas = tk.Canvas(window, width=800, height=480)
        self.canvas.pack()

        camButton = tk.Button(window, text = 'camera', command = lambda: self.canvas.destroy() | CamStream(window))
        self.canvas.create_window(300,235, window=camButton, anchor = 'center')

MainWindow(root)
root.mainloop()

Simplified imported videocapture code:

import tkinter as tk
import cv2
from PIL import Image, ImageTk

class CamStream:
    def __init__(self, window):
        self.canvas = tk.Canvas(window, width=800, height=480)
        self.canvas.pack()

        # And other stuff

    def close_video(self, event):
        if MyVideoCapture().vid.isOpened:
            self.vid.vid.release()
            self.canvas.destroy()
            MainWindow(root)          #####Problem creating main canvas  ######

0 Answers