Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

python 3.x - I am using tkinter ,I want to clear the canvas using a button(close_button) but the camera is always on and the button not doing anything

I am using Tkinter, I want to clear the canvas using a button(close_button) but the camera is always on and the button not doing anything I want the button to close the camera feed and reset the canvas so that the canvas becomes as it was before I opened the cam

    from tkinter import*
    import tkinter as tk
    from PIL import Image, ImageTk
    import cv2
    
    
    # new window function which will be called when button pressed
    class OpenCam():
        
        def __init__(self, window, cap):
            self.window = window
            self.cap = cap
            self.width = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)
            self.height = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
            self.interval = 20 # Interval in ms to get the latest frame
            # Create canvas for image
            self.canvas = tk.Canvas(self.window, width=self.width, height=self.height)
            self.canvas.place(x=100,y=100)
    
            # close Button need to close cam and reset canvas
            close_button = tk.Button(root, text="close", bg='black', fg='white', command=self.canvas.delete() )
            close_button.place(x=610,y=0)
            # Update image on canvas
            self.update_image()
        def update_image(self):
            # Get the latest frame and convert image format
            self.image = cv2.cvtColor(self.cap.read()[1], cv2.COLOR_BGR2RGB) # to RGB
            self.image = Image.fromarray(self.image) # to PIL format
            self.image = ImageTk.PhotoImage(self.image) # to ImageTk format
            # Update image
            self.canvas.create_image(0, 0, anchor=tk.NW, image=self.image)
            # Repeat every 'interval' ms
            self.window.after(self.interval, self.update_image)
    
    
    
    def new_window():
        OpenCam(root, cv2.VideoCapture(0)) 
        
    
    
    #create original window 
    root = tk.Tk()
    root.title("ADD CAM: ")
    canvas = tk.Canvas(root, height=1000, width=1000)
    canvas.pack()
    #create button that will be placed
    button = tk.Button(root, text="ADD CAM", bg='black', fg='white', command=new_window )
    button.place(x=0,y=0)
    
    
    root.mainloop()
question from:https://stackoverflow.com/questions/66061537/i-am-using-tkinter-i-want-to-clear-the-canvas-using-a-buttonclose-button-but

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...