Kivy Digital Clock Issues

。_饼干妹妹 提交于 2020-01-17 13:59:08

问题


I'm trying to add a digital clock to my Kivy program, it seems to be having trouble.

Here is the .py:

import kivy

kivy.require('1.10.0')

from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.stacklayout import StackLayout
from kivy.clock import Clock
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty

import time


class IntroScreen(Screen):
    pass


class ContScreen(Screen):
    pass


class ScreenManagement(ScreenManager):
    pass


backbone = Builder.load_file("main.kv")


class Status(FloatLayout):
    _change = StringProperty()
    _tnd = ObjectProperty(None)

    def update(self, *args):
        self.time = time.asctime()
        self._change = str(self.time)
        self._tnd.text = str(self.time)
        print (self._change)


class XGApp(App):
    time = StringProperty()

    def update(self, *args):
        self.time = str(time.asctime())  # + 'time'?

    def build (self):
        Clock.schedule_interval(self.update, 1)
        return backbone


xApp = XGApp()

if __name__ == "__main__":
    xApp.run()

and the .kv

<ContScreen>:
    FloatLayout
        size_hint: .1,.1
        canvas.before:
            Color:
                rgba: 0,0,0,1
            Rectangle:
                pos: self.pos
                size: self.size
        Label:
            text: app.time

ContScreen is the title of the screen I want to show the clock on, it's served by a separate Builder (main.kv).

Any help would be appreciated! Been struggling with this clock for a few hours now. The trouble seems to be on the .kv side from what I can tell.

BONUS: If you want to go the extra mile, I also want to add a timer that counts down x amount on press of a button on the .kv. The x amount would be different depending on which button you press.


回答1:


I have made a fork of the original kivydigitalclock here. It should be easier to use than the original. You can add it to your .kv file just as any other widget. For example, something like:

<ContScreen>:
    FloatLayout
        size_hint: .1,.1
        canvas.before:
            Color:
                rgba: 0,0,0,1
            Rectangle:
                pos: self.pos
                size: self.size
        Label:
            text: app.time
    DigitalClock:
        pos_hint: {'right': 1.0, 'center_y': 0.5}
        size_hint: (0.2, 0.2)

should work. Note that in your main .py file you will need to include:

from digitalclock.digitalclock import DigitalClock

(assuming that the digitalclock.py file is in a digitalclock folder)



来源:https://stackoverflow.com/questions/52444993/kivy-digital-clock-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!