首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每隔指定时间调用wxPython.NotificationMessage

每隔指定时间调用wxPython.NotificationMessage
EN

Stack Overflow用户
提问于 2016-07-29 02:06:58
回答 2查看 410关注 0票数 0

我已经编写了一个函数来检查每个用户指定的更新时间,如果有任何更新的话,可以显示关于它的通知消息。我的代码:

代码语言:javascript
复制
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()代替了打印指令进行测试,一切正常,所以调用该函数会带来麻烦。

EN

回答 2

Stack Overflow用户

发布于 2016-07-29 07:01:49

问题在于您的show_notification()函数。这句话:

代码语言:javascript
复制
notification_bubble.MainLoop()

只有在窗口关闭后才能完成。因此,一旦调用了show_notification()函数,就会进入一个循环,等待用户关闭窗口。但如果他这么做了,程序就会退出。因此,不可能多次调用show_notification()。

关于wxPython ()的MainLoop文档

我对wxPython不太了解,但我希望您可能不得不进入线程化。

线程教程

您将为每个通知启动一个新线程,如下所示:

代码语言:javascript
复制
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

这样,每个通知都是一个后台进程,而您的主程序可以继续运行。

希望我能帮上忙。祝您今天愉快!

票数 1
EN

Stack Overflow用户

发布于 2016-07-30 17:03:54

你得把整件事都打包到mainloop

这个简单的应用程序创建一个阻塞消息,如果你想要一个非阻塞或自我取消消息,那是一个完全不同的鱼缸!

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38649436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档