gpt4 book ai didi

python - 如何使 ipywidgets 图像可点击?

转载 作者:太空狗 更新时间:2023-10-30 00:05:28 31 4
gpt4 key购买 nike

我想在 jupyter notebook 中创建一个 Image 实例来响应点击事件 - 我们该怎么做?我还希望能够识别被点击的图像。使用按钮很简单,但使用图像则不然。

最佳答案

在尝试了这个之后,到目前为止我能做到的唯一方法是使用一些 javascript...在 python 代码中,我有类似的东西:

from ipywidgets import Image
from IPython.display import display, Javascript
im = Image(value=open(filename, 'rb').read())
im.add_class('the_image_class')

def on_image_click():
#do something....
return

#Now, I wrote some javascript(jQuery) code like this...
js = ''' $(".the_image_class").on("click", function(e){
var kernel = IPython.notebook.kernel;
kernel.execute("on_image_click()");
});'''

#then, run the javascript...
display(Javascript(js))

这有点遗漏了完全在 python 中使用小部件的要点......有没有更好的方法将 python 函数绑定(bind)到小部件事件,而无需 javascript?

关于python - 如何使 ipywidgets 图像可点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41999020/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com