【问题标题】:How to add an image and clock to a kv file如何将图像和时钟添加到 kv 文件
【发布时间】:2016-11-15 10:07:06
【问题描述】:

我正在尝试将ComicCreator GUI 示例的模板实现为我自己项目的模板。 code 很容易理解,但我想将 toolbox.kv 更改为如下所示:

问:我怎样才能附加一个徽标,而不是当前那里的按钮,并且还永久显示当前日期时间显示 (DD/MM/YYYY HH:MM)。最后添加一个字符串NYC, New York, USA,如图所示。

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    如果您的图片来自网络,则可以使用 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"
    


    这将如下所示:

    【讨论】:

    • 谢谢你,我的问题是如何打印当前时间而不仅仅是“DD/MM/YYYY HH:MM”,
    • @3kstc 好的,你从哪里得到时间变量?
    • 这是我的关键问题 - 尽管阅读了各种 examples,但我只是不明白如何在我的代码中实现它。
    • @3kstc 已更新。认为这就是你想要的
    • @3kstc 你会接受这个答案:),还是你在寻找别的东西?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多