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



我的目标是观看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

from 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 import ObjectProperty
from 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)


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

        mypopup = Popup(content = content,              
                title = "Score",     
                auto_dismiss = False,         
                size_hint = (.7, .5),         
                font_size = 20)

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

        ins.a = scoreInc

        if(scoreInc >= 10):
            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()

class MyApp(App):     

    def build(self):

        mypopup = MyPopup()

        return mypopup.show_popup()

if __name__ == "__main__":



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)



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


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


登录 关闭
发送“验证码”获取 | 15天全站免登陆