当我使用代码运行简单的.py文件时
notify-send " ha ha "我在22.04收到通知,没有任何问题。
然后,我在另一个.py文件中尝试了以下代码。它产生了一个语法错误。
x = 1
count = x
while count <= 8:
notify-send "Let's Take a Break!"
sleep 60
count += 1
if count <= 8:
notify-send "Ok folks," "Let's get back to work!"
sleep 3600
else:
notify-send "Ok folks," "Let's call it a day!" 请帮我找出问题所在。谢谢。
发布于 2022-10-08 08:55:26
为了能够从python脚本中执行系统命令(如notify-send ),您需要首先导入os模块,然后使用它执行系统命令,如下所示:
import os
os.system('notify-send "Let\'s Take a Break!"')尽管如此,您可能需要注意其他语法问题,比如在bash中工作的D4调用,但是python需要time.sleep(60),并且您可能希望首先导入time模块才能工作。另一件事是if . else的缩进,你也需要注意这一点。
因此,您问题中的示例代码应该是:
import os
import time
x = 1
count = x
while count <= 8:
os.system('notify-send "Let\'s Take a Break!"')
time.sleep(60)
count += 1
if count <= 8:
os.system('notify-send "Ok folks," "Let\'s get back to work!"')
time.sleep(3600)
else:
os.system('notify-send "Ok folks," "Let\'s call it a day!"')正确地使用python运行您的python脚本也非常重要,而不是像我这样理解的其他解释器,例如bash (很可能是由您的shell运行,例如bash而不是python解释器):
当我使用代码.py运行一个简单的
notify-send " ha ha "文件时,我会在22.04中获得通知,没有任何问题。
https://askubuntu.com/questions/1434386
复制相似问题