【问题标题】:kivy, how to trigger event by text changekivy,如何通过文本更改触发事件
【发布时间】:2017-11-30 19:38:07
【问题描述】:

一些 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 事件,如果是,我该如何实现我的目标?

【问题讨论】:

    标签: python kivy


    【解决方案1】:

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

    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 创建自动在属性更改时调用的回调:

    • 基维朗格耶:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      • 2010-11-01
      • 2022-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多