1

This is what I am trying to achieve

enter image description here

Here is the code:

class GeometryFrame(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.columnconfigure(0, weight=1)

        rectangle_1 = tk.Label(self, text="Rectangle 1", bg="green", fg="white")
        rectangle_1.grid(column=0, row=0, ipadx=10, ipady=10, sticky="EW")

        rectangle_2 = tk.Label(self, text="Rectangle 2", bg="red", fg="white")
        rectangle_2.grid(column=0, row=1, ipadx=10, ipady=10, sticky="EW")

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("600x100")
        self.columnconfigure(0, weight=1)

        self.geo_frame = GeometryFrame(self)
        self.geo_frame.grid(column=0, row=0)

if __name__ == "__main__":
    app = App()
    app.mainloop()

But I am getting this enter image description here

How to fix this? (NOTE: I want to use grid and not pack).

I get the desired result if I remove the GeometryFrame class and place it's code to the App class.

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("600x100")
        self.columnconfigure(0, weight=1)
        # self.rowconfigure(0, weight=1)
        rectangle_1 = tk.Label(self, text="Rectangle 1", bg="green", fg="white")
        rectangle_1.grid(column=0, row=0, ipadx=10, ipady=10, sticky="EW")

        rectangle_2 = tk.Label(self, text="Rectangle 2", bg="red", fg="white")
        rectangle_2.grid(column=0, row=1, ipadx=10, ipady=10, sticky="EW")
haccks
  • 104,019
  • 25
  • 176
  • 264

1 Answers1

3

You need to modify the GeometryFrame class to include the columnconfigure method for the desired column.

import tkinter as tk

class GeometryFrame(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.columnconfigure(0, weight=1)

        rectangle_1 = tk.Label(self, text="Rectangle 1", bg="green", fg="white")
        rectangle_1.grid(column=0, row=0, ipadx=10, ipady=10, sticky="EW")

        rectangle_2 = tk.Label(self, text="Rectangle 2", bg="red", fg="white")
        rectangle_2.grid(column=0, row=1, ipadx=10, ipady=10, sticky="EW")

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("600x100")
        self.columnconfigure(0, weight=1)

        self.geo_frame = GeometryFrame(self)
        self.geo_frame.grid(column=0, row=0, sticky="NSEW")  # Add sticky="NSEW" to fill the available space

if __name__ == "__main__":
    app = App()
    app.mainloop()

This is end result: result


The self.columnconfigure(0, weight=1) line is included in the GeometryFrame class to assign weight to the column, enabling it to expand and occupy the available space. In the App class, the grid method call for self.geo_frame is modified with the sticky="NSEW" parameter to ensure that the GeometryFrame widget completely fills the available space within the main window.

By implementing these changes, the GeometryFrame expands to occupy the entire width of the main window, while the Rectangle 1 and Rectangle 2 labels within their respective rows also expand accordingly.

Marko
  • 103
  • 5