0

I'm trying to create a button with the vpython module (and not tkinter for some reasons) and I have this error : AttributeError: bind missing

What's the bind and how can I fix that ?

from vpython import *
def change(): # Appelé par controls quand on clique sur le bouton
    if b.text == 'Click me':
        b.text = 'Try again'
    else:
        b.text = 'Click me'

c = controls() # Crée une fenêtre pour les contrôles
# Crée un bouton dans la fenêtre des contrôles:
b = button( pos=(0,0), width=60, height=60, 
          text='Click me', action=lambda: change() )
while 1:
    c.interact()
Torxed
  • 22,866
  • 14
  • 82
  • 131
kikizeppelin
  • 15
  • 1
  • 6
  • 1
    Which line gives the error? Also, the whitespace as posted doesn't look correct. Is this exactly the code causing the problem? – doctorlove Jun 12 '18 at 12:19
  • It appears as if you're missing `from visual.controls import *`, just as the official guide says: http://vpython.org/contents/docs/controls.html – Torxed Jun 12 '18 at 12:24
  • Tip: Document your code in english, always. Let's say you make some really useful piece of code and share it, imagine the stuggle that other developers which don't speech that language will have. – Fusseldieb Jun 12 '18 at 12:26
  • 1
    @Fusseldieb : Sorry, you're right. I'll make an effort for the next time – kikizeppelin Jun 12 '18 at 12:52
  • @Torxed : I tried this code before and this error appears: ModuleNotFoundError: No module named 'visual' I look for it on stackoverflow and I found this topic : https://stackoverflow.com/questions/28592211/importerror-no-module-named-visual that said you have to replace visual by vpython but vpython has no module named controls... – kikizeppelin Jun 12 '18 at 12:56
  • @doctorlove : the line text='Click me', action=lambda: change() I copy paste this code to make sure and yes, this is the right code – kikizeppelin Jun 12 '18 at 12:59
  • Now the `change` function doesn't have a `b` in sight - have you missed some parameters to this? – doctorlove Jun 12 '18 at 13:03
  • @doctorlove I try to declare the b in the function change but it raises the error AttributeError: 'controls' object has no attribute 'interact' – kikizeppelin Jun 12 '18 at 13:21

1 Answers1

0

From the following example

https://github.com/BruceSherwood/vpython-jupyter/blob/master/Demos/ButtonsSlidersMenus2.ipynb

There is the line

cbutton = button(text='Red', textcolor=color.red, background=color.cyan, pos=scene.title_anchor, bind=Color)

so it looks like you need to add bind attribute to your button.

b = button( pos=(0,0), width=60, height=60, text='Click me', action=lambda: change(), bind = change )

From the documentation about buttons and widgets in VPython 7 .

http://www.glowscript.org/docs/VPythonDocs/controls.html

it says that bind attribute for button is

"bind The name of the function to be called when the button is clicked. Cannot be changed after creating the button."

john
  • 156
  • 4