pythontkinterpython-3.11

Frame that resizes by adding any widget inside


I have a behavior that is happening, that I don't know how to find a solution to. The solution is probably right before my eyes, but I can't find it, despite my searches.

The problem is this, I have a frame with a certain size, in which I created a grid inside. Until that, there are no problems.

enter image description here

The problem happens the moment I put something inside the grid, whatever it is (in this case, two buttons)

enter image description here

The code is this:

import tkinter

from engine import th

class Eframe(tkinter.Frame):
    def __init__(self, master: any, height, width, **kwargs):
        super().__init__(master=master, **kwargs)
        self.height = height
        self.width = width

        self.EframeGen()

    def EframeGen(self):
        theme = th.FrameTheme
        self.configure(background=theme["bg"][0])
        self.configure(highlightbackground="gray28")
        self.configure(highlightthickness=3)
        self.configure(height=self.height)
        self.configure(width=self.width)


class file():

    def __init__(self, engine):
        self.engine = engine
        self.engine.updateClass.append(self)
        self.widget = []

    def frameFile(self):
        self.engine.buttonLoad.configure(state="disabled")
        self.height = self.engine.FrameOperation.winfo_height()
        self.width = self.engine.FrameOperation.winfo_width()

        self.MainFrame = Eframe(self.engine.FrameOperation, self.height, self.width)
        self.widget.append(self.MainFrame)
        self.MainFrame.pack(pady=(20, 10), padx=30)

        self.MainFrame.columnconfigure(0, weight=1)
        self.MainFrame.columnconfigure(1, weight=3)

        frame1 = tkinter.Button(self.MainFrame, background="white")
        frame1.grid(column=0, row=0, padx=5, pady=5)

        frame2 = tkinter.Button(self.MainFrame, background="red")
        frame2.grid(column=1, row=0, padx=5, pady=5)

    def Update(self):
        if len(self.widget) >= 1:
            for wid in self.widget:
                wid.update()

So, in itself, the posted code is "reproducible", if modified. To explain better:

engine = This is the parent class, where the main code of the program is.

self.engine.updateClass = It refers to a list that the parent class references to update all classes after every certain amount of time. To be clear, it calls the Update function of each class contained in the list.

self.engine.buttonLoad = This is the button that calls the frameFile function of this class, only to be disabled. Not necessary to reproduce the code.

self.engine.FrameOperation = It refers to the main frame in which the MainFrame is contained, which I'm having problems with. In theory, you can replace it with root.

enter image description here

Can you help me with this problem? Which I'm sure has a simple solution, which I don't see though. If the code isn't right, I'll try to put it better so I can reproduce it.

Final note, I've modified the code several times, reading different systems to create the grid correctly, but it still comes out this way, so I definitely don't understand how it works yet.


Solution

  • @acw1668 observes that:

    It is expected behavior. If you don't want the frame to be resized when widgets are added into it using grid(), then add self.grid_propagate(0) inside Eframe.__init__().