【问题标题】:Kivy TextInput: TypeError: on_enter() takes exactly 2 arguments (1 given)Kivy TextInput:TypeError:on_enter() 正好需要 2 个参数(给定 1 个)
【发布时间】:2017-11-06 13:36:54
【问题描述】:

我有以下代码(几乎是直接从https://kivy.org/docs/api-kivy.uix.textinput.html复制过来的):

   18 def on_enter(instance, value):
   19     print('User pressed enter in', value)
          ...
   83 class UserInterface(BoxLayout):
   84     def __init__(self, SomeStringList, **kwargs):
   85         super(UserInterface, self).__init__(**kwargs)
              ...
   152    def callback(self, instance):
              ...
   205        textinput = TextInput(text='Hello world', multiline=False)
   206        textinput.bind(on_text_validate=on_enter)

当我运行它时,我得到以下错误

   TypeError: on_enter() takes exactly 2 arguments (1 given)

【问题讨论】:

    标签: python user-interface kivy


    【解决方案1】:

    on_text_validate 不返回两个参数,而是返回一个对象。您可以使用以下代码访问文本:

    18 def on_enter(instance):
    19     print('User pressed enter in', instance.text)
    

    你还可以打印更多

    18 def on_enter(instance):
    19     print('User pressed enter in', instance.text, instance.id, instance.tab_width) 
              ...
    205        textinput = TextInput(id="myID", text='Hello world', multiline=False)
    206        textinput.bind(on_text_validate=on_enter)
    

    这将打印以下内容:

    ('小部件', 'have:', u'', myID, 4)

    https://kivy.org/docs/api-kivy.uix.textinput.html查看更多属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-17
      • 2015-01-21
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      相关资源
      最近更新 更多