【发布时间】:2020-07-11 22:25:12
【问题描述】:
我是 python 和 kivy 的新手,并尝试从代码 sn-ps 和反复试验中学习。但现在我被困住了。 为了在树莓上显示天气和垃圾信息,我使用了 kivy。 为了获取这些信息,我使用了 URLRequest 函数。这个函数需要clock-function
while not req.is_finished:
Clock.tick()
return req.result
所以程序可以工作,显示信息,但在大约 20 分钟左右后定期崩溃,并出现错误“RuntimeError:超出最大递归深度 但我不明白如何通过仍然让事情正常工作来摆脱递归:(
这里是上下文中的代码。有人可以帮忙吗?
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.network.urlrequest import UrlRequest
from time import gmtime, strftime, localtime, sleep
class garbage:
def garbage_text(garbage):
req = UrlRequest('http://192.168.1.1:8083/fhem?cmd={ReadingsVal(%22ABFALL%22,%22next_text%22,0)}&XHR=1&fwcsrf=password')
while not req.is_finished:
Clock.tick()
return req.result
class weather:
def weather_db1(weather):
req = UrlRequest('http://192.168.1.1:8083/fhem?cmd={ReadingsVal(%22netatmo_M01_00_00_3f_1d_1a%22,%22temperature%22,0)}&XHR=1&fwcsrf=password')
while not req.is_finished:
Clock.tick()
return req.result
class MyBox(BoxLayout):
def update(self, *args):
uweather = weather()
aktw = uweather.weather_db1()
ggarbage = garbage()
garbagetext = ggarbage.garbage_text()
self.ids.temp_ist.text = str(aktw)
self.ids.uhrzeit_top.text = strftime("%H:%M", localtime())
self.ids.datum_top.text = strftime("%d.%m.%Y", localtime())
self.ids.garbage_std.text = garbagetext+" rausstellen "
class ControlApp(App):
def build(self):
actclock = MyBox()
Clock.schedule_interval(actclock.update, 1)
return actclock
if __name__ == "__main__":
ControlApp().run()
【问题讨论】:
-
你试过用
req.wait()替换那个循环吗? -
我建议在
update()方法中添加代码来计时,以确保返回时间少于 1 秒。如果update()方法的返回时间超过1秒,那么Clock.schedule_interval()对update()的调用就会慢慢堆积起来。 -
您的代码没有正确缩进。你能解决这个问题吗?格式化是 Python 语法的一部分,所以它很重要(不同的缩进 = 不同的代码)。