# Widgets selection priority bug

**URL:** https://discourse.vtk.org/t/widgets-selection-priority-bug/13642
**Category:** Development
**Tags:** bug, python
**Created:** [March 28, 2024, 8:21am UTC](https://discourse.vtk.org/t/widgets-selection-priority-bug/13642 "2024-03-28T08:21:06Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![cvAIRteam](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/c/e274bd/32.png) [@cvAIRteam](https://discourse.vtk.org/u/cvAIRteam)
#### Post date: [March 28, 2024, 8:21am UTC](https://discourse.vtk.org/t/widgets-selection-priority-bug/13642/1 "2024-03-28T08:21:06Z")

</div>

Hello,

When two (or more) widgets are created and one is _visually behind_ the other, the widget selected is the first widget created.  
So if the widget _behind_ is created first, it will be selected even if visually we select/click on the widget _visually in front_.  
I guess it is due to event/observers priorities.

Here is a code to illustrate this behaviour:

```auto
# First access the VTK module (and any other needed modules) by importing them.
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera
from vtkmodules.vtkInteractionWidgets import vtkSphereWidget
from vtkmodules.vtkRenderingCore import (
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer
)

def main(argv):
    colors = vtkNamedColors()

    ren1 = vtkRenderer()
    ren1.SetBackground(colors.GetColor3d('MidnightBlue'))

    renWin = vtkRenderWindow()
    renWin.AddRenderer(ren1)
    renWin.SetSize(300, 300)
    renWin.SetWindowName('Widgets bug')

    iren = vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    style = vtkInteractorStyleTrackballCamera()
    iren.SetInteractorStyle(style)

    sphereWidgetBehind = vtkSphereWidget()
    sphereWidgetBehind.SetInteractor(iren)
    sphereWidgetBehind.SetCenter(0., 0., -5)
    sphereWidgetBehind.SetRadius(5.)
    sphereWidgetBehind.GetSphereProperty().SetColor(colors.GetColor3d('Green'))
    sphereWidgetBehind.GetSelectedSphereProperty().SetColor(colors.GetColor3d('Green'))
    sphereWidgetBehind.SetRepresentationToSurface()
    sphereWidgetBehind.On()

    sphereWidgetFront = vtkSphereWidget()
    sphereWidgetFront.SetInteractor(iren)
    sphereWidgetFront.SetCenter(0., 0., 0)
    sphereWidgetFront.SetRadius(5.)
    sphereWidgetFront.GetSphereProperty().SetColor(colors.GetColor3d('Red'))
    sphereWidgetFront.GetSelectedSphereProperty().SetColor(colors.GetColor3d('Red'))
    sphereWidgetFront.SetRepresentationToSurface()
    sphereWidgetFront.On()

    iren.Initialize()
    iren.Start()

if __name__ == ' __main__':
    import sys

    main(sys.argv)

```

On the example, if you right click _on_ the red sphere et move the mouse, the green sphere behind is actually selected and translated.

How can I change this behaviour so that the widget selected is always the widget visually in front ?

Config info:

- vtk 9.2.6
- python 3.10.9

Thanks
