1

I have a notebook that I placed in grid (0;0). I want to the notebok to fill the entire screen even if its content (frames) would not fill the screen.

screenshot

class App(Tk):
    def __init__(self) -> None:
        super().__init__()

        # Widgets
        self.notebook = Notebook(self) #  <-- Widget I want to fill the window
        self.notebook.grid(row=0, column=0)

        self.control_frame = ControlFrame(self)
        self.notebook.add(self.control_frame, text="Control")

class ControlFrame(Frame):
    def __init__(self, master):
        super().__init__(master)

        self.control_bar = ControlBar(self)
        self.control_bar.grid(row=0, column=0)

        self.connect_btn = Button(self, text="Connect")
        self.connect_btn.grid(row=1, column=0)

        self.log = Text(self, width=100)
        self.log.grid(row=2, column=0)
Psionman
  • 3,084
  • 1
  • 32
  • 65

1 Answers1

0

I think you need to use the sticky argument in you grid:

The width of a column (and height of a row) depends on all the widgets contained in it. That means some widgets could be smaller than the cells they are placed in. If so, where exactly should they be put within their cells?

By default, if a cell is larger than the widget contained in it, the widget will be centered within it, both horizontally and vertically. The master's background color will display in the empty space around the widget. In the figure below, the widget in the top right is smaller than the cell allocated to it. The (white) background of the master fills the rest of the cell.

The sticky option can change this default behavior. Its value is a string of 0 or more of the compass directions nsew, specifying which edges of the cell the widget should be "stuck" to. For example, a value of n (north) will jam the widget up against the top side, with any extra vertical space on the bottom; the widget will still be centered horizontally. A value of nw (north-west) means the widget will be stuck to the top left corner, with extra space on the bottom and right.

So your code should look something like

self.notebook.grid(row=0, column=0, sticky='NSWE')

If you need more information check out this article

EDIT:

Thanks to @acw1668 for the hint!

I think the behaviour of the ttk.Notebook class is strange with the grid layout. I manage to "solve" it using pack in the App() class.

Here is the code that worked for me:

class App(Tk):
    def __init__(self) -> None:
        super().__init__()

        self.notebook = Notebook(self)  # <-- Widget I want to fill the window

        self.control_frame = ControlFrame(self.notebook)
        self.notebook.add(self.control_frame, text="Control", sticky="NSWE")
        self.notebook.pack(expand=True, fill=BOTH)


class ControlFrame(Frame):
    def __init__(self, master):
        super().__init__(master)

        self.control_bar = ControlBar(self)
        self.control_bar.grid(row=0, column=0)
        self.grid(column=0, row=0, sticky="SNWE")

        self.connect_btn = Button(self, text="Connect")
        self.connect_btn.grid(row=1, column=0, sticky=N)

        self.log = Text(self)
        self.log.grid(row=2, column=0, sticky="NSWE")
        
        self.grid_rowconfigure(0, weight=0)
        self.grid_rowconfigure(1, weight=0)
        self.grid_rowconfigure(2, weight=1)
        self.grid_columnconfigure(0, weight=1)

Hope this solves your problem

DonPre
  • 158
  • 7
  • Yeah I've tried exactly that and that doesn't change anything. – Latex User 101 Oct 24 '22 at 09:44
  • If I add that to something inside the notebok it works. But it's not really the intetion here, I just want to notebook itself to fill the window. – Latex User 101 Oct 24 '22 at 09:51
  • 3
    You need to set `weight=1` for row 0 column 0 using `.rowconfigure()` and `.columnconfigure()` on the parent of the notebook. – acw1668 Oct 24 '22 at 10:19
  • When sorting out layout problems with multiple frames/toplevels, it helps to set their backgrounds to garish colours so you can easily see which ones are not behaving as expected. – Donal Fellows Oct 27 '22 at 07:43