【问题标题】:How to bind a widget to a value from Kivy App如何将小部件绑定到 Kivy 应用程序中的值
【发布时间】:2018-09-10 05:55:03
【问题描述】:

我有以下 Kivy 应用程序,我正在尝试根据另一个小部件的变量更改标签的文本。

我的意思是,如果类 TestApp 的变量 testing 发生变化,我还想要类 text 的变量值strong>TestLabel 进行更改。

为此,我在 TestLabel 类中创建了一个 BooleanProperty,它指向 TestApp 的 testing 变量 类。问题是,尽管每次按下按钮都会更改此回调,但它从未执行过。

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import BooleanProperty

Builder.load_string('''
<MainApp>:
    orientation: 'horizontal'
    rows: 2

    TestButton:
        text: 'Change value'
        on_release: self.change_value()
    TestLabel:
''') 


class TestLabel(Label):
    testing = BooleanProperty()

    def __init__(self, **kwargs):
        super(TestLabel, self).__init__(**kwargs)
        self.app = App.get_running_app()
        self.testing = self.app.testing
        self.bind(testing=self.changed_value)

    def changed_value(self, _instance, newvalue):
        self.text = str(newvalue)


class TestButton(Button):

    def __init__(self, **kwargs):
        super(TestButton, self).__init__(**kwargs)
        self.app = App.get_running_app()

    def change_value(self):
        self.app.testing = not self.app.testing


class MainApp(BoxLayout):
    pass


class TestApp(App):
    testing = BooleanProperty(False)

    def build(self):
        return MainApp()


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

【问题讨论】:

    标签: python properties kivy bind


    【解决方案1】:

    没有必要在 TestLabel 中创建测试属性,因为当您这样做时:self.bind(testing = self.changed_value) 您正在连接 TestLabeltesting 而不是 TestApptesting ,所以它永远不会绑定后更改测试然后它永远不会调用回调。

    绑定必须使用具有该属性的对象完成,并且在您的情况下,测试属于应用程序,因此您必须使用应用程序。

    from kivy.app import App
    from kivy.uix.button import Button
    from kivy.uix.label import Label
    from kivy.uix.boxlayout import BoxLayout
    from kivy.lang import Builder
    from kivy.properties import BooleanProperty
    
    Builder.load_string('''
    <MainApp>:
        orientation: 'horizontal'
        rows: 2
    
        TestButton:
            text: 'Change value'
            on_release: self.change_value()
        TestLabel:
    ''') 
    
    
    class TestLabel(Label):
        def __init__(self, **kwargs):
            super(TestLabel, self).__init__(**kwargs)
            self.app = App.get_running_app()
            self.app.bind(testing=self.changed_value)
    
        def changed_value(self, _instance, newvalue):
            self.text = str(newvalue)
    
    
    class TestButton(Button):
        def __init__(self, **kwargs):
            super(TestButton, self).__init__(**kwargs)
            self.app = App.get_running_app()
    
        def change_value(self):
            self.app.testing = not self.app.testing
    
    
    class MainApp(BoxLayout):
        pass
    
    
    class TestApp(App):
        testing = BooleanProperty(False)
        def build(self):
            return MainApp()
    
    
    if __name__ == '__main__':
        TestApp().run()
    

    【讨论】:

    • 这是有道理的。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多