【问题标题】:Kivy: Trying to continuously "refresh" a screen in KivyKivy:尝试在 Kivy 中不断“刷新”屏幕
【发布时间】: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.timerself.timerselfobjscreenTwo ... 它告诉你的是你的班级没有任何叫做“计时器”的东西
  • 抱歉,只是稍微修改了代码和描述。

标签: python user-interface kivy kivy-language


【解决方案1】:

在你的 onLeave 中 self.timer 可能不存在(即使它存在也可能是 None,它不会有 .cancel 方法)

class screenTwo(Screen):
    timer = None # now timer will exist
    def __init__(self, **kwargs):           
       super(screenTwo, self).__init__(**kwargs)
       self.screenTwoLabel = Label(text="Screen Two")
       self.add_widget(self.screenTwoLabel)
       ...
    
    def onLeave(self):
       if(self.timer): # check to make sure it exists...
          self.timer.cancel()
        ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多