假设我想要一个由IntText小部件和DropDown小部件组成的小部件,该小部件的值是这些小部件值连接在一起的字符串。我该怎么做呢?
下面是一个尝试:
import re
import ipywidgets as ipw
from IPython.display import display
class IntMultipliedDropdown:
_VALUE_PATTERN = re.compile('(?P<num>\d+) (?P<option>\w+-?\w*)')
def __init__(self, options, option_value, int_value=1):
self.number = ipw.IntText(int_value)
self.options = ipw.Dropdown(options=options, value=option_value)
self.box = ipw.HBox([self.number, self.options])
self.number.observe(self._on_changes, names='value')
self.options.observe(self._on_changes, names='value')
self._handelers = []
def _on_changes(self, change):
for handeler in self._handelers:
handeler(self.value)
@property
def value(self):
return "{} {}".format(self.number.value, self.options.value)
@value.setter
def value(self, value):
match = re.search(self._VALUE_PATTERN, value)
groupdict = match.groupdict()
self.number.value = groupdict['num']
self.options.value = groupdict['option']
def _ipython_display_(self, **kwargs):
return self.box._ipython_display_(**kwargs)
def observe(self, handler):
if handler not in self._handelers:
self._handelers.append(handler)
mywidget = IntMultipliedDropdown(['apple', 'bed', 'cell'], 'cell')
mywidget.observe(print)
display(mywidget)
print('default value:', mywidget.value)
mywidget.value = '2 bed'
它是有效的,但也有缺点。首先,当我设置mywidget.value
时,观察到的函数被调用两次:在数值更改时和在选项值更改时。
第二个也是最糟糕的是,我不能在Box小部件中使用这个小部件,比如:
ipw.HBox([ipw.Label('Mylabel'), mywidget])
这就引出了:
ValueError: Can't clean for JSON: <__main__.IntMultipliedDropdown object at 0x7f7d604fff28>
有没有更好的解决方案?
发布于 2017-02-15 07:35:16
observe
,display
)。这可能就是您无法将其显示在HBox
中的原因。如果您想创建一个新的小部件,可以从ipyw.Widget
或任何其他小部件继承。value
。这就是您可以通过继承HBox
来实现的方法
import re
import ipywidgets as ipw
from traitlets import Unicode
from IPython.display import display
class IntMultipliedDropdown(ipw.HBox):
_VALUE_PATTERN = re.compile('(?P<num>\d+) (?P<option>\w+-?\w*)')
value = Unicode()
def __init__(self, options, option_value, int_value=1, **kwargs):
self.number = ipw.IntText(int_value)
self.options = ipw.Dropdown(options=options, value=option_value)
self._update_value()
self.number.observe(self._update_value, names='value')
self.options.observe(self._update_value, names='value')
self.observe(self._update_children, names='value')
super().__init__(children=[self.number, self.options], **kwargs)
def _update_children(self, *args):
match = re.search(self._VALUE_PATTERN, self.value)
groupdict = match.groupdict()
self.number.value = groupdict['num']
self.options.value = groupdict['option']
def _update_value(self, *args):
self.value = "{} {}".format(self.number.value, self.options.value)
mywidget = IntMultipliedDropdown(['apple', 'bed', 'cell'], 'cell')
display(mywidget)
发布于 2016-11-16 01:02:48
这可能是您不厌其烦地创建一个新小部件的原因,但为什么不使用the interactive
function呢
类似于:
import ipywidgets as ipw
from ipywidgets import *
w_number = ipw.IntText(value = 1)
w_options = ipw.Dropdown(options = ['apple', 'bed', 'cell'], value ='cell')
mywidget_value = ''
def display_value(number, options):
mywidget_value = str(number)+' '+options
#print(mywidget_value)
return mywidget_value
w_box = interactive(display_value, number=w_number, options=w_options)
display(w_box)
然后你就有了一个Box
,你可以调整它的布局。您还可以使用w_box.kwargs
访问关键字参数,或者使用w_box.result
访问函数的返回值,这是您正在查找的两个小部件的连接字符串...
https://stackoverflow.com/questions/40594372
复制相似问题