question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

button.on_click with parameters

See original GitHub issue

In the example of “Widgets Events” of the ipywidgets doc, I knew that button.on_click can call a function without parameters. But suppose that I define a str variable a, and a function def fun(a): print(a) I would like to call this ‘fun’ function when I click the button, but I don’t know how to give the parameter a to the function fun. Thank you for your kind help!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
jtpiocommented, Jun 23, 2018

How about calling the fun function in the on_button_clicked function?

import ipywidgets as widgets
from IPython.display import display
button = widgets.Button(description="Click Me!")
display(button)

def fun(a):
    print(a)

def on_button_clicked(b):
    fun('a')

button.on_click(on_button_clicked)
2reactions
zmk5commented, Oct 24, 2019

@rishabhmulani It would be something like this:

import functools

def on_button_clicked(b, rs_="some_default_string"):
    fun(rs_)

button.on_clicked(functools.partial(on_button_clicked, rs_="abcdefg"))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Pass a string parameter in an onclick function - Stack Overflow
I would like to pass a parameter (i.e. a string) to an Onclick function. with result.name for example equal to string "Add". When...
Read more >
Call a function with an argument - W3Schools Tryit Editor
<p>Click "Try it" to call a function with arguments</p> ​ <button onclick="myFunction('Harry Potter','Wizard')">Try it</button> ​ <p id="demo"></p>
Read more >
JavaScript | Pass string parameter in onClick function
Example 2: This example simply put the argument which is a string in the onClick attribute of the button which calls a function...
Read more >
How To Pass A Parameter Through onClick In React?
Here is how to make it work: Create the first function that accepts the parameter you want to pass and return the second...
Read more >
Pass parameter to JavaScript function onclick in HTML
Pass parameter to JavaScript function onclick in HTML | Example code ... It's very easy to Pass parameter to JavaScript function using the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found