pythontkintertkinter-layout

Tkinter Frame won't fill remaining space


I am just trying to make the green frame fill the remaining empty space of a window, but I can only get it to fill the bottom half no matter what I do.

Screenshot

Here is my code:

from tkinter import *

class CredsWindow:

    def __init__(self, root):

        root.title("Greenbox")
        root.geometry("300x200")
        root.attributes('-topmost', True)

        self.drag_offsetx = IntVar()
        self.drag_offsety = IntVar()

        def drag_window_start(v):
            self.drag_offsetx = v.x
            self.drag_offsety = v.y

        def drag_window(v):
            x = root.winfo_pointerx()-self.drag_offsetx
            y = root.winfo_pointery()-self.drag_offsety
            root.geometry(f'+{x}+{y}')

        title_bar = Frame(root, bg="#252525")
        title_bar.pack(anchor=N, expand=True, fill=X)
        title_bar.bind("<Button-1>", drag_window_start)
        title_bar.bind("<B1-Motion>", drag_window)

        title_label = Label(title_bar, text="Greenbox", bg="#252525", fg="white")
        title_label.pack(side=LEFT, pady=3, padx=2)

        close_button = Button(title_bar, text="X", bg="#252525", fg="white", relief="flat", command=root.quit)
        close_button.pack(side=RIGHT, pady=3, padx=2)

        
        mainframe = Frame(root, bg='#171717')
        mainframe.pack(side=TOP, expand=True, fill=BOTH)


root = Tk()
CredsWindow(root)
root.mainloop()

Solution

  • You need to remove expand=True in this statement:

    title_bar.pack(anchor=N, expand=True, fill=X)
    

    When you set it to True, you're telling pack to have it allocate extra space to that widget. You've also added expand=True when packing mainframe, which means that all unused space will be evenly distributed between title_bar and mainframe.