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

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

问题描述

因此,我已经在kivy用户支持(Google网上论坛)上问过这个问题,但是还没有得到任何答复,所以我将在这里尝试.

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天全站免登陆