How to have an updating time in Kivy

后端 未结 1 1517
南方客
南方客 2021-01-25 15:42

I am creating a calendar app in kivy, and I was wondering how I would be able to add an updating clock? I can use the datetime python function, but when I load it in my app it s

相关标签:
1条回答
  • 2021-01-25 16:05

    Here's a complete example of how to get this feature working. You should be able to work from this and apply it to your own code. If you have any specific questions, let me know in a comment and I'll be happy to answer :)

    from kivy.app import App
    from datetime import datetime
    from datetime import timedelta
    from kivy.clock import Clock
    from kivy.uix.label import Label
    
    class MyApp(App):
        def build(self):
            self.now = datetime.now()
    
            # Schedule the self.update_clock function to be called once a second
            Clock.schedule_interval(self.update_clock, 1)
            self.my_label = Label(text= self.now.strftime('%H:%M:%S'))
            return self.my_label  # The label is the only widget in the interface
    
        def update_clock(self, *args):
            # Called once a second using the kivy.clock module
            # Add one second to the current time and display it on the label
            self.now = self.now + timedelta(seconds = 1)
            self.my_label.text = self.now.strftime('%H:%M:%S')
    
    MyApp().run()
    
    0 讨论(0)
提交回复
热议问题