Python Notify2不显示图像

时间:2019-08-29 07:15:55

标签: python notify

我正在使用notify2模块在Linux中显示桌面通知。该代码可从互联网获取最新的地震信息,并显示桌面通知。

当我在spyder3和带有python3 earthquake.py的终端上运行通知时,代码可以完美地显示通知。我已将代码注册到启动应用程序中。当我登录计算机python3 "/home/ali/Programlarım/Python/My Projects/earthquake.py"时,命令会运行,并且代码会自动运行。它从网上获取信息,并显示通知。但是通知中没有图标。如何解决这个问题?

import requests
import time as t
import notify2 as not2
from os import getcwd
wdir=getcwd()
url="http://www.koeri.boun.edu.tr/scripts/lst2.asp"
ico1=wdir+"/files/earthquake.ico"
ico2=wdir+"/files/network-error.png"

def get_earthquake():        
    #get latest earthquakes from http://www.koeri.boun.edu.tr/scripts/lst2.asp
    url="http://www.koeri.boun.edu.tr/scripts/lst2.asp"
    html=["No internet connection"]
    ok=0

    try:
        html=requests.get(url).text
        if "Dicle University" in html:
            ok=1 
    except:
        print("Internet Connection Error")
        ok=1

    #if there is no error ok variable is equal to 0 and the program works correctly
    if ok==0: 
        #clear the text from unnecessary parts. Such as header, banner etc...
        x=(html.index("--------------"),html.index('</pre>'))
        html=html[x[0]:x[1]].splitlines()[1:500]

        #split the data into date, location and magnitude 
        html_=[]
        for i in html:
            html_.append(i.split(" "))

        html=[]

        for data in html_:
            scl=[]#scarp list
            for i in range(len(data)):
                if data[i]=="":
                    scl.append(i)
            scl.reverse()
            for i in scl:
                data.pop(i)
            html.append(data)
        del html_
        return html   
    else:
        print("Connect to internet") 
        return ["No internet connection"]



'''Main loop'''

while True:
    html=get_earthquake()
    if html[0]=="No internet connection":
        not2.init("Deprem")
        n=not2.Notification("Deprem","Deprem bilgilerini almak için internete bağlanın.",ico2)
        n.show()
    else:
        try:
            if not latest_earthquake==html[0]:
                not2.init("Deprem")
                output=html[0][8]+html[0][9]+" bölgesinde "+html[0][6]+" şiddetinde deprem."
                n=not2.Notification("Deprem",output,ico1)
                n.show()
                print("ok")
        except:
            not2.init("Deprem")
            output=html[0][8]+html[0][9]+" bölgesinde "+html[0][6]+" şiddetinde deprem."
            n=not2.Notification("Deprem",output,ico1)
            n.show()               
    t.sleep(60)
    latest_earthquake=html[0]

1 个答案:

答案 0 :(得分:1)

ico1真的存在吗?这行引起了我的注意:wdir=getcwd()

调试,请尝试import os;print(os.path.exists(ico1))。如果这是notify2错误,则必须返回True。使用ipdb:import ipdb;ipdb.set_trace()进行调试也很不错。

如果先前的输出为False,则解决方案可能是将wdir=getcwd()替换为os.path.basename(__file__)

更好的实践:

使用os.path.join代替'/'。例如。而不是ico1=wdir+"/files/earthquake.ico"ico1 = os.path.join(wdir,'files','earthquake.ico')

使用%s代替+output=html[0][8]+html[0][9]+" bölgesinde "-> output="%s %s bölgesinde "%(html[0][8],html[0][9])"

您可能想缩短ok变量:

#ok=0
try:
    html=requests.get(url).text
    if "Dicle University" in html:
      #ok=1
      raise
except:
    #ok=1
    print("Internet Connection Error")
    return ["No internet connection"]

#if there is no error ok variable is equal to 0 and the program works correctly
#if ok==0:
#clear the text from unnecessary parts. Such as header, banner etc...
x=(html.index("--------------"),html.index('</pre>'))
html=html[x[0]:x[1]].splitlines()[1:500]

要解析HTML,可以使用BeautifulSoup之类的框架。