Python Notify2 不显示图像
Python Notify2 Doesn't Show Images
我正在使用 notify2 模块在 linux 中显示桌面通知。此代码从 Internet 获取最新的地震并显示桌面通知。
当我在 spyder3 和终端 python3 earthquake.py
上 运行 时,代码完美地显示了通知。我已将代码注册到启动应用程序中。当我登录我的计算机时,python3 "/home/ali/Programlarım/Python/My Projects/earthquake.py"
命令起作用并自动编码 运行s。它从网络获取信息并显示通知。但是通知中没有图标。如何解决这个问题?
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=[] #scrape 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]
ico1
真的存在吗?这行引起了我的注意:wdir=getcwd()
对于调试,尝试import os;print(os.path.exists(ico1))
。如果这是 notify2 错误,则必须 return 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这样的框架。
我正在使用 notify2 模块在 linux 中显示桌面通知。此代码从 Internet 获取最新的地震并显示桌面通知。
当我在 spyder3 和终端 python3 earthquake.py
上 运行 时,代码完美地显示了通知。我已将代码注册到启动应用程序中。当我登录我的计算机时,python3 "/home/ali/Programlarım/Python/My Projects/earthquake.py"
命令起作用并自动编码 运行s。它从网络获取信息并显示通知。但是通知中没有图标。如何解决这个问题?
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=[] #scrape 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]
ico1
真的存在吗?这行引起了我的注意:wdir=getcwd()
对于调试,尝试import os;print(os.path.exists(ico1))
。如果这是 notify2 错误,则必须 return 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这样的框架。