Jupyter,AttributeError:类型对象“Widget”没有属性“observe”

问题描述 投票:0回答:2

我尝试对我的切换按钮使用观察

wtarget = widgets.ToggleButtons(
    description='select target',
    options=['A', 'B', 'C', 'D', 'E', 'F'])
wtarget.observe(target_on_value_change, names='value')  

显示此错误:

AttributeError: 'ToggleButtons' object has no attribute 'observe'

我的另一台 MacBook 没有问题,但这台却出现了问题。 我使用的是 MacBook,10.12。 Python 版本 4.0.0。 ipywidgets 是通过 pip 安装的。

谢谢。

python ipython jupyter
2个回答
2
投票

这表明您拥有旧版本的 Traitlet。

.observe
已添加到 Traitlets 4.1:

pip install --upgrade traitlets

您可能想要升级更多:

pip install --upgrade ipywidgets

0
投票

如果您从一个小部件继承子类但忘记调用 super,也可能会出现此错误。那就是

from ipywidgets import VBox, Text

class MyClass(VBox):
    def __init__(self):
        self.children = [Text(description="hello")]

my_class = MyClass()
my_class

退货

# ...
# AttributeError: 'MyClass' object has no attribute '_model_id'

解决办法是在

__init__
中调用super:

class MyClass(VBox):
    def __init__(self):
        super(MyClass, self).__init__()
        self.children = [Text(description="hello")]

© www.soinside.com 2019 - 2024. All rights reserved.