kivy,如何通过文本更改触发事件

时间:2017-11-30 19:38:07

标签: python kivy

多个GUI工具箱包括on_change等事件,每次文本框中的文本发生更改时都会触发这些事件。

根据这个: https://kivy.org/docs/api-kivy.uix.textinput.html on_text事件应该相等。因此,我创建了一个TextInput框,期望每次更改单个字母时,该框的内容将显示在终端中。这是代码:

from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout

class LoginScreen(BoxLayout):

    def __init__(self, **kwargs):
        super(LoginScreen, self).__init__(**kwargs)
        self.orientation = 'horizontal'
        self.mytext = TextInput(text='500', multiline = False)
        self.add_widget(self.mytext)

        self.mytext.bind(on_text = self.calc)
        #self.mytext.bind(on_text_validate = self.calc)

    def calc(self, mytext):
        print mytext.text

class MyApp(App):

    def build(self):
        return LoginScreen()

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

然而,没有任何反应,这显然意味着calc功能根本没有被触发。请注意on_text_validate事件正常,因为当我按Enter键时,框中的内容将打印在终端中。

那么,我是否误解了on_text事件,如果是,我怎样才能实现目标?

1 个答案:

答案 0 :(得分:2)

on_text不是TextInput事件。要在文本更改时运行回调,您可以绑定text属性(存储textinput的文本):

from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout

class LoginScreen(BoxLayout):

    def __init__(self, **kwargs):
        super(LoginScreen, self).__init__(**kwargs)
        self.orientation = 'horizontal'
        self.mytext = TextInput(text='500', multiline = False)
        self.add_widget(self.mytext)
        self.mytext.bind(text = self.calc)

    def calc(self, instance, text):
        print(text)

class MyApp(App):

    def build(self):
        return LoginScreen()

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

使用on_<property_name> sintax更改属性时,可以创建自动调用的回调:

  • Kivy Languaje:

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.lang import Builder
    
    Builder.load_string('''\
    <LoginScreen>:
        orientation: "horizontal"
        TextInput:
            text: "500"
            on_text: root.calc(self.text)
    ''')
    
    class LoginScreen(BoxLayout):
        def __init__(self, **kwargs):
            super(LoginScreen, self).__init__(**kwargs)
    
        def calc(self, text):
            print(text)
    
    class MyApp(App):
    
        def build(self):
            return LoginScreen()
    
    if __name__ == '__main__':
        MyApp().run()
    
  • 扩展窗口小部件类:

    from kivy.app import App
    from kivy.uix.textinput import TextInput
    from kivy.uix.boxlayout import BoxLayout
    
    class My_TextInput(TextInput):
        def __init__(self, **kwargs):
            super(My_TextInput, self).__init__(**kwargs)
    
        def on_text(self, instance, text):
            print(text)
    
    class LoginScreen(BoxLayout):
        def __init__(self, **kwargs):
            super(LoginScreen, self).__init__(**kwargs)
            self.mytext = My_TextInput(text='500', multiline = False)
            self.add_widget(self.mytext)
    
    
    class MyApp(App):
    
        def build(self):
            return LoginScreen()