Potentially unintended behavior when integrating with tkinter

I would not catalogue this as a bug, but more as “unintended behavior”. First, let me put you into context:

Let’s say, I want to create a matplotlib chart, embedded in a tkinter window. Therefore, I will be using tools like FigureCanvasTkAgg and NavigationToolbar2Tk.

Then, I want to edit the NavigationToolbar2Tk. I will remove buttons this way:


Then, I will create some Buttons and Checkbuttons, and add them by packing. I will also use the method self.toolbar._Spacer() to add some Spacers.

All of these WORKS fine. The problem is: let’s now do the following. All the chart is being created in a custom class object (that’s why you see me using self). Now, I will delete the class, and start the process again. When running line self.toolbar.children['!checkbutton1'].pack_forget(), I will get an error. That one is the only pack_forget() triggering it. The reason is, the new toolbar has the same children as before when initialised, except for checkbuttons - their numbers will change, but why?

Consider that “delete the class” does not involve stopping the code. I have a Tk() window with a button, and matplotlib plots in a Toplevel() window. I close the Toplevel window, and press a button in my Tk() to create the new Toplevel and custom class with my matplotlib stuff.

I’ll give you a photo of two prints. They are printing all keys of self.toolbar.children, for the first try (no errors) and the second (triggers error)