代码之家  ›  专栏  ›  技术社区  ›  RhinoFreak

如何定义当用户在TextInput中输入文本并在Kivy中按enter键时发生的情况?

  •  1
  • RhinoFreak  · 技术社区  · 8 年前

    我正在尝试使用Kivy和Python创建一个简单的聊天机器人应用程序UI,但我还停留在第一阶段。 如何访问BoxLayout中的TextInput小部件以获取其内容?

    我读到函数 on\u text\u validate() 每当您在TextInput上按“Enter”时调用。然而,下面的代码似乎不起作用。

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.label import Label
    from kivy.uix.textinput import TextInput
    from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition
    from kivy.uix.scrollview import ScrollView
    from kivy.properties import StringProperty
    from kivy.lang import Builder
    
    Builder.load_string('''
    #:import AnchorLayout kivy.uix.anchorlayout
    #:import Layout kivy.uix.layout
    <ChatBotScreen>:
        BoxLayout:
            orientation: 'vertical'
    
            ScrollView:
                Label:
                    size_hint_y: None
                    height: self.texture_size[1]
                    text_size: self.width, None
                    botOutput: root.botOutput
            ScrollView:
                Label:
                    size_hint_y: None
                    height: self.texture_size[1]
                    text_size: self.width, None
                    userInput: root.userInput
            TextInput:
                id: ti_userinput
                multiline: False
    ''')
    
    class ChatBotScreen(Screen):
        userInput = StringProperty()
        botOutput = StringProperty()
    
        def on_text_validate(self):
            text_input_userInput = self.ids['ti_userinput'].text
            self.ids['ti_userinput'].text = ''
            print(text_input_userInput)
    
        def UserInput(self):
            pass
        def BotOutput(self):
            pass
    
    sm = ScreenManager(transition=FadeTransition())
    sm.add_widget(ChatBotScreen(name='mainchat'))    
    
    class MyApp(App):
        def build(self):
            return sm
    

    请引导我。

    1 回复  |  直到 8 年前
        1
  •  1
  •   eyllanesc    8 年前

    你的问题是 on_text_validate 不是 Screen 类别,但属于 TextInput ,所以你永远不会打电话 on\u text\u验证 的 ChatBotScreen 类,您可以做的是从 on\u text\u验证 的事件 文本输入框 :

    Builder.load_string('''
    #:import AnchorLayout kivy.uix.anchorlayout
    #:import Layout kivy.uix.layout
    <ChatBotScreen>:
        BoxLayout:
            orientation: 'vertical'
    
            ScrollView:
                Label:
                    size_hint_y: None
                    height: self.texture_size[1]
                    text_size: self.width, None
                    botOutput: root.botOutput
            ScrollView:
                Label:
                    size_hint_y: None
                    height: self.texture_size[1]
                    text_size: self.width, None
                    userInput: root.userInput
            TextInput:
                id: ti_userinput
                multiline: False
                on_text_validate: root.on_text_validate()
    ''')
    
    class ChatBotScreen(Screen):
        userInput = StringProperty()
        botOutput = StringProperty()
    
        def on_text_validate(self):
            text_input_userInput = self.ids['ti_userinput'].text
            self.ids['ti_userinput'].text = ''
            print(text_input_userInput)
    
    sm = ScreenManager(transition=FadeTransition())
    sm.add_widget(ChatBotScreen(name='mainchat'))    
    
    class MyApp(App):
        def build(self):
            return sm