How to fix wxpython tray bar right click not working while showing opencv video capture frame?

Viewed 68

Im using a wxpython tray bar icon menu. Right clicks on the icon (to open the menu) stop working when showing frames of an opencv webcam video capture at the same time.

In the background runs processing of the captured live webcam video stream (mostly facial landmarks detection with dlib).

All works fine unless I start showing the captured video (for debugging and performance control). In that case, right clicks on the menu stop working the moment the video stream starts showing. During the short time lag after starting the script and the video starting to show, the menu still shows on right clicks. Also, if I open the menu with a right click before the video starts showing, the opened menu is still usable after the video starts playing.

Its seems as soon as the video starts showing, right clicks on the tray icon stop working while everything else still works.

import settings
import webcam_stream


import threading
import wx.adv
import wx

settings.initialize()

def threadFunc():
    webcam_stream.webcamStream()

print("Starting Stream Thread")
stream_thread = threading.Thread(target=threadFunc)
stream_thread.start()
settings.processing_stopped = False

print("Continuing execution")

TRAY_TOOLTIP = 'Name' 
TRAY_ICON = 'iris_icon_16.png' 



def create_menu_item(menu, label, func):
    item = wx.MenuItem(menu, -1, label)
    menu.Bind(wx.EVT_MENU, func, id=item.GetId())
    menu.Append(item)
    return item


class TaskBarIcon(wx.adv.TaskBarIcon):
    def __init__(self, frame):
        self.frame = frame
        super(TaskBarIcon, self).__init__()
        self.set_icon(TRAY_ICON)
        self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)

    def CreatePopupMenu(self):
        menu = wx.Menu()

        create_menu_item(menu, 'Hello World', self.on_hello)
        menu.AppendSeparator()
        create_menu_item(menu, 'Exit', self.on_exit)
        return menu

    def set_icon(self, path):
        icon = wx.Icon(path)
        self.SetIcon(icon, TRAY_TOOLTIP)

    def on_left_down(self, event):
        print ('Tray icon was left-clicked.')

    def on_hello(self, event):
        print ('Hello, world!')

    def on_exit(self, event):
        wx.CallAfter(self.Destroy)
        self.frame.Close()
        settings.processing_stopped = True
        return 

class App(wx.App):
    def OnInit(self):
        frame=wx.Frame(None)
        self.SetTopWindow(frame)
        TaskBarIcon(frame)
        return True

def main():
    app = App(False)
    app.MainLoop()


if __name__ == '__main__':
    main()

print(settings.processing_stopped)    
print("end of interface program")

Here is the part of the webcam_stream.py file which shows the video capture:

        if settings.show_webcam_stream == True:
            # show the frame
            cv2.imshow("Frame", frame)
            key = cv2.waitKey(1) & 0xFF

Setting settings.show_webcam_stream to False resolves the error, however I am unable to see the capture for debugging. Also stopping to show the capture during execution of the program immediately makes the menu to show again on right click.

I want the tray icon menu to open on right clicks, even if the video stream is showing.

Does somebody have an idea why the opencv frame is messing with the right click menu?

0 Answers
Related