Kivy数字时钟问题 [英] Kivy Digital Clock Issues
问题描述
我正在尝试向我的Kivy程序中添加数字时钟,这似乎有麻烦.
I'm trying to add a digital clock to my Kivy program, it seems to be having trouble.
这是.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()
和.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是我想显示时钟的屏幕的标题,它由一个单独的Builder(main.kv)提供.
ContScreen is the title of the screen I want to show the clock on, it's served by a separate Builder (main.kv).
任何帮助将不胜感激!现在已经为这个时钟挣扎了几个小时.据我所知,麻烦 似乎在.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.
奖金:如果您想加倍努力,我还想添加一个计时器,在按下.kv上的一个按钮时,该计时器会倒计时x数量. x的数量会有所不同,具体取决于您按下哪个按钮.
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.
推荐答案
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)
应该工作.请注意,在主.py
文件中,您将需要包括:
should work. Note that in your main .py
file you will need to include:
from digitalclock.digitalclock import DigitalClock
(假设digitalclock.py
文件位于digitalclock
文件夹中)
(assuming that the digitalclock.py
file is in a digitalclock
folder)
这篇关于Kivy数字时钟问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!