DirectOptionMenu dynamic updating issue

Here’s the issue:
I have a DirectOptionMenu for selecting a usb webcam. Since those can be plugged in and out, the list on the DirectOptionMenu has to be updated with a task, even if not necessarily every frame and when the mouse cursor is not on the DirectOptionMenu.

The issue is, when the user selects one webcam and the above task to update the list is constantly running, the current item of the DirectOptionMenu which is selected jumps to the first one in the visible list and not necessarily the currently (last) selected one by the user. So the name of the camera displayed by Panda and one currently in use do not match.
If I do DirectOptionMenu.set(index) to set the correct name in the GUI in the task, the function to set the camera stream is also called, and since it is a task, the camera (OpenCV VideoStream) is constantly updated with the same camera, essentially freezing the program. I couldn’t find a way to do set() without the DirectOptionMenu assigned command being called.
Is there a way to set the current item in the list without using set() which also calls the function assigned to the command of DirectOptionMenu?
Sure, a flag variable could be used to make sure the task doesn’t set the camera input more than once but that feels like a bandaid solution and I’m sure DirectOptionMenu has a better way of handling a situation where the list of items are constantly updated without the user wanting the currently selected item to be reset to the first one.


Hmm… I don’t know of a “proper” solution offhand, but another band-aid might be to clear the command-function just before calling “set()”, and then apply it again directly afterwards. That should result in the function not being called in that one case, but still being called otherwise.

I suppose doing

command = menu['command']
menu['command'] = None
menu['command'] = command

is a better bandaid solution, thanks.
Still, this doesn’t feel right. I don’t think a drop down list (DirectOptionMenu) that needs to be updated without resetting the current item is a rare scenario.

I don’t understand why you don’t want to use the correct solution in the form of a flag.

what flag?..

Control the flag variable when you need to change the logic of your function in a loop .

flag = False
if flag:
    Your logic

I think that DirectOptionMenu is designed on the assumption that it is in fact a rare scenario. Compare the fact that styling of the menu-items is lost when the item-list is updated (if I recall correctly; perhaps a fix for that has been added recently), suggesting that the designers didn’t expect that many users would want to change the item-list.

Because it’s not a logic of my own function, it’s a band-aid solution variable that won’t make much sense to a Python coder reading the code who is not very experienced with Panda, and if I need to use a bandaid solution I might as well keep it inside the function itself like Thaumaturge’s solution, it’s just more tidy.

Well, my point is this is not a rare scenario, regardless of the assumption of the original developers.

I don’t know whether it’s rare or not, offhand–I have no real information on that, I think; I was just commenting on the (inferred) design behind the widget.

Sure, we are just discussing. And the reason I’m not marking this as solved is for others to join the discussion.
As far as I’m concerned, the bandaid solution works for me.

Of course! :slight_smile:

Oh, and you could always add a pull-request on GitHub proposing a means of setting the current item without calling the command-function. It does seem like it might be a useful feature to have. Or alternatively, perhaps a way of setting the item-list while preserving the currently-selected item, if that item is also found in the new list.

1 Like