同时消息框
Simultaneous Message Boxes
所以我创建了一个事件驱动程序,当特定事件处理程序触发时,它会通过消息对话框提示用户。如果它们重合,是否有任何方法可以同时实例化这些消息对话框?
################################################################################
# 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()
################################################################################
################################################################################
################################################################################
它应该可以正常工作,请考虑以下内容
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()
这将每 3 秒打开一次消息框,而不管上一个消息框是否打开...
[编辑]
经过进一步讨论后,我会假设您的事件是您必须轮询的...您应该将轮询逻辑放在它自己的线程中
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()
[编辑 2]
在您编辑之后,我将做出另一个假设,即您的 watch
库会阻塞直到回调 returns ... 只需使用 wx.Callafter
如下
def old_callback(*args):
wx.MessageBox("Whatever","blah")
def callback(*args):
wx.CallAfter(old_callback,*args)
watch.bind(callback) # or however
这允许回调到 return 并 watch 继续收听......无论阻塞对话框如何
[编辑 3]
这里是一个使用 watch 库的完整示例
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()
然后只需在目录中添加和删除文件(注意新弹出窗口 可能 在现有弹出窗口后面弹出...但是如果您移动它们,您应该会看到所有这些弹出窗口)
所以我创建了一个事件驱动程序,当特定事件处理程序触发时,它会通过消息对话框提示用户。如果它们重合,是否有任何方法可以同时实例化这些消息对话框?
################################################################################
# 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()
################################################################################
################################################################################
################################################################################
它应该可以正常工作,请考虑以下内容
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()
这将每 3 秒打开一次消息框,而不管上一个消息框是否打开...
[编辑]
经过进一步讨论后,我会假设您的事件是您必须轮询的...您应该将轮询逻辑放在它自己的线程中
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()
[编辑 2]
在您编辑之后,我将做出另一个假设,即您的 watch
库会阻塞直到回调 returns ... 只需使用 wx.Callafter
如下
def old_callback(*args):
wx.MessageBox("Whatever","blah")
def callback(*args):
wx.CallAfter(old_callback,*args)
watch.bind(callback) # or however
这允许回调到 return 并 watch 继续收听......无论阻塞对话框如何
[编辑 3]
这里是一个使用 watch 库的完整示例
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()
然后只需在目录中添加和删除文件(注意新弹出窗口 可能 在现有弹出窗口后面弹出...但是如果您移动它们,您应该会看到所有这些弹出窗口)