In Jupyter Lab when the ipywidgets.IntSlider object is selected I can use the arrow keys to change its value interactively. Is it possible to select the IntSlider widget (or any widget) by running some code so that I don't have to click on it to be able to interact with it? Please see sample code below.
Use-case: I'm using some functionalities to set the value of an IntSlider. It would be nice to be able to interact with it using the arrow keys right after the value change without having to click on it.
import ipywidgets as w
w_int = w.IntSlider(
value=7,
min=0,
max=10,
)
display(w_int)
# How can I select widget w_int at this point?
Good question. Unfortunately I don't think is currently possible.
The technical limitation is that there can be arbitrary number of views of the same widget displayed. So you could easily do:
display(w_int)
display(w_int)
display(w_int)
in which case there is no obvious way of ipywidgets to know which one you want to focus on.
Related
I'm new to python and dont understand tag and cell metadata. I want to add button to hide cell input code in jupyter notebook itself, as shown in: https://jupyterbook.org/interactive/hiding.html > Hide cell inputs. Can someone direct me please.
If you want to add a button on the notebook you must have an extension fir that.
You can use an existing one like https://github.com/osscar-org/jupyterlab-hide-code or do it yourself.
Otherwise you can use the 'Collapse' commands in the 'view' menu.
I am trying to get output from my ipywidgets widgets in Microsoft Azure Notebooks running Jupyter Notebooks in Python 3.6. However, it does not return new values when I get them. This also applies to the event handlers/interact never being called for other widgets.
I have tried putting in different initial values, using different types of widgets (Text, Textarea, Checkbox, Button, ToggleButton). I have tried getting the w.value, ipywidgets.interact, w.observe, and w.on_click on Buttons.
A test that I did:
import time
import ipywidgets as widgets
from IPython.display import display
w = widgets.Text(disabled=False)
display(w)
while True:
print(w.value)
time.sleep(1)
I expect that when I enter something into the Text field, that it will output that text, but instead it continues printing out what it started with. There are no errors. So, for the above example, regardless of what I input into the resultant Text field, all that is printed is empty lines.
The problem is that communication between widgets and the Python
kernel is asynchronous and confusing.
time.sleep(...) in the cell only blocks the Python interpreter and does
not allow the widget Javascript implementation to send the changed value to the Python
kernel (because the Python kernel is blocked and not doing anything).
If you create the widget and then modify the widget text entry and then evaluate
w.value in the next cell interactively you will see the changed value.
See further discussion here (look for "async"):
https://github.com/AaronWatters/jp_proxy_widget/blob/master/notebooks/Tutorial.ipynb
In general if you want to force the Python interpreter to see some value sent from the Javascript widget implementation the Javascript side must call back to the Python interpreter in some way and the Python interpreter cannot be blocked by sleep or any other such mechanism.
This code doesn't work as intended in a normal notebook server, so probably won't work in Azure either. I suspect you need a thread process to read from the updated widget. Try this and see if you get anything printing in Azure Notebooks as you change the text field.
import time
import ipywidgets as widgets
from IPython.display import display
w = widgets.Text(disabled=False)
display(w)
def print_text(widget):
print(widget['new'])
w.observe(print_text, names='value')
I am using ipywidgets to create text boxes as described in the documentation. That works fine, but I want to create text boxes which show just dots/stars/whatever when an user gives some input, like for a password input box.
Is that possible to do with ipywidgets, and if so, how? The documentation is a bit scarce I would say...
It appears to be missing in the documentation, but it does exist, we'll need to fix that.
import ipywidgets
password_widget = ipywidgets.Password(description='Password:', placeholder='Make it long!')
password_widget
In another cell, you can print out it's value:
password_widget.value
I am currently working on a project using Python and tkinter.
The problem is that I don't know what's the proper way to display multiple windows, or screens, I don't know how to call them. Let me explain better.
When the application starts the login screen appears. After that, if I click register, I want to go to the register screen, but I don't want it to be a separate window (I don't want to have 2 windows displayed at the same time), but rather another window with different content ?!
How should I handle properly this situation? Create a second window using Toplevel and hiding the first (can I do that?) or changing the widgets of the first?
Code I've written so far
You can do that- just call window.withdraw() on the Toplevel you need to hide after creating a new Toplevel. Changing the widgets in the first is also an option- if you like, you could always try a Notebook widget and disable manual flipping or just put each "screen" in a frame and grid_ or pack_forget them to remove them from the window.
I am new to wxPython and trying out some examples. I am using wxFormBuilder to create a simple GUI with many wxTextCtrl. I want to determine the current location of cursor, which is in one of those wxTextCtrl and do some operation. How do I do this? Please help!
Try using the wx.Frame's FindFocus() method. That should return the widget that has focus.
See also:
wxPython: How do I find out which widget has the focus?