kivy,如何通过文本更改触发事件 [英] kivy, how to trigger event by text change

查看:114
本文介绍了kivy,如何通过文本更改触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个GUI工具箱包含诸如on_change之类的事件,每次文本框中的文本更改时都会触发这些事件.

Several GUI toolboxes include events such as on_change which are triggered every time the text in a textbox changes.

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

According to this: https://kivy.org/docs/api-kivy.uix.textinput.html the on_text event should be equal. So, I created a single TextInput box expecting everytime a change a single letter, the content of the box to be displayed in the terminal. This is the code:

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键时,该框的内容会在终端中打印出来.

Yet, nothing happens, which obviously means that the calc function is not triggered at all. Mind that the on_text_validate event works fine, because the contents of the box are printed in the terminal when I press Enter.

那么,我是否误解了on_text事件?如果是,我该如何实现我的目标?

So, did I misunderstand the on_text event, and if so, how can I accomplish my goal?

推荐答案

on_text不是

on_text is not a TextInput event. To run a callback when the text changes, you can bind text property (where textinput’s text is stored):

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:

You can create callback that automatic is called when a property change using 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()
    

  • 这篇关于kivy,如何通过文本更改触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆