Search code examples
pythonuser-interfacetkinter

Tkinter grid cells displaying on top of each other


I am new to tkinter and I'm trying to make a few windows using the grid() method.

However, I'm always struggling to understand which cell will show above the other when resizing the window to a very small one. Sometimes the last-added one will be on top, sometimes not (my last example is a separator going on top of a frame containing labels even though I declared the separator first).

Could anyone explain to me how these 'levels' work? and how I can set which one should 'disappear' behind the other in case the user decides to resize the window to make it small?

Thanks in advance!


Solution

    • In Tkinter, stacking order of widgets is determined by the order in which they are added to their parent widget.
    • When widgets are added to a grid layout using the grid() method, the stacking order is still determined by the order in which they are added, but it's also influenced by the row and column indices specified in the grid layout.
    • However, when resizing the window to a very small size, widgets may overlap or appear on top of each other depending on their geometry management and resizing properties.
    • To control the stacking order, you can use the lift() and lower() methods of the widget. These methods allow you to raise a widget to the top of the stacking order (lift()) or lower it to the bottom (lower()).