0

I want to use my Xbox controller in Jupyter environment. I initialized controller, there widget said press any button, then, widget started displaying all of my controls. as on Screenshot. enter image description here

However when i tried to subscribe with observe method but it didnt notify any at all, tried look into fields of axes, buttons, connectedit was empty and False. Perhaps i am using wrong interface or may be widget supports only one observer since all actions are displayed properly on widget display. I attach my code below.

#%%
import ipywidgets.widgets as widgets
from IPython.display import display

c = widgets.Controller(index=0)
display(c)
#%%
def on_change(change):
   print("Axes:", c.axes)
   print("Buttons:", c.buttons)

c.observe(on_change, names=['axes', 'buttons'])
2
  • 1
    "with observe method but it didnt notify any at all". Can you explain more of what you mean by this part? print() isn't the best way these days to work with ipywidgets because unless you handle it well it goes to the Log console. And that is if you are lucky to be working with JupyterLab, which is best for working with ipywidgets at this time. See here, here and ... Commented Jan 11 at 17:01
  • 1
    <continued> here for more about this and some options. It may be as simple as wrapping it in a context manager with with output: as in my first example. As to this widget itself...Unfortunately I don't see many examples of this. This one is from 2017 and things were in flux, plus ipywidgets & Jupyter has dramatically changed in just the last few years. GitHub may have more recent, fleshed-out examples, see here. Commented Jan 11 at 17:02

1 Answer 1

0

Try this:

import ipywidgets.widgets as widgets
from IPython.display import display

c = widgets.Controller(index=0)
display(c)

def on_change(change):
    print("Axes:", c.axes)
    print("Buttons:", c.buttons)

c.observe(on_change)

Output in my case:

Buttons: (Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0))
Axes: (Axis(value=0.00912503432482481), Axis(value=0.04126102477312088), Axis(value=0.037537768483161926), Axis(value=0.005096591077744961))
Buttons: (Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=1.0, pressed=True), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0), Button(value=0.0))
Axes: (Axis(value=0.00912503432482481), Axis(value=0.04126102477312088), Axis(value=0.037537768483161926), Axis(value=0.005096591077744961))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.