Python Notify2不显示图像

2024-10-03 17:17:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用notify2模块在linux中显示桌面通知。此代码从internet获取最新地震并显示桌面通知

当我在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]

Tags: 代码inimportforoutputdatagetif