Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9129689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:48:22+00:00 2026-06-17T07:48:22+00:00

so I’ve asked this question on kivy user support (google groups) but haven’t got

  • 0

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

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:

In my .kv file I have the textinput widget:

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

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)

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:48:23+00:00Added an answer on June 17, 2026 at 7:48 am

    Bind an event type or a property to a callback

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

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

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

    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
    

    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'
    >>>
    

    In your code::

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

    You are calling the function

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

    Instead of passing the function

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

    call from functools import partial before hand

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.