2016-07-29 10 views
0

私は、ユーザが指定したすべての時刻をチェックし、もしあればそれに関する通知メッセージを表示する関数を作成しました。 マイコード:指定された時間ごとにwxPython.NotificationMessageを呼び出す

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 

私の問題は、それが唯一の通知1時間を表示し、その後、それは更新機能を実行するなど、何かを、行いませんということです。私はcall_function()printの指示をテストのために置き換えました。すべてうまくいきました。その機能を呼び出すことは問題を引き起こしています。

答えて

1

問題はshow_notification()関数です。行:

notification_bubble.MainLoop() 

は、ウィンドウが閉じられると終了します。だから、あなたのshow_notification()関数が呼び出されると、ユーザがウィンドウを閉じるのを待つループに入ります。しかし、もしそうすれば、プログラムは終了する。したがって、show_notification()を複数回呼び出すことは不可能です。

wxPython Documentation on MainLoop()

私はwxPythonをについて多くを知らないが、私はあなたがスレッドに入るために必要がある場合がありますことを期待しています。

Threading Tutorial

あなたは、このような各通知のための新しいスレッドを起動します:あなたのメインプログラムが実行し続けることができますが

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

あなたは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() 
関連する問題