我怎样才能在我所有的屏幕上贴上一个带有时间的标签

2024-09-29 02:15:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我从奇维开始,有一些问题。我有两个屏幕和按钮的代码。我怎么能在我所有的屏幕上贴上一个时间标签或类似的东西呢?在下面的代码中,我创建了一个屏幕,但我不想在单个屏幕上放置3个单独的标签

你知道吗主.py你知道吗

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import StringProperty, ObjectProperty
from kivy.clock import Clock
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen

Builder.load_string("""
<StartScreen>:
    BoxLayout:
        orientation: 'vertical'
        Button:
            text: 'Start >'
            size_hint_y: None
            hight: '40dp'
            on_press: root.manager.current = 'second'

<SecondScreen>:
    BoxLayout:
        orientation: 'vertical'
        Button:
            text: 'Test2'
            size_hint_y: None
            hight: '40dp'
            on_press: root.manager.current = 'end'

<EndScreen>:
    BoxLayout:
        Button:
            text: 'Test3'
            size_hint_y: None
            hight: '40dp'
            on_press: root.manager.current = 'start'
""")

#declarate both screens
class StartScreen(Screen):
        pass

class SecondScreen(Screen):
        pass

class EndScreen(Screen):
        pass

#create the screen manager
sm = ScreenManager()
sm.add_widget(StartScreen(name='start'))
sm.add_widget(SecondScreen(name='second'))
sm.add_widget(EndScreen(name='end'))

class TutorialApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    TutorialApp().run()

Tags: textnamefromimport屏幕managerbuttonwidget
1条回答
网友
1楼 · 发布于 2024-09-29 02:15:48

这是一个基于https://stackoverflow.com/a/18926863/6646710的可能解决方案

下面的代码定义了一个显示时间的标签。你知道吗

import time

class IncrediblyCrudeClock(Label):
    def __init__(self, **kwargs):
        super(IncrediblyCrudeClock, self).__init__(**kwargs)
        Clock.schedule_interval(self.update, 1)

    def update(self, *args):
        self.text = time.asctime()

update函数从pythontime模块获取当前时间。时间用于更新标签的text属性。在init方法中,clock模块用于每秒调度对此更新函数的调用。你知道吗


下一步,我把这个标签添加到你所有的克维屏幕内的千伏字符串。你知道吗

<StartScreen>:
    BoxLayout:
        orientation: 'vertical'
        Button:
            text: 'Start >'
            size_hint_y: None
            hight: '40dp'
            on_press: root.manager.current = 'second'
        IncrediblyCrudeClock:

<SecondScreen>:
    BoxLayout:
        orientation: 'vertical'
        Button:
            text: 'Test2'
            size_hint_y: None
            hight: '40dp'
            on_press: root.manager.current = 'end'
        IncrediblyCrudeClock:

<EndScreen>:
    BoxLayout:
        Button:
            text: 'Test3'
            size_hint_y: None
            hight: '40dp'
            on_press: root.manager.current = 'start'
        IncrediblyCrudeClock:

最后的结果是enter image description here

相关问题 更多 >