I am currently using a Spinbox in my python gui which is implemented by usage of Tkinter. The method to setup the Spinbox would be this one:
def _select_event(self, type, id):
""" Selects an event for editing its properties """
print("_select_event")
if not self._can_draw(): return
if type != self.event_type:
#Destroy all widgets on the frame and build those neccessary
for widget in self.event_edit_widgets: widget.grid_remove()
self.event_edit_widgets = []
self.event_type = type
if self.event_type == "Person": #Build widgets for person
if len(self.map.persons) <= id: return
print("mark0")
self.event_edit_widget_id_spinbox = tkinter.Spinbox(self.event_edit_widget, from_=0, to=len(self.map.persons) - 1, command=self._select_event(self.event_type, self.event_edit_type_dropdown.get()))
print("mark1")
elif self.event_type == "Warp": #Build widgets for warp
if len(self.map.warps) <= id: return
self.event_edit_widget_id_spinbox = tkinter.Spinbox(self.event_edit_widget, from_=0, to=len(self.map.warps) - 1, command=self._select_event(self.event_type, self.event_edit_type_dropdown.get()))
elif self.event_type == "Trigger": #Build widgets for trigger
if len(self.map.triggers) <= id: return
self.event_edit_widget_id_spinbox = tkinter.Spinbox(self.event_edit_widget, from_=0, to=len(self.map.triggers) - 1, command=self._select_event(self.event_type, self.event_edit_type_dropdown.get()))
elif self.event_type == "Sign": #Build widgets for sign
if len(self.map.signposts) <= id: return
self.event_edit_widget_id_spinbox = tkinter.Spinbox(self.event_edit_widget, from_=0, to=len(self.map.signposts) - 1, command=self._select_event(self.event_type, self.event_edit_type_dropdown.get()))
else:
raise Exception("Unkown event type selected: " + self.event_type)
#Push all common widgets to the ui
self.event_edit_widget_id_spinbox.grid(row=1, column=0, sticky=tkinter.NW)
self.event_edit_widget_id_spinbox.config(state="readonly")
self.event_edit_widgets.append(self.event_edit_widget_id_spinbox)
#Update all widgets
self.event_edit_widget_id_spinbox.delete(0, tkinter.END)
self.event_edit_widget_id_spinbox.insert(0, str(id))
As you can see the Spinbox that is setup for e.g. self.event_type == "Person" has the setup method itself associated as command (this means, when you change the value in the spinbox it might setup a new one - nothing to wild). However I found out, that the constructor of the spinbox automatically triggers the command function itself. Meaning the output would be:
"_select_event, mark0, _select_event" (mark0 is not triggered again since the if condition now is not true anymore and thus an endless recursion is preveted luckily). Of course the
self.event_edit_widget_id_spinbox.delete(0, tkinter.END) does not work and raises an Exception in this second call of _select_event, that was triggered by the constructor of the spin box. How can I prevent this constructor from triggering the command at setup?