如何将图像和时钟添加到 kv 文件
How to add an image and clock to a kv file
我正在尝试实现 ComicCreator GUI sample as a template for my own project. The code is easy to follow, but I want to change the toolbox.kv
的模板,使其看起来像:
问:我怎样才能附加一个徽标,而不是当前那里的按钮,并且还可以显示当前日期时间(DD/MM/YYYY HH:MM) showed perpetually.And 最后添加一个字符串 NYC, New York, USA
,如图所示。
如果您的图片来自网络,有些人会使用 BoxLayout 和 Image 或 AsyncImage。
因此 python 代码可能如下所示。
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.clock import Clock
import time
class MyLayout(BoxLayout):
your_time = StringProperty()
def __init__(self,**kwargs):
super(MyLayout,self).__init__(**kwargs)
self.orientation = "vertical"
self.padding = 10
Clock.schedule_interval(self.set_time, 0.1)
def set_time(self,dt):
self.your_time = time.strftime("%m/%d/%Y %H:%M")
class MyApp(App):
def build(self):
return MyLayout()
if __name__ == "__main__":
MyApp().run()
kv 文件看起来像这样。
#:kivy 1.9.1
<MyLayout>:
BoxLayout:
spacing: 10
padding: 10,10,10,0
orientation: "horizontal"
BoxLayout:
orientation: "vertical"
size_hint: 0.3,1
canvas:
Rectangle:
pos: self.pos
size: self.size
AsyncImage
source: 'http://lmsotfy.com/so.png'
Label:
size_hint: 1,0.3
text: root.your_time
color: [0,0,0,1]
Label:
size_hint: 1,0.3
text: "NYC, New York, USA"
color: [0,0,0,1]
Button:
text: ""
BoxLayout:
padding: 10,10,10,0
spacing: 10
size_hint: 1,0.3
orientation: "horizontal"
Button:
text: "Clear"
Button:
text: "Remove"
Button:
text: "Group"
Button:
text: "Color"
Button:
text: "Gestures"
BoxLayout:
padding: 10,10,10,10
size_hint: 1,0.3
Button:
text: "Total figures: 1 Kivy Started"
这看起来像这样:
我正在尝试实现 ComicCreator GUI sample as a template for my own project. The code is easy to follow, but I want to change the toolbox.kv
的模板,使其看起来像:
问:我怎样才能附加一个徽标,而不是当前那里的按钮,并且还可以显示当前日期时间(DD/MM/YYYY HH:MM) showed perpetually.And 最后添加一个字符串 NYC, New York, USA
,如图所示。
如果您的图片来自网络,有些人会使用 BoxLayout 和 Image 或 AsyncImage。
因此 python 代码可能如下所示。
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.clock import Clock
import time
class MyLayout(BoxLayout):
your_time = StringProperty()
def __init__(self,**kwargs):
super(MyLayout,self).__init__(**kwargs)
self.orientation = "vertical"
self.padding = 10
Clock.schedule_interval(self.set_time, 0.1)
def set_time(self,dt):
self.your_time = time.strftime("%m/%d/%Y %H:%M")
class MyApp(App):
def build(self):
return MyLayout()
if __name__ == "__main__":
MyApp().run()
kv 文件看起来像这样。
#:kivy 1.9.1
<MyLayout>:
BoxLayout:
spacing: 10
padding: 10,10,10,0
orientation: "horizontal"
BoxLayout:
orientation: "vertical"
size_hint: 0.3,1
canvas:
Rectangle:
pos: self.pos
size: self.size
AsyncImage
source: 'http://lmsotfy.com/so.png'
Label:
size_hint: 1,0.3
text: root.your_time
color: [0,0,0,1]
Label:
size_hint: 1,0.3
text: "NYC, New York, USA"
color: [0,0,0,1]
Button:
text: ""
BoxLayout:
padding: 10,10,10,0
spacing: 10
size_hint: 1,0.3
orientation: "horizontal"
Button:
text: "Clear"
Button:
text: "Remove"
Button:
text: "Group"
Button:
text: "Color"
Button:
text: "Gestures"
BoxLayout:
padding: 10,10,10,10
size_hint: 1,0.3
Button:
text: "Total figures: 1 Kivy Started"
这看起来像这样: