Home > Net >  Should I use _setit even if I get a warning? (tkinter)
Should I use _setit even if I get a warning? (tkinter)

Time:08-27

When using _setit I get the following warning in PyCharm: enter image description here

Why do I use _setit?

I want to add or remove dynamically options to a dropdown list. To do so, I extract the tk.Menu object from the tk.OptionMenu I defined. And I add_command to this tk.Menu object. Problem is, when I click on the dropdown list, the element is not selected.

Hence, I use the protected member _setit like so: MY_MENU.add_command(label=LABEL, command=tk._setit(MY_STRING_VAR, LABEL)). MY_STRING_VAR is obviously the tk.StringVar associated to the tk.OptionMenu object defined earlier.

This works perfectly but I'm not allowed to use any # noqa in my project and I'm not allowed any weak warnings either. So two questions:

  • Is it actually a problem to use a "protected member of a module"? whatever that means?
  • Is there any workaround? Am I doing it the wrong way?

CodePudding user response:

As a general rule, no, you should not be calling the private functions of objects. Being private, it could change on any future release of tkinter. In reality it probably won't, but it's still a bad practice.

The simple solution is to create your own implementation of _setit.

  • Related