【发布时间】:2021-05-17 06:38:35
【问题描述】:
我正在为用户界面编写代码,简而言之,我正在尝试刷新页面,直到外部变量发生更改。更改此外部变量后,我想进入下一个屏幕(此值来自 arduino)。
class screenTwo(Screen):
def __init__(self, **kwargs):
super(screenTwo, self).__init__(**kwargs)
self.screenTwoLabel = Label(text="Screen Two")
self.add_widget(self.screenTwoLabel)
if(READ_ARDUINO == b'Finished):
Clock.schedule_once(self.go_screenthree, 2)
Clock.schedule_interval(self.go_loop, 3)
def go_loop(self, *args):
self.manager.current = 'screenTwo'
def on_enter(self, *args):
# start the timer for 10 seconds
self.timer = Clock.schedule_once(self.idle_logout, IDLE_DELAY)
def on_leave(self, *args):
self.timer.cancel() # This is where the error comes in
self.timer = None
def on_touch_down(self, touch):
if self.timer is not None:
self.timer.cancel()
self.timer = Clock.schedule_once(self.idle_logout, IDLE_DELAY)
return super(screenTwo, self).on_touch_down(touch)
这是我正在尝试做的简化版本,但如果我运行它,我会收到错误:
AttributeError: 'screenTwo' object has no attribute 'timer'
我在这段代码中有一些其他的东西,比如 on_enter 和 on_leave 命令用于不活动的目的,但我想知道如何绕过它,因为从逻辑上讲它似乎应该工作。奇怪的是每个其他屏幕都有 on_enter、on_leave、on_touch_down 计时器,但没有一个会给出错误并正常工作。
【问题讨论】:
-
你实际上并没有向我们展示有错误的代码部分(你有错误的实际位置可能看起来像
obj.timer或self.timer和self或obj是screenTwo... 它告诉你的是你的班级没有任何叫做“计时器”的东西 -
抱歉,只是稍微修改了代码和描述。
标签: python user-interface kivy kivy-language