【问题标题】:How do I define what happens when user enter's text in TextInput and presses enter in Kivy?如何定义当用户在 TextInput 中输入文本并在 Kivy 中按 Enter 时会发生什么?
【发布时间】:2018-02-21 16:29:58
【问题描述】:

我正在尝试使用 Kivy 和 Python 创建一个简单的聊天机器人应用程序 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

请指导我。

【问题讨论】:

  • 我的问题是:你想什么时候得到文本?
  • @eyllanesc 我希望在输入 TextInput 字段后按 Enter/Return 键时获取文本。所以每次用户这样做时,我都希望更新值(调用函数)。
  • 你想更新什么值?你的意思是什么功能?
  • @eyllanesc 我想要执行 on_text_validate 函数中的任何内容。所以我将文本的值存储在 text_input_userInput 变量中,打印它并清除当前的 TextInput 文本。

标签: python oop user-interface kivy kivy-language


【解决方案1】:

你的问题是on_text_validate不是Screen类的方法,而是TextInput的方法,所以你永远不会调用ChatBotScreen类的on_text_validate,你可以做的是从TextInputon_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

【讨论】:

  • 这很有意义!非常感谢你的帮助!如果你不介意,我还有另一个稍微不相关但很小的问题。如何在特定的小部件层次结构中使用 python 代码实例化/创建小部件?例如在第二个 ScrollView 中实例化另一个标签? (基本上像聊天框消息一样显示用户的输入)。再次感谢!
  • @RhinoFreak 我不明白您想要什么,我建议您尝试解决它,查找信息,设计和构建您的代码,然后在完成所有涉及大量时间和精力的工作之后提出一个问题,它将有助于增强您的知识。 :D
猜你喜欢
  • 2011-10-03
  • 2013-11-20
  • 2017-11-08
  • 2016-07-11
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
相关资源
最近更新 更多