Python kivy时钟调度间隔更新

Python kivy clock shedule interval updating

我在更改时钟更新间隔时遇到问题。我需要在 运行 时钟时更改间隔。我对 kivy 库还是个新手,这是我的第一个更大的项目,所以我对它的工作原理有一个基本的了解。

我最初只是更改值的计划行不通。我可以看到值在变化,但时钟保持不变。

class MainWindow(Screen):

    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        Clock.schedule_interval(self.btn, player.clock_interval)

    def reduce_clock_interval(self):
        player.clock_interval -= 1

稍后使用按钮调用 reduce_clock_interval() 函数:

<SecondWindow>:
    name: "second"
    BoxLayout:     
        BoxLayout:
            BoxLayout:
                Button:    
                    text: "Reduce time"
                    on_press: root.reduce_clock_interval()

我也尝试过使用 Clock.cancel() 停止当前时钟并创建一个具有新值的新时钟,但仍然无法正常工作。

class MainWindow(Screen):

    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        clock = Clock.schedule_interval(self.btn, player.clock_interval)

    def reduce_clock_interval(self):
        player.clock_interval -= 1
        Clock.cancel(MainWindow.__init__.clock)
        clock = Clock.schedule_interval(self.btn, player.clock_interval)

更新:

我在修改代码的同时做了一些修改: - 将 clock_interval 移动到主窗口 class - 将 reduce_clock_interval 函数移动到 SecondWindow

最小可重现示例:

1.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock

class MainWindow(Screen):
    clock_interval = 5

    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        self.clock = Clock.schedule_interval(self.btn, self.clock_interval)

    def btn(self, *args):
        print(*args)
        print(MainWindow.clock_interval)

class SecondWindow(Screen):

    def reduce_clock_interval(self):
        MainWindow.clock_interval -= 1
        MainWindow.clock.cancel()
        MainWindow.clock = Clock.schedule_interval(MainWindow.btn(), self.clock_interval)

class WindowManager(ScreenManager):
    pass

kv = Builder.load_file("my.kv")

class MyApp(App):
    def build(self):
        return kv

if __name__ == '__main__':
    MyApp().run()

2.kv

ScreenManager:
    MainWindow:
    SecondWindow:

<MainWindow>:
    name: "main"
    value: value
    BoxLayout:
        cols: 2
        Button:
            text: "Screen 2"
            on_release:
                app.root.current = "second"

<SecondWindow>:
    name: "second"
    BoxLayout:
        cols: 2

        Button:
            text: "update clock"
            on_press: root.reduce_clock_interval()

        Button:
            text: "Screen 1"
            on_release:
                app.root.current = "main"

您可以像这样保留对 Clock 事件的引用来做您想做的事:

class MainWindow(Screen):

    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        # save a reference to the event
        self.clock = Clock.schedule_interval(self.btn, player.clock_interval)

    def reduce_clock_interval(self):
        player.clock_interval -= 1
        self.clock.cancel()  # cancel the saved event
        # create a new event
        self.clock = Clock.schedule_interval(self.btn, player.clock_interval)

在您编辑的 post 中,您试图将 clock 作为 class 属性访问,但它是一个实例属性。这是 reduce_clock_interval() 的更正版本:

def reduce_clock_interval(self):
    main_window = App.get_running_app().root.get_screen('main')
    MainWindow.clock_interval -= 1
    main_window.clock.cancel()
    main_window.clock = Clock.schedule_interval(main_window.btn, MainWindow.clock_interval)