Search code examples
pythontkinterwxpythonmessagebox

Simultaneous Message Boxes


So I have created an event driven program that is supposed to prompt users when triggered by the specific event handler with message dialogs. Is there any way to instantiate these message dialogs simultaneously in the event that they coincide?

################################################################################
# Create Listener ##############################################################
################################################################################

# listens to a local directory for files

watch = Watcher(LOCAL_DIRECTORY, callback)

# set the appropriate flags for a listener

watch.flags = FILE_NOTIFY_CHANGE_FILE_NAME

################################################################################
# Start Listener ###############################################################
################################################################################

watch.start()

################################################################################
################################################################################
################################################################################

Solution

  • it should just work, consider the following

    import wx
    
    def handle(e):
        wx.MessageBox("Hello","world")
    
    a = wx.App(redirect=False)
    t = wx.Timer()
    t.Bind(wx.EVT_TIMER,handle)
    t.Start(3000)
    f = wx.Frame(None)
    a.MainLoop()
    

    this will open the message box every 3 seconds regardless of the previous one being open...

    [edit]

    after further discussion I will make an assumtion that your event is one you must poll for ... you should put the polling logic in its own thread

    def poll_data(data_callback):
        def check_new_event():
            if do_something_to_check():
               wx.CallAfter(data_callback) # this will cause the callback to be executed in the main thread (All gui updates *should* be done in the main thread)
        while True:
           check_new_event()
           sleep(1) # force thread to surrender control for a while
    
    def on_callback(*args,**kwargs):
        wx.MessageBox("Some Message","Title")
    
    def my_app():
        a= wx.App(redirect=False)
        f=wx.Frame(None)
        th = threading.Thread(target=poll_data,args=(on_callback,))
        th.start()
        a.MainLoop()
    

    [edit 2]

    after your edit I will make another assumption that your watch library blocks until callback returns ... just use wx.Callafter like follows

    def old_callback(*args):
        wx.MessageBox("Whatever","blah")
    def callback(*args):
        wx.CallAfter(old_callback,*args)
    
    watch.bind(callback) # or however
    

    this allows the callback to return and watch to continue listening ... regardless of a blocking dialog

    [edit 3]

    here is a complete example using watch library

    import watcher,wx
    a = wx.App(redirect=False)
    TARGET_DIR="/py_exp/testing"
    def real_callback(*args):
        wx.MessageBox(str(args),"QQQQ")
    def callback(*args):
        wx.CallAfter(real_callback,*args)
    w = watcher.Watcher(TARGET_DIR, callback)
    w.flags = watcher.FILE_NOTIFY_CHANGE_FILE_NAME
    w.start()
    f = wx.Frame(None)
    a.MainLoop()
    

    then just add and remove files from the directory (note the new popups might popup behind the existing one...but if you move them you should see all of them)