0

I'm sorry if this question has already been answered, but I couldn't find anything on here (except this one which hasn't been answered: link and one that doesn't quite answer the question i'm asking link

I'm creating a button with ipywidgets, but the script doesn't wait for the button to be clicked. Here's a sample of the code/what I'm trying to achieve:

button = widgets.Button(description = "Click")
output = widgets.Output()
display(button, output)

def on_button_clicked(b):
     with output:
     print("button clicked")

button.on_click(on_button_clicked)
print("Python is ignoring you")

If I run this, I'll just get "Python is ignoring you" before I click the button. I want it to display the button, wait for the person to click it, and only then execute the rest ("Python is ignoring you").

Does anyone have an idea of how I can make python wait for the button to be clicked?

Would appreciate any help! Thanks

  • If you have *lots* more code to run, then you will need to place all of this code inside functions. The `button.on_click` just makes the connection between what *will* happen if you do click the button. There is no way to suspend the body of code being run. – ac24 Sep 08 '20 at 08:21

1 Answers1

1

I never used ipywidgets but the problem is on the last line

you're telling python to print "Python is ignoring you" and python is doing it.

It will not wait for the user to click button,

because the print statement is out the function "on_button_clicked".(or any)

So just Put it in the function.(That print Statement)

Hope this helps :D

If You want i can make this same program in Tkinter,I can do that for you let me know if you want :)

Nili
  • 11
  • 4
  • Thanks! For now I just put a random input command that makes you press enter after the button click. Good enough for what I'm trying to achieve :) – david_abaev Sep 10 '20 at 17:44