Kivy 按钮循环绑定 on_press 以回调 [英] Kivy button loop bind on_press to call back

查看:18
本文介绍了Kivy 按钮循环绑定 on_press 以回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在 kivy 用户支持(谷歌群组)上问过这个问题,但还没有得到任何答复,所以我会在这里尝试.

so I've asked this question on kivy user support (google groups) but haven't got any replies yet, so I'll try here.

我有一组基于搜索工具创建的按钮.基本上它的工作方式是用户在文本输入框中输入搜索词,并根据输入文本,我的程序在数据库中搜索匹配的结果.如果有任何匹配的结果,就会创建按钮(将其文本作为匹配结果的文本),这非常有效.但我的问题是,当在循环中创建按钮时,如何将它们各自的 on_press 分配给回调?例如,在我的例子中,代码如下所示:

I've got a set of buttons which are created based on a search tool. Basically the way it works is that the user inputs a search term in a textinput box and based on the input text, my program searches a database for matched results. If there are any matched results, buttons are created (with their text as the text of the matched result) and this works very well. But my question is when buttons are created in a loop, how does one assign their individual on_press to call backs? For example in the my case, the code looks something like this:

在我的 .kv 文件中,我有 textinput 小部件:

In my .kv file I have the textinput widget:

<my rule>:
    ti: Ti
    TextInput:
        id: Ti
    on_text: root.function()

在我的 .py 文件中,我有以下内容(加上一些其他代码):

in my .py file I have the following (plus some other code):

t1 = ObjectProperty()
function():
    layout = StackLayout(orientation = 'lr-tb', size_hint = (0.3, 0.8), pos_hint =   {'top' : 0.87})

    root.clear_widgets() #added this so that upon user input (i.e. on_text event of textinput) only matching results create new buttons

    list_1 = ['a', 'b', 'c'] #this is a list of matching results
    root.add_widget(layout)

    for item in list_1: #this is the loop which creates the buttons
        buttons = Button(text = str(item), size_hint = (1, 0.1), background_color = [255, 0, 0, 1])
        buttons.bind(on_press = self.t1.insert_text(str(item)))                   
        layout.add_widget(buttons)

分配给回调的 on_press(如上所示)实际上不起作用.它应该完成的是,当用户按下该按钮时,textinput 小部件(self.ti1)中的文本应该更改为按钮文本(即按钮作为自动填充小部件工作).我究竟做错了什么?请注意,以上只是部分代码.主要代码的结构应如此,唯一的问题在于上面的代码片段.
谢谢!

The on_press assigned to the callback (as shown above) doesn't really work. What it is supposed to accomplish is that when the user presses that button, the text in textinput widget (self.ti1) is supposed to change to the button text (i.e. the buttons work as an auto-fill widget). What am I doing wrong? Note that the above is only part of the code. The main code is structured as it should be, the only issue lies in the above snippet.
Thanks!

推荐答案

绑定一个事件类型或一个属性到一个回调

Bind an event type or a property to a callback

self.bind(x=my_x_callback, width=my_width_callback,...)

所以 x 应该是 事件属性,以及my_x_callback 需要是对 callback/method

So x should be a event or a property, and my_x_callback needs to be the reference to a callback/method

my_method() 和 my_method 有什么区别?

What's the difference between my_method() and my_method?

让我们在控制台上执行此操作,考虑以下代码中的方法 what_up::

let's do this on the console, consider the method what_up in the following code::

python<enter>
Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def what_up():
...     return 'nothing man, same old stuff' 
... 
>>> print what_up
<function what_up at 0x7f8df8a76a28>
>>> print what_up()
nothing man, same old stuff

从上面的结果可以看出,如果你直接打印 what_up 你会得到一个方法的引用,另一方面如果你调用方法 what_up() 你会得到函数返回的任何东西,如果它什么也不返回,你会得到 None.

As you can see from the results above, if you print what_up directly you get a ref to the method, on the other hand if you call the method what_up() you get whatever the function returns or None if it returns nothing.

您可以将函数的引用传递给任何变量,例如 ::

You can just pass the reference of the function to any variable like ::

>>>my_up = what_up
>>> my_up()
'nothing man, same old stuff'
>>>

在你的代码中::

buttons.bind(on_press = self.t1.insert_text(str(item))) 

你正在调用函数

on_press = self.t1.insert_text(str(item))

而不是传递函数

on_press = partial(self.t1.insert_text, str(item))

在手前调用 from functools import partial

这篇关于Kivy 按钮循环绑定 on_press 以回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆