📜  Python|在 kivy 中使用时钟对象创建秒表

📅  最后修改于: 2022-05-13 01:54:21.633000             🧑  作者: Mango

Python|在 kivy 中使用时钟对象创建秒表

Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、Linux和Windows等平台上运行。它基本上是用来开发Android应用程序的,但这并不意味着它不能在桌面应用程序上使用。
在本文中,我们将了解如何使用标签创建秒表。
在代码中,我们将使用标签创建一个计数器,当您以秒为单位设置时间时,它会像倒计时一样开始减少,第二秒我们将使用时钟对象来做同样的事情。

时钟对象:

  1. Kivy 提供 Clock 对象。
  2. 可以使时钟对象在指定的时间段过去后调用函数。
  3. Kivy 中的时钟对象可以配置为在每次经过时间时调用一个函数,或者只调用一次。
Basic Approach:
1) import kivy
2) import kivyApp
3) import label
4) import Animation
5) Import clock
6) import kivy properties(only needed one)
7) Set minimum version(optional)
8) Create Label class
9) Create App class
10) return Layout/widget/Class(according to requirement)
11) Run an instance of the class

# 简单方法:

Python3
'''
Code of How to create countdown using label only
'''
   
# Program to Show how to create a switch
# import kivy module   
import kivy 
       
# base Class of your App inherits from the App class.   
# app:always refers to the instance of your application  
from kivy.app import App
     
# this restrict the kivy version i.e 
# below this kivy version you cannot 
# use the app or software 
kivy.require('1.9.0')
 
# The Label widget is for rendering text.
from kivy.uix.label import Label
 
# Animation is used to animate Widget properties
from kivy.animation import Animation
 
# The Properties classes are used when you create an EventDispatcher.
from kivy.properties import StringProperty, NumericProperty
 
 
# create a label class
class Clock(Label):
 
    # Set the numeric property
    # i.e set the counter number you can change it accordingly
    a = NumericProperty(100)  # seconds
 
    # To start countdown
    def start(self):
        Animation.cancel_all(self)  # stop any current animations
        self.anim = Animation(a = 0, duration = self.a)
 
        # TO finish count down
        def finish_callback(animation, clock):
            clock.text = "FINISHED"
 
        self.anim.bind(on_complete = finish_callback)
        self.anim.start(self)
 
    # If u remove this there will be nothing on screen
    def on_a(self, instance, value):
        self.text = str(round(value, 1))
 
         
# Create the App class
class TimeApp(App):
    def build(self):
        # Create the object of Clock class
        clock = Clock()
 
        # call the function from class Clock
        clock.start()
        return clock
 
# Run the App
if __name__ == "__main__":
    TimeApp().run()


Python3
'''
Code of How to create countdown using label only
'''
   
# Program to Show how to create a switch
# import kivy module   
import kivy 
       
# base Class of your App inherits from the App class.   
# app:always refers to the instance of your application  
from kivy.app import App
     
# this restrict the kivy version i.e 
# below this kivy version you cannot 
# use the app or software 
kivy.require('1.9.0')
 
# The Label widget is for rendering text.
from kivy.uix.label import Label
 
# The Clock object allows you to schedule
# a function call in the future; once or
# repeatedly at specified intervals.
from kivy.clock import Clock
  
# The kivy App that extends from the App class
class ClockDemo(App):
 
    count = 0
 
    def build(self):
       self.myLabel = Label(text ='Waiting for updates...')
 
       # Start the clock
       Clock.schedule_interval(self.Callback_Clock, 1)
        
       return self.myLabel
 
    def Callback_Clock(self, dt):
        self.count = self.count + 1
        self.myLabel.text = "Updated % d...times"% self.count
 
        
# Run the app
if __name__ == '__main__':
    ClockDemo().run()


输出:

注意:倒计时从 100 开始,到 0 结束

# 现在通过使用时钟对象:

Python3

'''
Code of How to create countdown using label only
'''
   
# Program to Show how to create a switch
# import kivy module   
import kivy 
       
# base Class of your App inherits from the App class.   
# app:always refers to the instance of your application  
from kivy.app import App
     
# this restrict the kivy version i.e 
# below this kivy version you cannot 
# use the app or software 
kivy.require('1.9.0')
 
# The Label widget is for rendering text.
from kivy.uix.label import Label
 
# The Clock object allows you to schedule
# a function call in the future; once or
# repeatedly at specified intervals.
from kivy.clock import Clock
  
# The kivy App that extends from the App class
class ClockDemo(App):
 
    count = 0
 
    def build(self):
       self.myLabel = Label(text ='Waiting for updates...')
 
       # Start the clock
       Clock.schedule_interval(self.Callback_Clock, 1)
        
       return self.myLabel
 
    def Callback_Clock(self, dt):
        self.count = self.count + 1
        self.myLabel.text = "Updated % d...times"% self.count
 
        
# Run the app
if __name__ == '__main__':
    ClockDemo().run()

输出:

注意:这从 0 开始,一直运行到你切开窗口