Kivy recycleview 不更新时钟调用功能

Kivy recycleview not updating on Clock call function

我的应用程序有一个计时器作为 recycleview 中的数据,它将更新以每秒更改计时器的文本。所以我使用 Clock 每隔一秒调用 timer_update 函数来更新时间。

问题是计时器文本在终端上打印时会更新,但在应用程序屏幕上不会更新。

这是我的代码:

import datetime
from kivy.clock import Clock
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.properties import StringProperty
from kivymd.uix.boxlayout import MDBoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen


Builder.load_string( '''
<Timer>:
    orientarion: 'vertical'
    md_bg_color: 0, 0, 1, 1
        
    MDLabel:
        text: root.text
        color: 1,1,1,1
        halign: 'center'
            
<MyLayout>:
    MDBoxLayout:
        orientation: 'vertical'
    
        RecycleView:
            id: rv
            key_viewclass: 'viewclass'
            key_size: 'height'
            effect_cls: 'ScrollEffect'
            
            RecycleBoxLayout:
                padding: dp(10)
                size_hint_y: None
                default_size: None, dp(60)
                default_size_hint: 1, None
                height: self.minimum_height
                orientation: 'vertical'
                   
''')
class Sm(ScreenManager):
    pass
    
    
class Timer(MDBoxLayout):
    text = StringProperty()
    

class MyLayout(Screen):
    
    def on_enter(self):
        self.add_timer()
        Clock.schedule_interval(self.update_timer, 1)

    def update_timer(self, dt):
        self.ids.rv.data[0]['text'] = str(datetime.datetime.now())
        print(self.ids.rv.data[0]['text'])
        
    def add_timer(self):
        self.ids.rv.data.append(
            {
                "viewclass": "Timer",
                "text": str(datetime.datetime.now()),
                "callback": lambda x: x,
            }
        )
        

class MyApp(MDApp):
    
    def build(self):
        sm = Sm()
        sm.add_widget(MyLayout())
        return sm
        
MyApp().run()

如果您在更新数据后调用 self.ids.rv.refresh_from_data() 应该可以解决问题。

完整示例:

import datetime
from kivy.clock import Clock
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.properties import StringProperty
from kivymd.uix.boxlayout import MDBoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen


Builder.load_string( '''
<Timer>:
    orientarion: 'vertical'
    md_bg_color: 0, 0, 1, 1
        
    MDLabel:
        text: root.text
        color: 1,1,1,1
        halign: 'center'
            
<MyLayout>:
    MDBoxLayout:
        orientation: 'vertical'
    
        RecycleView:
            id: rv
            key_viewclass: 'viewclass'
            key_size: 'height'
            effect_cls: 'ScrollEffect'
            
            RecycleBoxLayout:
                padding: dp(10)
                size_hint_y: None
                default_size: None, dp(60)
                default_size_hint: 1, None
                height: self.minimum_height
                orientation: 'vertical'
                   
''')
class Sm(ScreenManager):
    pass
    
    
class Timer(MDBoxLayout):
    text = StringProperty()
    

class MyLayout(Screen):
    
    def on_enter(self):
        self.add_timer()
        Clock.schedule_interval(self.update_timer, 1)

    def update_timer(self, dt):
        self.ids.rv.data[0]['text'] = str(datetime.datetime.now())
        self.ids.rv.refresh_from_data()
        print(self.ids.rv.data[0]['text'])
        
    def add_timer(self):
        self.ids.rv.data.append(
            {
                "viewclass": "Timer",
                "text": str(datetime.datetime.now()),
                "callback": lambda x: x,
            }
        )

class MyApp(MDApp):
    
    def build(self):
        sm = Sm()
        sm.add_widget(MyLayout())
        return sm
        
MyApp().run()