试图在python中创建一个一小时的循环


from notifypy import Notify
import schedule
def remember_water():
notification = Notify()
notification.title = "XXX"
notification.message = "XXX"
notification.send()

schedule.every().hour.do(remember_water())
while True:
schedule.run_pending()
time.sleep(1)

试着每小时提醒一次喝水…执行时出现类型错误,也许有一些其他模块更好。没有接触过这些模块,请帮助:)

运行代码产生:

Traceback (most recent call last):
File "/home/lars/tmp/python/drink.py", line 11, in <module>
schedule.every().hour.do(remember_water())
File "/home/lars/.local/share/virtualenvs/lars-rUjSNCQn/lib/python3.10/site-packages/schedule/__init__.py", line 625, in do
self.job_func = functools.partial(job_func, *args, **kwargs)
TypeError: the first argument must be callable

你的问题在这里:

schedule.every().hour.do(remember_water())

.do方法的参数必须是一个可调用的(像一个函数),但是你在这里调用你的函数并传递结果。你想传递函数本身:

schedule.every().hour.do(remember_water)