Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通知在呼叫后退出_Python - Fatal编程技术网

Python 通知在呼叫后退出

Python 通知在呼叫后退出,python,Python,我编写了一个Python脚本,在比特币价格达到4500美元时显示桌面通知,但如果达到价格,脚本将退出。如何保持脚本运行 代码如下: import time import requests import gi gi.require_version('Notify', '0.7') from gi.repository import Notify r = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json") r.json

我编写了一个Python脚本,在比特币价格达到4500美元时显示桌面通知,但如果达到价格,脚本将退出。如何保持脚本运行

代码如下:

import time
import requests
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify

r = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")
r.json()
resp = r.json()

price = resp["bpi"]["USD"]["rate_float"]
top = 4200

if price > top :

# One time initialization of libnotify
Notify.init("Crypto Notify")

# Create the notification object
summary = "Crypto Alert!"
body = "BTC : $ %s" % (price)
notification = Notify.Notification.new(
    summary,
    body, # Optional
)

# Actually show on screen
notification.show()

else:
    while price < top :
        r =requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")
        print price
time.sleep(10)
导入时间
导入请求
导入gi
gi.require_版本('Notify','0.7')
从gi.repository导入通知
r=请求。获取(“https://api.coindesk.com/v1/bpi/currentprice.json")
r、 json()
resp=r.json()
价格=响应[“bpi”][“美元”][“汇率浮动”]
top=4200
如果价格>顶部:
#libnotify的一次性初始化
Notify.init(“加密通知”)
#创建通知对象
summary=“加密警报!”
body=“BTC:$%s”%(价格)
通知=Notify.notification.new(
总结,
主体,#可选
)
#实际显示在屏幕上
notification.show()
其他:
虽然价格<顶部:
r=请求。获取(“https://api.coindesk.com/v1/bpi/currentprice.json")
印刷价格
时间。睡眠(10)

因此,从我所看到的情况来看,您编写的脚本似乎是在一个过程中执行的,即所有语句只执行一次。因此,您的脚本将等待price大于的条件为true,一旦为true,它将执行IF块脚本的其余部分

您需要的是一个封装脚本的循环,而who的最终条件需要很长时间才能实现某种无限循环,但更安全

另外,您可以尝试的另一种方法是将脚本保持在无限循环中,并在需要时使用ctrl+c退出脚本

虽然这不是很干净的方法

示例代码:

import time
import requests
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify

while true :
    r = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")
    r.json()
    resp = r.json()

    price = resp["bpi"]["USD"]["rate_float"]
    top = 4200

    if price > top :

    # One time initialization of libnotify
    Notify.init("Crypto Notify")

    # Create the notification object
    summary = "Crypto Alert!"
    body = "BTC : $ %s" % (price)
    notification = Notify.Notification.new(summary,body)

    # Actually show on screen
    notification.show()

    else:
        r =requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")
        print price
        time.sleep(10)

因此,从我所看到的情况来看,您的脚本似乎是编写为在一个过程中执行的,即所有语句只执行一次。因此,您的脚本将等待price大于的条件为true,一旦为true,它将执行IF块脚本的其余部分

您需要的是一个封装脚本的循环,而who的最终条件需要很长时间才能实现某种无限循环,但更安全

另外,您可以尝试的另一种方法是将脚本保持在无限循环中,并在需要时使用ctrl+c退出脚本

虽然这不是很干净的方法

示例代码:

import time
import requests
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify

while true :
    r = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")
    r.json()
    resp = r.json()

    price = resp["bpi"]["USD"]["rate_float"]
    top = 4200

    if price > top :

    # One time initialization of libnotify
    Notify.init("Crypto Notify")

    # Create the notification object
    summary = "Crypto Alert!"
    body = "BTC : $ %s" % (price)
    notification = Notify.Notification.new(summary,body)

    # Actually show on screen
    notification.show()

    else:
        r =requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")
        print price
        time.sleep(10)

在循环时移动
。如果希望脚本永远运行(除非手动中断),请将其设置为
while True:
。因此,它应该是:while True:r=??我希望脚本永远运行并在价格到达后继续推送通知,是否可能?移动
while
循环。如果希望脚本永远运行(除非手动中断),请将其设置为
while True:
。因此,它应该是:while True:r=??我希望脚本永远运行,并在价格到达后继续推送通知,有可能吗?像编辑这样的东西,如果从机场应答后一次就不能工作,那么表示歉意,所以无法测试。太好了!它确实有效,我做了类似的事情等待你的回答!但它和你的一样“好”,谢谢你的澄清!!像编辑这样的东西会道歉,如果它在机场应答后一次就不能工作,所以无法测试。太好了!它确实有效,我做了类似的事情等待你的回答!但它和你的一样“好”,谢谢你的澄清!!