Kivy无法用kv语言连接on_enter回调

时间:2012-10-27 14:51:06

标签: python kivy

我最近开始学习Kivy并使用它。今天我遇到了一个问题,我不确定这是Kivy中的错误还是我的误解。这是一个简化版本。

我正在开发一个GUI,它在顶部有一个文本输入用于搜索。我的Kv文件如下所示:

#:kivy 1.0
#:import kivy kivy

<SearchPanel>:
    searchquery: searchquery

    AnchorLayout:
        orientation: 'vertical'
        anchor_x: 'center'
        anchor_y: 'top'
        padding: 10

        TextInput:
            id: searchquery
            text: 'What to search for'
            size_hint_x: 0.98
            size_hint_y: None
            height: 30
            on_enter: root.search()

应用程序代码如下所示:

# -*- coding: utf-8 -*-
import kivy
kivy.require('1.4.1')

from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout


class SearchPanel(AnchorLayout):
    def search(self):
        pass


class ExampleApp(App):
    def build(self):
        return SearchPanel()


if __name__ == '__main__':
    ExampleApp().run()

问题在于,当我运行应用程序时,我得到了这个回溯:

Traceback (most recent call last):
  File "example.py", line 24, in <module>
    ExampleApp().run()
  File "/home/testing/.virtualenvs/kivy2/local/lib/python2.7/site-packages/Kivy-1.4.1-py2.7-linux-x86_64.egg/kivy/app.py", line 523, in run
    root = self.build()
  File "example.py", line 20, in build
    return SearchPanel()
  File "/home/testing/.virtualenvs/kivy2/local/lib/python2.7/site-packages/Kivy-1.4.1-py2.7-linux-x86_64.egg/kivy/uix/anchorlayout.py", line 62, in __init__
    super(AnchorLayout, self).__init__(**kwargs)
  File "/home/testing/.virtualenvs/kivy2/local/lib/python2.7/site-packages/Kivy-1.4.1-py2.7-linux-x86_64.egg/kivy/uix/layout.py", line 60, in __init__
    super(Layout, self).__init__(**kwargs)
  File "/home/testing/.virtualenvs/kivy2/local/lib/python2.7/site-packages/Kivy-1.4.1-py2.7-linux-x86_64.egg/kivy/uix/widget.py", line 113, in __init__
    Builder.apply(self)
  File "/home/testing/.virtualenvs/kivy2/local/lib/python2.7/site-packages/Kivy-1.4.1-py2.7-linux-x86_64.egg/kivy/lang.py", line 1217, in apply
    self._apply_rule(widget, rule, rule)
  File "/home/testing/.virtualenvs/kivy2/local/lib/python2.7/site-packages/Kivy-1.4.1-py2.7-linux-x86_64.egg/kivy/lang.py", line 1346, in _apply_rule
    crule, idmap)})
  File "_event.pyx", line 184, in kivy._event.EventDispatcher.bind (/tmp/easy_install-PMy51_/Kivy-1.4.1/kivy/_event.c:2038)
KeyError: 'enter'

显然我无法将on_enter回调插入根布局,我做错了什么?如果我使用on_enter更改on_text则可行。

1 个答案:

答案 0 :(得分:5)

好的,我发现on_enter不是TextInput事件。

相反,您需要使用仅适用于monoline文本输入的on_text_validate。或者,如果您愿意,可以将小部件的on_text_validate绑定到on_enter函数或方法。