Skip to content Skip to sidebar Skip to footer

Tkinter Frame Container Color Not Visible

Why can't I see a red frame with the following code? import Tkinter root = Tkinter.Tk() root.geometry('220x300') container_frame = Tkinter.Frame(background = 'red', width = 100,

Solution 1:

You don't see it because you have no padding between the button and the frame. By default, containers "shrink to fit" around their contents. Even if you add an explicit width or height to the frame, it will shrink to exactly fit its children.

There are several ways to achieve the effect you're looking for, but it's not clear exactly what effect you want. You can turn off this "shrink-to-fit" behavior (using container_frame.pack_propagate(False)). Or, you can add padding around the widget. Or, you can apply the background to the container of the frame. Or you could pack the frame to fill its container (the main window), then make sure the containing window is large enough to expose the frame.

For an example of that last suggestion, you can change one line to be this:

container_frame.pack(side="top", fill="both", expand=True)

Solution 2:

If you change to:

widget_button.pack(padx=10, pady=10)

You can see that the frame has been resized when call widget_button.pack(...)

enter image description here

Post a Comment for "Tkinter Frame Container Color Not Visible"