2016-07-29 2 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 

내 문제는 단지 통보 한 시간을 표시한다는 것입니다 그것은 업데이 트 기능을 실행을 포함하여 아무 것도 수행하지 않습니다 그 후. 나는 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() 
관련 문제