python 3.4.3 ipywidgets 交互导致 KeyError 'kernel' 和 traitlets.traitlets.TraitError

python 3.4.3 ipywidgets interact results in KeyError 'kernel' and traitlets.traitlets.TraitError

当我 运行 下面的代码时,我总是会遇到两个错误。

import cv2
from ipywidgets import interact

def thrImage(thr=254):
    _,imgOut = cv2.threshold(img,thr,255,cv2.THRESH_BINARY)
    cv2.imshow("tresholdImage",imgOut)

img = cv2.imread("../../bspBilder/001.jpg")
hist = cv2.calcHist([img],[0],None,[256],[0,256])

cv2.imshow("image",img)
_ = cv2.waitKey(0)
# up until here, everything works as expected

interact(thrImage,thr=(0,255,1))
_ = cv2.waitKey(0)

cv2.destroyAllWindows()

如评论中所述,正常图像关闭后,我得到以下回溯:

Traceback (most recent call last):
  File "F:\OneDrive\Software\Python\lib\site-packages\traitlets\traitlets.py", line 432, in __get__
    value = obj._trait_values[self.name]
KeyError: 'kernel'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:\OneDrive\!!Workspaces!!\Python\!!Projects!!\Learning\Bildverarbeitung\Schwellwert\schwellwert.py", line 68, in <module>
    interact(thrImage,thr=(0,255,1))
  File "F:\OneDrive\Software\Python\lib\site-packages\ipywidgets\widgets\interaction.py", line 318, in interact
    w = interactive(f, **kwargs)
  File "F:\OneDrive\Software\Python\lib\site-packages\ipywidgets\widgets\interaction.py", line 184, in interactive
    container = Box(_dom_classes=['widget-interact'])
  File "F:\OneDrive\Software\Python\lib\site-packages\ipywidgets\widgets\widget_box.py", line 41, in __init__
    super(Box, self).__init__(**kwargs)
  File "F:\OneDrive\Software\Python\lib\site-packages\ipywidgets\widgets\widget.py", line 505, in __init__
    super(DOMWidget, self).__init__(*pargs, **kwargs)
  File "F:\OneDrive\Software\Python\lib\site-packages\ipywidgets\widgets\widget.py", line 175, in __init__
    self.open()
  File "F:\OneDrive\Software\Python\lib\site-packages\ipywidgets\widgets\widget.py", line 192, in open
    self.comm = Comm(**args)
  File "F:\OneDrive\Software\Python\lib\site-packages\ipykernel\comm\comm.py", line 63, in __init__
    self.open(data)
  File "F:\OneDrive\Software\Python\lib\site-packages\ipykernel\comm\comm.py", line 94, in open
    comm_manager = getattr(self.kernel, 'comm_manager', None)
  File "F:\OneDrive\Software\Python\lib\site-packages\traitlets\traitlets.py", line 439, in __get__
    value = self._validate(obj, dynamic_default())
  File "F:\OneDrive\Software\Python\lib\site-packages\traitlets\traitlets.py", line 471, in _validate
    value = self.validate(obj, value)
  File "F:\OneDrive\Software\Python\lib\site-packages\traitlets\traitlets.py", line 1045, in validate
    self.error(obj, value)
  File "F:\OneDrive\Software\Python\lib\site-packages\traitlets\traitlets.py", line 899, in error
    raise TraitError(e)
traitlets.traitlets.TraitError: The 'kernel' trait of a Comm instance must be a Kernel, but a value of class 'NoneType' (i.e. None) was specified.

由于我是 python 菜鸟,不理解这些行的含义,所以我在搜索了这两个错误后进行了搜索。我偶然发现了更新相应模块的建议,所以我做了,但这也没有帮助。

C:\Users\Phi>python --version
Python 3.4.3

C:\Users\Phi>pip install --upgrade --ignore-installed ipython
Collecting ipython
  Downloading ipython-4.0.1-py3-none-any.whl (730kB)
    100% |################################| 733kB 39kB/s
Collecting decorator (from ipython)
  Using cached decorator-4.0.4-py2.py3-none-any.whl
Collecting pickleshare (from ipython)
Collecting simplegeneric>0.8 (from ipython)
Collecting traitlets (from ipython)
  Using cached traitlets-4.0.0-py2.py3-none-any.whl
Collecting path.py (from pickleshare->ipython)
  Using cached path.py-8.1.2-py2.py3-none-any.whl
Collecting ipython-genutils (from traitlets->ipython)
  Using cached ipython_genutils-0.1.0-py2.py3-none-any.whl
Installing collected packages: decorator, path.py, pickleshare, simplegeneric, ipython-genutils, traitlets, ipython
Successfully installed decorator-4.0.4 ipython-4.0.0 ipython-genutils-0.1.0 path.py-8.1.2 pickleshare-0.5 simplegeneric-0.8.1 traitlets-4.0.0

我仍然得到上面的两个错误。

如果有人has/gets遇到同样的问题并搜索答案,这是我的解决方案:

模块没有问题,但交互栏缺少 window。 对我来说,解决这个问题最简单的方法就是在 jupyter notebook 中简单地执行代码。其中交互栏是根据需要自动生成的。