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

How do I define what happens when user enter's text in TextInput and presses enter in Kivy?

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

我读到只要您在 TextInput 上按 'Enter' 就会调用函数 on_text_validate()。但是,下面的这段代码似乎无法正常工作。

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

请指导我。

你的问题是 on_text_validate 不是 Screen class 的方法,而是 TextInput 的方法,所以你永远不会调用 [=11] =] ChatBotScreen class,您可以从 TextInput:

on_text_validate 事件中调用该方法
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