如何避免Glyph被TapTool选中修改?Bokeh多Glyph方案最优性问询
Great question! Let's unpack this and clarify the best approaches for your use case.
First off: your method of setting nonselection_glyph=None is absolutely valid and effective for making a glyph appear unselectable (i.e., not show any visual style changes when clicked). Here's why it works:
By default, Bokeh swaps in a nonselection_glyph style for all items not in the current selection. When you set this property to None, there's no alternative glyph to render—so the original glyph stays identical regardless of selection state. This gives users clear visual feedback that this element isn't meant to be interacted with via selection.
That said, if you want to go a step further and completely disable selection functionality for that glyph (meaning clicking it won't even modify the data source's selected property), you have another option: set the GlyphRenderer's selection_policy to None. Here's how to add that to your code:
pglyph.selection_policy = None
This prevents the renderer from processing any selection events at all, which might be more aligned with your intent if you want to block selection entirely, not just hide the visual change.
Quick comparison of the two approaches:
nonselection_glyph=None: Hides the visual selection feedback, but clicking the glyph will still update the data source'sselectedstate (if no other restrictions are in place). Useful if you want to keep underlying selection logic but don't want users to see it.selection_policy=None: Fully disables selection for the glyph—clicking it won't trigger any selection state changes. Ideal if you want to make sure users can't select that element at all.
In your specific scenario (only wanting circles to trigger actions on click), your current setup works well: you've added the callback only to the circles' data source, and set the patches' nonselection_glyph to None to avoid confusing visual feedback. If you want to make extra sure patches can't be selected at all, adding the selection_policy=None line would be a robust addition.
内容的提问来源于stack exchange,提问作者Karel Marik

