Python Notify2不显示图像

问题描述 投票:0回答:1

我正在使用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]
python notify
1个回答
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之类的框架。

© www.soinside.com 2019 - 2024. All rights reserved.