简体   繁体   中英

Can't Delete TkInter Label using OpenCV

What I'm Trying to do:

Hello. I'm creating a GUI that contains Camera and Map View. Firstly, I would like to control the GUI with using "F1" and "F2".

The Problem:

My problem is that I can't delete/remove the label when I pressed F2. There is no problem about pressing F1. I tried to use packname.pack() then packname.forget_pack() but nothing happened. Furthmore, When I try packname.destroy(), the frame stops but the screen is still there.(On GUI)

How can I solve that problem?

Code:

 import sys

if sys.version_info[0] == 2:
    import Tkinter as tk
else:
    import tkinter as tk

from PIL import ImageTk
import cv2
from PIL import Image


class UI(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        self.maxsize(width = 500, height = 500)
        self.minsize(width = 500 , height = 500)
        self.cap = cv2.VideoCapture(1)
        self.geometry("500x500")
        self.title('Test1')
        self.camera_view = tk.Frame(self)
        self.camera_on= self.bind("<F1>", self.camera)
        self.bind("<F2>", self.remove_camera)


    def camera(self, event=None):

        _, frame = self.cap.read()
        frame = cv2.flip(frame, 1)
        frame = cv2.resize(frame, (0, 0), fx=0.75, fy=0.65)
        cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
        img = Image.fromarray(cv2image)
        imgtk = ImageTk.PhotoImage(image=img)
        self.camera_label = tk.Label(image=imgtk)
        self.camera_label.imgtk = imgtk
        self.camera_label.after(10, self.camera)
        self.camera_label.place(x=10, y=100)


    def remove_camera(self, event=None):
        self.camera_label.place_forget()

UI().mainloop()  

Solution:

We cannot use bind for streaming camera function. We need a control function for adding or removing. Here is the example:

import sys

if sys.version_info[0] == 2:
    import Tkinter as tk
else:
    import tkinter as tk

from PIL import ImageTk
from goompy import GooMPy
import cv2
import numpy as np
from PIL import Image
import PIL



class UI(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        self.maxsize(width = 800, height = 640)
        self.minsize(width = 800 , height = 640)
        self.cap = cv2.VideoCapture(1)
        self.geometry("800x640")
        self.title('Testing1')
        self.arka_plan = tk.Frame(self, bg='#2a2a2a')
        self.arka_plan.pack(fill='both', expand='yes')
        ##self.logo_label.place(x=150, y=0)
        self.camera_label = tk.Label(self)
        self.bind("<F1>", self.control)
        self.bind("<F2>", self.control)
        self.bind("<F3>", self.control)


    def camera(self):

        _, frame = self.cap.read()
        frame = cv2.flip(frame, 1)
        frame = cv2.resize(frame, (0, 0), fx=1.24, fy=1.32)
        cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
        img = Image.fromarray(cv2image)
        imgtk = ImageTk.PhotoImage(image=img)
        self.camera_label.imgtk = imgtk
        self.camera_label.configure(image=imgtk)
        self.camera_label.after(10, self.camera)


    def control(self, event = None):
        if event.keysym == 'F1': ##kamera calistirilirsa
            self.camera_label.place(x = 0, y = 0)

        elif event.keysym == 'F2': 

            self.camera_label.place_forget()

    def check_quit(self, event):
        pass

app = UI()
app.camera()
app.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM