无法调用“按钮”命令:应用程序已被破坏

时间:2019-01-22 11:37:15

标签: python opencv tkinter raspberry-pi

这是我的Opencv代码。 如果超过3秒未检测到眼睛,则执行弹出功能。 如果再次执行弹出功能,但超过3秒钟没有再次检测到眼睛, “无法调用“按钮”命令:应用程序已准备好” 发生错误。 第19和63行发生错误(def弹出,否则〜如果Sec = 3:弹出) 你能告诉我为什么吗?

正在使用翻译器。对不起,谢谢你。祝您工作顺利。

from picamera.array import PiRGBArray

from tkinter import *

from tkinter import messagebox

import RPi.GPIO as gpio

from picamera import PiCamera

import time

import cv2

import threading





window = Tk()

window.title("Message Box")

window.geometry("200x100+0+0")

window.resizable(0,0)



Sec = 0



def popup():

        test_button = Button(window, text = "Open Your Eyes!!",)

        test_button.pack()

        window.mainloop()



RELAY = 17

gpio.setmode(gpio.BCM)

gpio.setup(RELAY, gpio.OUT, initial=gpio.LOW)

camera = PiCamera()

camera.resolution = (640, 480)

camera.framerate = 40

rawCapture = PiRGBArray(camera, size=(640, 480))



faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

eyesCascade = cv2.CascadeClassifier("haarcascade_eye_tree_eyeglasses.xml")



time.sleep(0.1)



for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):

    image = frame.array



    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(50, 50))



    for (x, y, w, h) in faces:

        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

        roi_gray = gray[y:y + h, x:x + w]

        roi_color = image[y:y + h, x:x + w]

        eyes = eyesCascade.detectMultiScale(roi_gray)



        for (ex, ey, ew, eh) in eyes:

                print (eyes)

                cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (100, 255, 255), 2)





        if len(faces) >= 1 and len(eyes) >= 2:

            Sec = 0

            # cv2.putText(image, 'WARNING!', (10, 500), cv2.FONT_HERSHEY_SIMPLEX, 4, (255, 255, 255), 2)

            gpio.output(RELAY, False)



        else:

            Sec += 1

            print(str(Sec) + " Sec")

            time.sleep(1)

            if Sec == 3:

                popup()

            gpio.output(RELAY, True)



    cv2.imshow("OPEN CV", image)

    key = cv2.waitKey(1) & 0xFF

    rawCapture.truncate(0)



    if key == ord("q"):

        break

gpio.output(RELAY, False)

gpio.cleanup() 

cv2.destroyAllWindows()

错误是

Tkinter回调中的异常

回溯(最近通话最近一次):

在“ 调用

”中的文件“ /usr/lib/python3.5/tkinter/init.py”,第1562行
return self.func(*args)

文件“ /home/pi/opencv/rpi_eye_blink_detector-master/detector.py”,第19行,位于popup_quit

window.destroy()

1922行中的文件“ /usr/lib/python3.5/tkinter/init.py”

self.tk.call('destroy', self._w)

_tkinter.TclError:无法调用“销毁”命令:应用程序已被破坏

1 个答案:

答案 0 :(得分:0)

最后5行呢? 应该是这样吗? 试试

if key == ord("q"): 
            gpio.output(RELAY, False)   
            gpio.cleanup()     
            cv2.destroyAllWindows()            
            break