本文介绍了Kivy ScreenManager-更新标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了一个有关更新标签文本的问题(使用python更改标签文本)-已回答并且正在运行,但是我已经尝试了几个小时,试图将运行的代码调整为ScreenManager,这样我可以有多个屏幕.我已将代码缩减为无法正常工作的基本内容.

I asked a question regarding updating label text (Kivy change label text with python) - it was answered and was working but I've been trying for hours trying to adapt my running code to ScreenManager so I can have multiple screens. I've cut down the code to the basic stuff I can't get working.

ScreenManager代码破坏了解决方案,我知道问题是什么,但我无法解决.代码在外壳中正常工作,文本和时间在更新,但标签未更新,我无法将return self.first_screen添加到Kivy def build中,因此它未绑定到the_time: _id_lbl_time-任何帮助/指针都将非常感谢. /p>

ScreenManager code breaks the solution, I know what the issue is I just can't resolve it. The code is working, text and time updating in the shell but not the labels, I couldn't add return self.first_screen to the Kivy def build so its not binding to the_time: _id_lbl_time - Any help/pointers would be really appreciated.

from kivy.app import App
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.properties import ListProperty, StringProperty, ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen, WipeTransition, FadeTransition
from  kivy.uix.anchorlayout import AnchorLayout
from kivy.clock import Clock
import time
from datetime import datetime, timedelta


class FirstScreen(Screen):
  def update_time(self, sec):
    MyTime = time.strftime("%H:%M:%S")
    print  MyTime # to test update_time is called etc
    self.the_time.text = MyTime #<---- self.the_time isn't working

class SecondScreen(Screen):
  def update_text(self, sec):
    MyText = 'New Text'
    print  MyText # to test update_text is called etc
    self.the_set.text = MyText #<---- self.the_set isn't working

class MyScreenManager(ScreenManager):
    pass

Builder.load_string('''

<FirstScreen>:
    name: 'first'
    the_time: _id_lbl_time

    BoxLayout:
        orientation: 'vertical'
        Label
            id: _id_lbl_time
            text: 'Clock'
            font_size: 30
        BoxLayout:
            Button:
                text: 'New Here'
                font_size: 20
                size: 200, 50
                size_hint: None, None
                on_release: app.root.current = 'second'

<SecondScreen>:
    name: 'second'
    the_set: _id_lbl_set

    BoxLayout:
        orientation: 'vertical'
        Label:
            id: _id_lbl_set
            text: 'New Here'
            font_size: 30
        BoxLayout:
            Button:
                text: 'Main'
                font_size: 20
                size: 200, 50
                size_hint: None, None
                on_release: app.root.current = 'first'
''')
class ScreenManagerApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(FirstScreen(name='first'))
        sm.add_widget(SecondScreen(name='second'))
        self.first_screen = FirstScreen()
        self.sec_screen = SecondScreen()
        return sm #<---- I can't return self.first_screen etc here?

    def on_start(self):
        Clock.schedule_interval(self.first_screen.update_time, 1)             # 1 second
        Clock.schedule_interval(self.sec_screen.update_text, 2)

ScreenManagerApp().run()

推荐答案

不.在Screen小部件顶部必须有一个ScreenManager,否则它只是一个RelativeLayout,正如其在源代码中的定义一样.

No. There has to be a ScreenManager on top of the Screen widgets, otherwise it's just a RelativeLayout as is its definition in the source code.

您遇到了初学者的一个非常常见的错误.您将一个实例FirstScreen()SecondScreen()用于ScreenManager(可见),然后使用以下命令创建了另一个实例:

You experienced a really common mistake by beginners. You used one instance FirstScreen() and SecondScreen() for ScreenManager(those are visible) and then you created another instances with:

self.first_screen = FirstScreen()
self.sec_screen = SecondScreen()

您随后将其用于Clock:

Clock.schedule_interval(self.first_screen.update_time, 1)
Clock.schedule_interval(self.sec_screen.update_text, 2)

,这意味着在实例中的text属性实际上是未添加到任何地方的,因为使用了小部件进行更新.实例存在,因此除了可见的实例外没有错误可触发,因为您使用了错误的实例,所以您看不到这些值.

and that means text properties in instances that actually aren't added anywhere as widgets were used for updating. The instances exist, so no error to trigger except the visual one → you don't see the values, because you used wrong instance.

正确的build():

def build(self):
    sm = ScreenManager()
    self.first_screen = FirstScreen(name='first')
    self.sec_screen = SecondScreen(name='second')
    sm.add_widget(self.first_screen)
    sm.add_widget(self.sec_screen)
    return sm

这篇关于Kivy ScreenManager-更新标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!