📌  相关文章
📜  Python|使用 .kv 文件在 kivy 中使用时钟对象创建秒表

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

Python|使用 .kv 文件在 kivy 中使用时钟对象创建秒表

Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、Linux和Windows等平台上运行。它基本上是用来开发Android应用程序的,但这并不意味着它不能在桌面应用程序上使用。

时钟对象:
Clock 对象允许您安排将来的函数调用;一次或以指定的时间间隔重复。
您可以通过 dt 参数获取调度和调用回调之间经过的时间:

Python3
# define callback
def my_callback(dt):
    pass
 
# clock.schedule_interval with time specified
Clock.schedule_interval(my_callback, 0.5)
 
# clock.schedule_once with time specified
Clock.schedule_once(my_callback, 5)
 
# call my_callback as soon as possible.
Clock.schedule_once(my_callback)


Python3
'''
Code of How to create Stopwatch
'''
    
# 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 Builder is responsible for creating
# a Parser for parsing a kv file
from kivy.lang import Builder
 
# The Properties classes are used
# when you create an EventDispatcher.
from kivy.properties import NumericProperty
 
# BoxLayout arranges children in a vertical or horizontal box.
# or help to put the children at the desired location.
from kivy.uix.boxlayout import BoxLayout
 
# he Clock object allows you to
# schedule a function call in the future
from kivy.clock import Clock
 
 
# Create the .kv file and load it by using Builder
Builder.load_string('''
 
:
 
    # Assigning the alignment to buttons
    BoxLayout:
        orientation: 'vertical'
 
        # Create Button
         
        Button:
            text: 'start'
            on_press: root.start()
             
        Button:
            text: 'stop'
            on_press: root.stop()
             
        Button:
            text: 'Reset'
            on_press: root.number = 0
 
    # Create the Label
    Label:
        text: str(round(root.number))
        text_size: self.size
        halign: 'center'
        valign: 'middle'
''')
  
# Create the Layout class
class MainWidget(BoxLayout):
     
    number = NumericProperty()
     
    def __init__(self, **kwargs):
 
        # The super() builtin
        # returns a proxy object that
        # allows you to refer parent class by 'super'.
        super(MainWidget, self).__init__(**kwargs)
 
        # Create the clock and increment the time by .1 ie 1 second.
        Clock.schedule_interval(self.increment_time, .1)
 
        self.increment_time(0)
 
    # To increase the time / count
    def increment_time(self, interval):
        self.number += .1
 
    # To start the count
    def start(self):
         
        Clock.unschedule(self.increment_time)
        Clock.schedule_interval(self.increment_time, .1)
 
    # To stop the count / time
    def stop(self):
        Clock.unschedule(self.increment_time)
 
# Create the App class
class TimeApp(App):
    def build(self):
        return MainWidget()
 
# Run the App
TimeApp().run()


注意:如果回调返回 False,计划将被取消并且不会重复。

在此,我们将创建 kivy 秒表,并在其中创建 3 个按钮,它们是开始、暂停、恢复。

Basic Approach:  
1) import kivy
2) import kivyApp
3) import Builder
4) import Boxlayout
5) Import clock
6) import kivy properties(only needed one)
7) Set minimum version(optional)
8) Create the .kv code:
     1) Create Buttons
     2) Add call to button
     3) Add label 
9) Create Layout class
10) Create App class
11) return Layout/widget/Class(according to requirement)
12) Run an instance of the class

# 方法的实施:

Python3

'''
Code of How to create Stopwatch
'''
    
# 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 Builder is responsible for creating
# a Parser for parsing a kv file
from kivy.lang import Builder
 
# The Properties classes are used
# when you create an EventDispatcher.
from kivy.properties import NumericProperty
 
# BoxLayout arranges children in a vertical or horizontal box.
# or help to put the children at the desired location.
from kivy.uix.boxlayout import BoxLayout
 
# he Clock object allows you to
# schedule a function call in the future
from kivy.clock import Clock
 
 
# Create the .kv file and load it by using Builder
Builder.load_string('''
 
:
 
    # Assigning the alignment to buttons
    BoxLayout:
        orientation: 'vertical'
 
        # Create Button
         
        Button:
            text: 'start'
            on_press: root.start()
             
        Button:
            text: 'stop'
            on_press: root.stop()
             
        Button:
            text: 'Reset'
            on_press: root.number = 0
 
    # Create the Label
    Label:
        text: str(round(root.number))
        text_size: self.size
        halign: 'center'
        valign: 'middle'
''')
  
# Create the Layout class
class MainWidget(BoxLayout):
     
    number = NumericProperty()
     
    def __init__(self, **kwargs):
 
        # The super() builtin
        # returns a proxy object that
        # allows you to refer parent class by 'super'.
        super(MainWidget, self).__init__(**kwargs)
 
        # Create the clock and increment the time by .1 ie 1 second.
        Clock.schedule_interval(self.increment_time, .1)
 
        self.increment_time(0)
 
    # To increase the time / count
    def increment_time(self, interval):
        self.number += .1
 
    # To start the count
    def start(self):
         
        Clock.unschedule(self.increment_time)
        Clock.schedule_interval(self.increment_time, .1)
 
    # To stop the count / time
    def stop(self):
        Clock.unschedule(self.increment_time)
 
# Create the App class
class TimeApp(App):
    def build(self):
        return MainWidget()
 
# Run the App
TimeApp().run()

输出:

笔记:

在这种情况下,当您按下开始计数开始时,当按下重新启动时它会再次开始,当暂停时它会暂停。