不能在kivy

时间:2018-05-03 17:19:30

标签: python-3.x kivy

我想创建一组widget的动态类,所以每当我将它们添加到我的主应用程序时,我只需要从某个地方引入更改(例如python文件,类等,这里我已经完成了它们在显式列表中),但是将窗口小部件与响应事件的“on_text”等属性绑定并没有真正起作用,其他属性如'text','hint_text'工作得很好,但是on_text并没有真正让步。我无法找出原因,因为我已经检查了正确的对象传递函数,下面是我的代码:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.properties import ObjectProperty



class Dynamic_TextInput(BoxLayout):
    def __init__(self,changes=None, **kwargs):
        super().__init__(**kwargs)
        self.widgets = {'Mylabel':self.ids.mylabel,
                        'Myinput': self.ids.mytext}
        self.Change(changes)


    def Change(self, changes=None, **kwargs):
        if changes: 
            for change in changes:
                curwidget = self.widgets[change[0]]
                cur_properties = change[1]
                for attr in cur_properties.keys():
                    if attr=='bind':
                        print("The cur properties are: ")
                        print(cur_properties[attr])
                        curwidget.bind(**(cur_properties[attr]))
                    else: 
                        setattr(curwidget, attr, cur_properties[attr])  



class mainwidget(BoxLayout):
    myobj1 = ObjectProperty()
    myobj2 = ObjectProperty() 
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.orientation='vertical'


        change1=[('Mylabel', {'text':'firstchange',
            'bind':{'on_text':lambda *_: print('something')}
            }),
                    ('Myinput', {'hint_text': 'Changed hint text'})
                ]

        self.add_widget(Dynamic_TextInput(changes=change1))
        self.add_widget(Dynamic_TextInput())


class MainApp(App):
    def build(self):
        return mainwidget()


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

并且,在kivy文件中:

#:kivy 1.10.0


<Dynamic_TextInput>:
    myobj1: mylabel
    myobj2: mytext
    orientation: 'horizontal'
    Label:
        id: mylabel
        text: 'testlable'
    TextInput:
        id: mytext
        hint_text: 'some test'   

原因是什么?我该如何解决?

1 个答案:

答案 0 :(得分:1)

有两种方法可以绑定属性:

<强> *。PY

object.bind(property=callback)

<强> *。KV

object:
    on_property: callback    

因此,如果在python中建立连接,则不应使用on_text,而应使用text

要验证更改,无法从GUI编辑Label,因此我将使用TextInputLabel上书写:

<强> *。PY

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.properties import ObjectProperty


class Dynamic_TextInput(BoxLayout):
    def __init__(self,changes=None, **kwargs):
        super().__init__(**kwargs)
        self.widgets = {'Mylabel':self.ids.mylabel,
                        'Myinput': self.ids.mytext}
        self.Change(changes)


    def Change(self, changes=None, **kwargs):
        if changes: 
            for change in changes:
                curwidget = self.widgets[change[0]]
                cur_properties = change[1]
                for attr in cur_properties.keys():
                    if attr=='bind':
                        print("The cur properties are: ")
                        curwidget.bind(**(cur_properties[attr]))
                        print(cur_properties[attr])
                    else: 
                        setattr(curwidget, attr, cur_properties[attr])  


class mainwidget(BoxLayout):
    myobj1 = ObjectProperty()
    myobj2 = ObjectProperty() 
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.orientation='vertical'


        change1=[('Mylabel', {'text':'firstchange',
            'bind':{'text':lambda *_: print('something')}
            }),
                    ('Myinput', {'hint_text': 'Changed hint text'})
                ]

        self.add_widget(Dynamic_TextInput(changes=change1))
        self.add_widget(Dynamic_TextInput())


class MainApp(App):
    def build(self):
        return mainwidget()


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

<强> *。KV

#:kivy 1.10.0


<Dynamic_TextInput>:
    myobj1: mylabel
    myobj2: mytext
    orientation: 'horizontal'
    Label:
        id: mylabel
        text: mytext.text # <----
    TextInput:
        id: mytext
        hint_text: 'some test'