Kivy更新动态标签文字 [英] Kivy Update Dynamic Label Text

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

问题描述

我的目标是观看Popup上的数字计数.我正在加载NumericProperty.但是,调用回调时,数字不会更改. (我在回调中没有任何代码链接到label.text)

My goal is to watch the number count on the Popup. I have a NumericProperty being loaded. However, The numbers do not change when callback is called. (I do not have any code in callback linking to the label.text)

有人问过类似的问题.但是,我一直无法看到它们如何适用于这种特定情况. 类似案例

Similar questions have been asked. But, I have been unable to see how they apply to this specific case. Similar Case

import kivy
kivy.require("1.7.0")

from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.properties import NumericProperty
from kivy.clock import Clock
from kivy.event import EventDispatcher

scoreInc = 0

class MyPopup(Popup):

    def show_popup(self):

        content = BoxLayout(orientation="vertical")

        self.incrementerFnc = Clock.schedule_interval(self.incrementer, .005)

        scoreLabel = Label(text=str(ins.a), id='scorelabel', font_size=20)

        content.add_widget(scoreLabel)

        mybutton = Button(text="Close", size_hint=(1,.20), font_size=20)
        content.add_widget(mybutton)

        mypopup = Popup(content = content,              
                title = "Score",     
                auto_dismiss = False,         
                size_hint = (.7, .5),         
                font_size = 20)
        mybutton.bind(on_press=mypopup.dismiss)  
        mypopup.open()

    def incrementer(self, dt):
        global scoreInc
        scoreInc += 1

        ins.a = scoreInc

        if(scoreInc >= 10):
            Clock.unschedule(self.incrementerFnc)
            print('quit')
        else:
            print('scoreInc', ins.a)    

class MyClass(EventDispatcher):
    a = NumericProperty(0)

def callback(instance, value):
    print('My callback is call from', instance)
    print('and the a value changed to', value)

ins = MyClass()
ins.bind(a=callback)


class MyApp(App):     

    def build(self):

        mypopup = MyPopup()

        return mypopup.show_popup()

if __name__ == "__main__":
    MyApp().run()

推荐答案

您缺少一个更新您要在MyClass中处理的scoreLabel文本值的事件,请参见下文:

You are missing an event that updates the text value of your scoreLabel which you need to handle in your MyClass, see below:

class MyClass(EventDispatcher):
    a = NumericProperty(0)
    def on_a(self, instance, value):
        app = App.get_running_app()
        app.scoreLabel.text = str(value)

当属性a更新时,会触发on_a然后,您可以使用它来更新scoreLabel值,否则它甚至不会连接. text = str(ins.a)行从ins.a中获取并为您使用,即0.

when property a updates, on_a is triggered and then you can use it to update the scoreLabel value, otherwise it's not even connected. The line text = str(ins.a) takes the value from the ins.a and uses it i.e. 0 for you.

但是,您需要以某种方式访问​​该scoreLabel,这可能通过例如 App.get_running_app() 可以在其中存储实例供以后使用:

However, you'll need to access that scoreLabel somehow, which might be useful through e.g. App.get_running_app() where you can store the instance for later use:

    app = App.get_running_app()
    app.scoreLabel = Label(text=str(ins.a), id='scorelabel', font_size=20)

    content.add_widget(app.scoreLabel)

这样,您甚至可以在on_a事件以后中访问它.或使用self并直接使用App.get_running_app().popup然后使用其内容访问Popup.

This way you can access it even in the on_a event later. Or use self and access the Popup directly with App.get_running_app().popup and then its content.

App.get_running_app() 但是有时不是最佳选择,因此您甚至可以使用全局变量或其他方式存储实例,例如在其他班级里如果您有Popup,则该小部件会将其自身添加到根Window中,并将其存储在以下位置:

App.get_running_app() however might be not a preferable option sometimes, therefore you can use even globals, or some other way to store the instance e.g. inside some other class. If you have a Popup, that widget adds itself to the root Window for example and it's stored in:

Window -> children -> Popup -> content -> layout -> scoreLabel

但是要小心,因为直接弄乱Window可能会导致不幸的结果.

but be careful with that because messing with the Window directly might have unfortunate results.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆