简体   繁体   English

GUI冻结了其余的python代码

[英]GUI freezes the rest of python code

I am using wx gui in python. 我在python中使用wx gui。 and a opencv object detection. 和一个opencv对象检测。 GUI freezes the rest of the code when I run it GUI appears and when I close the window the code starts I found many askers about this problem> The solution was to create a thread to run the gui and start this thread in the main method but the same problem is still there here is the thread 当我运行GUI时,GUI冻结了其余的代码GUI出现了,当我关闭窗口的代码启动时,我发现许多询问此问题的人>解决方案是创建一个线程来运行gui并在main方法中启动该线程,但是仍然存在相同的问题

class GuIthread(threading.Thread):

def __init__ (self):
    threading.Thread.__init__(self)

def run(self):
    gettext.install("app") # replace with the appropriate catalog name\
    global View

    app = MyApp(0)
    app.MainLoop()

then here is the main method 然后这是主要方法

if __name__ == '__main__':
parser = OptionParser(usage = "usage: %prog [options] [filename|camera_index]")
parser.add_option("-c", "--cascade", action="store", dest="cascade", type="str", help="Haar cascade file, default %default", default = "../data/haarcascades/haarcascade_frontalface_alt.xml")
(options, args) = parser.parse_args()

cascade = cv.Load(options.cascade)
global Count

if len(args) != 1:
    parser.print_help()
    sys.exit(1)

input_name = args[0]
if input_name.isdigit():
    capture = cv.CreateCameraCapture(int(input_name))
else:
    capture = None

cv.NamedWindow("video", 1)

#size of the video
width = 600
height = 500

if width is None:
    width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH))
else:
    cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH,width)

if height is None:
    height = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))
else:
    cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT,height)

if capture:
    frame_copy = None
    thread1 = myThread()
    thread1.start()
    threadGUI = GuIthread()
    threadGUI.start()
    threadGUI.join()
    frame_copy = None
    t0=time.time()
    while True:
        global frame 
        frame = cv.QueryFrame(capture)
      #  cv.SaveImage('pic.jpg', frame)
        t1=time.time()
        if (t1-t0) >= 10:
            thread1.run()
            t0=t1
            SendPic (frame)
            Count=0                

        if not frame:
            cv.WaitKey(0)
            break
        if not frame_copy:
            frame_copy = cv.CreateImage((frame.width,frame.height),
                                        cv.IPL_DEPTH_8U, frame.nChannels)

        if frame.origin == cv.IPL_ORIGIN_TL:
            cv.Copy(frame, frame_copy)
        else:
            cv.Flip(frame, frame_copy, 0)

        detect_and_draw(frame_copy, cascade)

        if cv.WaitKey(10) >= 0:
            break
else:
    image = cv.LoadImage(input_name, 1)
    detect_and_draw(image, cascade)
    cv.WaitKey(0)

cv.DestroyWindow("video")

the gui appears and the video window but the code doesn't run except when I close the GUI window the video starts gui出现并且显示视频窗口,但是代码没有运行,除非我关闭GUI窗口,然后视频开始

What I tried is I added GUIthread.join () (before adding it segmentation error) I tried to make the code that runs the GUI in a method and call it the same result happens but without the video window appears. 我试过的是添加GUIthread.join()(在添加分段错误之前),我试图制作一种在方法中运行GUI的代码,并称其结果相同,但没有出现视频窗口。

First, @Mark is correct, you're doing this backwards from the normal convention, where the GUI runs in the main thread. 首先,@ Mark是正确的,您是从GUI在主线程中运行的常规约定中倒过来的。 Your code is blocking because you're doing this 您的代码正在阻止,因为您正在执行此操作

threadGUI = GuIthread()
threadGUI.start()
threadGUI.join()  # This blocks until threadGUI is complete.

The threadGUI.join() call will make your program block until the app.MainLoop() completes, which won't happen until you close the GUI. 调用threadGUI.join()将使您的程序处于阻塞状态,直到app.MainLoop()完成为止,直到关闭GUI才会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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