Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tkinter pack with anchor/side not filling all available space

import tkinter as tk
from tkinter import ttk

class App(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.create_widgets()

    def create_widgets(self):
        self.notebook = ttk.Notebook(self.master)
        self.notebook.pack(side="top", anchor="n", expand=True)

        # create tabs
        self.tabs = {}
        self.tabs["tab1"] = tk.Frame(self.notebook, width=420, height=420, borderwidth=0)
        self.tabs["tab2"] = tk.Frame(self.notebook, width=420, height=420, borderwidth=0)
        self.tab_headers = ["tab1", "tab2"]

        self.tabs["tab1"].pack()
        self.tabs["tab2"].pack()

        # add frame with label below notebook
        self.bottom_frame = tk.Frame(self.master, bg="#ffff00")
        self.bottom_frame.pack(side="top", anchor="n", fill="x")

        self.bottom_label = tk.Label(self.bottom_frame, text="This is a label below the notebook", font=("TkDefaultFont", 14), bg="#ffff00")
        self.bottom_label.pack(side="top", pady=10)

        # add tabs to notebook
        self.notebook.add(self.tabs["tab1"], text='Tab 1')
        self.notebook.add(self.tabs["tab2"], text='Tab 2')

root = tk.Tk()
root.geometry("420x840")
root.resizable(False, False)

app = App(master=root)
app.mainloop()

How do you get the frame/label below the notebook to be packed directly below the notebook? Even after trying side=top and anchor=n, it still aligns to the bottom of the window, even when there is clearly enough room for it to be aligned further up.

enter image description here

Even after trying side=top and anchor=n, it still aligns to the bottom of the window, even when there is clearly enough room for it to be aligned further up.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It is because you have used expand=True in self.notebook.pack(...), so the notebook will use all the available space and the bottom frame will be pushed to the bottom of the window.

If you just use self.notebook.pack(), then the bottom frame will be just below the notebook:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading