Kivy 的计时问题

问题描述 投票:0回答:1

我是 Kivy / KivyMD 的新手,正在努力解决某种时间问题。

我有一个 Kivy ScreenManager,启用了 2 个屏幕,一个基本上可以工作的启动屏幕,以及一个我想在自定义功能完成时前进到的主屏幕。自定义函数正在对 SQLite 数据库进行一些验证,并更新接口的状态属性。

显示闪屏时调用自定义函数。在该功能中,我希望启动屏幕上有一个标签来更新显示进度。多次尝试后发生的情况是数据例程在完成之前不会更新标签。我尝试了不同的事件 on_start、on_enter 和 init 但效果保持不变。

主.py

class Interface(ScreenManager):
  status = StringProperty("Data Routines")
  stat_clock = None
  
  def __init__(self, **kwargs)
    super().__init__(**kwargs)

    self.stat_clock = Clock.schedule_interval(self.check_status, 3)
    self.data_routines()

  def check_status(self):
    if self.status == "Finished":
      self.stat_clock.cancel()
      self.current = "Main Screen"

  def data_routines(self):
    ... a bunch of code ...
    self.status = "whatever"
    ... a bunch of more code ...
    self.status = "Finished"

kivy.kv:

Interface:
  Screen:
    name: "Splash Screen"
    AnchorLayout:
      anchor_x: "center"
      anchor_y: "center"
      Image:
        source: "image.png"
    AnchorLayout:
      anchor_x: "center"
      anchor_y: "center"
      Label:
        text: root.status
        padding: [0, dp(260), 0, 0]
  Screen:
    name: "Main Screen"
    AnchorLayout:
      anchor_x: "center"
      anchor_y: "center"
      Label:
        text: "Main Screen"
        size_hint: None, None
        height: dp(40)
        width: dp(80)

任何建议都非常感谢。

python kivy kivy-language
1个回答
0
投票

您正在主线程上运行

data_routines()
。 GUI仅在主线程上更新,因此必须等到您释放主线程(从
data_routines()
返回)才能更新。

修复方法是在另一个线程中运行

data_routines()
并从该线程调用
Clock.schedule_once()
来启动更新 GUI 的任何方法。例如,更新
status
属性。而且那些 GUI 更新方法应该简短而快速。

© www.soinside.com 2019 - 2024. All rights reserved.