容器作为工具提示不显示内容

编程入门 行业动态 更新时间:2024-10-25 08:27:47
本文介绍了容器作为工具提示不显示内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在创建散点图,并希望在工具提示中显示一些信息.

I'm creating a scatterplot and want to show some information in a tooltip.

以下内容可以正常工作:

The following works perfectly:

import bqplot as bqp import ipywidgets as ipw xSc = bqp.LinearScale() ySc = bqp.LinearScale() tt = ipw.Label("A") def hover_handler(self, content): tt.value = str(content) s = bqp.Scatter(x=[0, 1, 2], y=[1, 2, 3], scales=dict(x=xSc, y=ySc), tooltip=tt) s.on_hover(hover_handler) bqp.Figure(marks=[s])

(没有轴,可以使代码简短)

(there's no axes and whatnot to keep the code short)

将鼠标悬停在每个点上可以显示其content很好.

Hovering over each point shows its content just fine.

但是,我不想简单地显示原始内容.相反,我想以表格形式显示它(但是默认的bqp.Tooltip不足以满足我的需要).

However, I don't want to simply show the raw content. Instead, I want to show it in tabular form (but the default bqp.Tooltip isn't sufficient for my needs).

但是,如果我将标签包裹在ipw.VBox中,则工具提示会变成一个很小的垂直条.添加min_width和min_height会增加工具提示的大小,但是没有内容(即使tt是使用默认值创建的).如果我单独调用以单独显示VBox,则该版本会正常显示(即使未定义布局),甚至在将鼠标移到这些点上时也会更新.

However, if I wrap the label in a ipw.VBox, the tooltip becomes a tiny vertical sliver. Adding a min_width and min_height increases the size of the tooltip, but there's no content (even though tt was created with a default value). If I make a separate call to display the VBox alone, that version appears normally (even without defining the layout) and even updates when moving the mouse over the points.

import bqplot as bqp import ipywidgets as ipw from IPython.display import display xSc = bqp.LinearScale() ySc = bqp.LinearScale() tt = ipw.Label("A") vb = ipw.VBox(children=[tt], layout=ipw.Layout(min_width='100px', min_height='100px')) display(vb) def hover_handler(self, content): tt.value = str(content) s = bqp.Scatter(x=[0, 1, 2], y=[1, 2, 3], scales=dict(x=xSc, y=ySc), tooltip=vb) s.on_hover(hover_handler) bqp.Figure(marks=[s])

我需要怎么做才能正确显示工具提示?

What do I need to do for the tooltip to appear correctly?

推荐答案

我认为,对于您尝试实现的目标,最好使用Output小部件作为工具提示,然后创建所需的小部件并显示他们将输出小部件用作上下文管理器.

I think for what you are trying you achieve you would be better served with an Output widget as a tooltip, and then create the widgets you need and display them using the output widget as a context manager.

如果要查看可以显示的其他信息,请尝试在hover_handler函数中打印content.

If you want to see what additional information could be presented, try printing content within your hover_handler function.

import bqplot as bqp import ipywidgets as ipw from IPython.display import display, clear_output xSc = bqp.LinearScale() ySc = bqp.LinearScale() out = ipw.Output() def hover_handler(self, content): out.clear_output() with out: label = ipw.Label(content['data']['name']) display(label) s = bqp.Scatter(x=[0, 1, 2], y=[1, 2, 3], scales=dict(x=xSc, y=ySc), tooltip=out) s.on_hover(hover_handler) bqp.Figure(marks=[s])

要显示信息表,您可能需要一个ipy.HTML小部件,可用于传递HTML表.有时,我会通过df.to_html()方法使用熊猫.

To display a table of information, you probably need an ipy.HTML widget which you can use to pass a HTML table. I sometimes use pandas for this, with the df.to_html() method.

更多推荐

容器作为工具提示不显示内容

本文发布于:2023-11-22 23:23:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619320.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   提示   工具   内容

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!