python - 如何将小部件绑定(bind)到 Kivy 应用程序中的值

标签 python properties kivy bind

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

我的意思是,如果TestApp类的变量testing发生变化,我还想要类text的变量值<更改测试标签。

为此,我在 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()

最佳答案

没有必要在TestLabel中创建测试属性,因为当您执行以下操作时:self.bind(testing = self.changed_value)您正在连接 TestLabel 的测试 而不是 TestApptesting ,因此由于它在绑定(bind)后永远不会更改测试,因此它永远不会被调用回调。

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

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()

关于python - 如何将小部件绑定(bind)到 Kivy 应用程序中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49587156/

相关文章:

python - 检测网球场线拦截

iphone - 从txt列表文件中读取并在ios中设置许多对象

php - 未定义的属性:Illuminate\Database\Eloquent\Collection::$id Laravel 4

java - 使用spring读取属性表单文件

python - 无 initscrip 名称控制台 - CX_freeze

python - 他们如何在 Django 项目中的 python 控制台中运行这些命令?

python - 对某些列中的 pandas 数据帧进行平均

python - 使用 Builder.load_string 在 Kivy 应用程序上显示变量

python - 是否有在 cygwin 上安装 Kivy 的说明?

python - Kivy TextInput 水平和垂直对齐(居中文本)