我已经编写了一个函数来检查每个用户指定的更新时间,如果有任何更新的话,可以显示关于它的通知消息。我的代码:
def show_notification():
notification_bubble = wx.App()
wx.adv.NotificationMessage("", "sample notification").Show()
notification_bubble.MainLoop()
def update():
# retrieve var from newest_porcys_url_imported
first_porcys_url = newest_porcys_url_imported()
# retrieve var from newest_pitchfork_url_imported
first_pitchfork_url = newest_pitchfork_url_imported()
# fetch newest review url with get_porcys_review_url
get_latest_porcys_url_ = get_porcys_review_url()
get_latest_porcys_url = get_latest_porcys_url_[0]
# fetch newest review url with get_pitchfork_review_url
get_latest_pitchfork_url_ = get_pitchfork_review_url()
get_latest_pitchfork_url = get_latest_pitchfork_url_[0]
a = first_porcys_url + ' ' + get_latest_porcys_url
b = first_pitchfork_url + ' ' + get_latest_pitchfork_url
get_datetime = datetime.now()
hour = str(get_datetime.hour)
minutes = str(get_datetime.minute)
f = open('log.txt', 'a')
f.write(hour + ':' + minutes + ' ' + a + ' ' + b + '\n')
f.close()
if first_porcys_url != get_latest_porcys_url or first_pitchfork_url != get_latest_pitchfork_url:
print('new reviews')
f = open('new reviews.txt', 'a')
f.write(hour + ':' + minutes + ' ' + a + ' ' + b + '\n')
f.close()
return True
else:
show_notification()
return False我的问题是,它只显示了一次通知,之后就什么也不做了,包括执行更新函数。我用call_function()代替了打印指令进行测试,一切正常,所以调用该函数会带来麻烦。
发布于 2016-07-29 07:01:49
问题在于您的show_notification()函数。这句话:
notification_bubble.MainLoop()只有在窗口关闭后才能完成。因此,一旦调用了show_notification()函数,就会进入一个循环,等待用户关闭窗口。但如果他这么做了,程序就会退出。因此,不可能多次调用show_notification()。
我对wxPython不太了解,但我希望您可能不得不进入线程化。
线程教程
您将为每个通知启动一个新线程,如下所示:
import threading
def show_notification():
notification_bubble = wx.App()
wx.adv.NotificationMessage("", "sample notification").Show()
notification_bubble.MainLoop()
def update():
# retrieve var from newest_porcys_url_imported
first_porcys_url = newest_porcys_url_imported()
# retrieve var from newest_pitchfork_url_imported
first_pitchfork_url = newest_pitchfork_url_imported()
# fetch newest review url with get_porcys_review_url
get_latest_porcys_url_ = get_porcys_review_url()
get_latest_porcys_url = get_latest_porcys_url_[0]
# fetch newest review url with get_pitchfork_review_url
get_latest_pitchfork_url_ = get_pitchfork_review_url()
get_latest_pitchfork_url = get_latest_pitchfork_url_[0]
a = first_porcys_url + ' ' + get_latest_porcys_url
b = first_pitchfork_url + ' ' + get_latest_pitchfork_url
get_datetime = datetime.now()
hour = str(get_datetime.hour)
minutes = str(get_datetime.minute)
f = open('log.txt', 'a')
f.write(hour + ':' + minutes + ' ' + a + ' ' + b + '\n')
f.close()
if first_porcys_url != get_latest_porcys_url or first_pitchfork_url != get_latest_pitchfork_url:
print('new reviews')
f = open('new reviews.txt', 'a')
f.write(hour + ':' + minutes + ' ' + a + ' ' + b + '\n')
f.close()
return True
else:
notificationThread = threading.Thread(target=show_notification)
# Prepare the thread
notificationThread.daemon = True
# Make shure it will run in the background
notificationThread.start()
# Start the thread
return False这样,每个通知都是一个后台进程,而您的主程序可以继续运行。
希望我能帮上忙。祝您今天愉快!
发布于 2016-07-30 17:03:54
你得把整件事都打包到mainloop里
这个简单的应用程序创建一个阻塞消息,如果你想要一个非阻塞或自我取消消息,那是一个完全不同的鱼缸!
import wx
import time
def Alerts(x):
#Do your stuff here rather than sleeping
time.sleep(2)
dlg = wx.MessageBox("New Message "+str(x),"My message heading",wx.OK | wx.ICON_INFORMATION)
if __name__ == "__main__":
A1 = wx.App()
#This could be a while True loop
for x in range(10):
Alerts(x)
A1.MainLoop()https://stackoverflow.com/questions/38649436
复制相似问题