PotatoBox
PotatoBox

Reputation: 608

Calling wxPython.NotificationMessage every specified time

I have written function to check every user-specified time for updates and if there any, to display notification message about it. My code:

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

My problem is that it only displays the notification one time and after that it doesn't do anything, including executing update function. I replaced call_function() with print instruction for testing and everything worked fine, so calling that function is causing troubles.

Upvotes: 0

Views: 448

Answers (2)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

You would have to wrap the whole thing into the mainloop:
This simple app creates a blocking message, if you want a non-blocking or self cancelling message, that is a completely different kettle of fish!

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()

Upvotes: 1

Readme
Readme

Reputation: 306

The Problem is your show_notification() function. The line:

notification_bubble.MainLoop()

will only finish once the window is closed. So what's happening is that once your show_notification() function is called, you go into a loop waiting for the user to close the window. But if he does, then the program exits. So it is impossible to call show_notification() more than once.

wxPython Documentation on MainLoop()

I don't know much about wxPython but I expect that you might have to get into threading.

Threading Tutorial

You would start a new thread for each notification like this:

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

That way, each notification is a background process while your main program can keep on running.

I hope I could help. Have a nice day!

Upvotes: 1

Related Questions