2025, Sep 27 23:00

Grid settings ignored in ThemedTKinterFrame? Disable auto-resize with cleanresize=False and set column and row weights

TKinterModernThemes grid layout ignoring grid_columnconfigure? ThemedTKinterFrame run overrides grid; fix with cleanresize=False and manual layout control.

When you wire up a UI with TKinterModernThemes and suddenly your grid layout ignores your settings, the culprit is often not the grid itself. The default auto-resize behavior in ThemedTKinterFrame can override manual geometry management, which makes grid_columnconfigure appear broken. Here is a compact walk-through of why that happens and how to take back control without fighting the framework.

Repro: grid_columnconfigure appears to do nothing

The example below uses ThemedTKinterFrame and tries to give the second column the stretch while keeping the first column fixed. The code looks fine at first glance, but the layout doesn’t respond as expected.

import tkinter as tk
import TKinterModernThemes as TKM
class UiPanel(TKM.ThemedTKinterFrame):
    def __init__(self, theme, mode, usecommandlineargs=True, usethemeconfigfile=True):
        super().__init__("Switch", theme, mode, usecommandlineargs=usecommandlineargs, useconfigfile=usethemeconfigfile)
        self.grp_left = self.addLabelFrame("Switch Frame 1", sticky=tk.NSEW, row=0, col=0)
        self.flag_left = tk.BooleanVar()
        self.grp_left.SlideSwitch("Switch1", self.flag_left)
        self.grp_right = self.addLabelFrame("Switch Frame 2", sticky=tk.NSEW, row=0, col=1)
        self.flag_right = tk.BooleanVar()
        self.grp_right.SlideSwitch("Switch2", self.flag_right)
        self.root.grid_columnconfigure(0, weight=0)
        self.root.grid_columnconfigure(1, weight=1)
        self.run()
if __name__ == "__main__":
    UiPanel("park", "dark")

What actually happens

The framework’s run method performs internal resize behavior that can overwrite manual grid configuration. In practice this makes your grid_columnconfigure calls look like they have no effect. Once you disable that internal pass, you are responsible for layout explicitly, not just columns but rows as well.

Fix: disable internal resize and manage grid yourself

Switch the run call to cleanresize=False. After that, also configure the row weights manually so the frames expand vertically, not just horizontally.

import tkinter as tk
import TKinterModernThemes as TKM
class UiPanel(TKM.ThemedTKinterFrame):
    def __init__(self, theme, mode, usecommandlineargs=True, usethemeconfigfile=True):
        super().__init__("Switch", theme, mode, usecommandlineargs=usecommandlineargs, useconfigfile=usethemeconfigfile)
        self.grp_left = self.addLabelFrame("Switch Frame 1", sticky=tk.NSEW, row=0, col=0)
        self.flag_left = tk.BooleanVar()
        self.grp_left.SlideSwitch("Switch1", self.flag_left)
        self.grp_right = self.addLabelFrame("Switch Frame 2", sticky=tk.NSEW, row=0, col=1)
        self.flag_right = tk.BooleanVar()
        self.grp_right.SlideSwitch("Switch2", self.flag_right)
        self.root.grid_columnconfigure(0, weight=0)
        self.root.grid_columnconfigure(1, weight=1)
        self.root.grid_rowconfigure(0, weight=1)
        self.run(cleanresize=False)
if __name__ == "__main__":
    UiPanel("park", "dark")

With cleanresize turned off, your grid settings are respected; and by adding grid_rowconfigure on row 0, the frames expand vertically as intended.

Why this nuance matters

When a UI toolkit manages geometry implicitly, manual layout tweaks can be silently reverted. Understanding that ThemedTKinterFrame can overwrite grid settings during run avoids hours of head-scratching. If you choose to opt out of the internal resize pass, take full ownership of the grid: configure both columns and rows explicitly.

Takeaways

If grid_columnconfigure looks like it’s ignored in ThemedTKinterFrame, disable the internal resize by calling run with cleanresize=False and then configure both grid columns and rows yourself. It’s a small change that restores predictable, explicit control over your layout and keeps your UI behaving the way you define it.

The article is based on a question from StackOverflow by marco and an answer by Aadvik.