Home > database >  Make Tkinter Frame Widget 80% height
Make Tkinter Frame Widget 80% height

Time:12-06

console_frame = LabelFrame(root)
console_frame.pack(side="top", fill="x")

I have this frame here, which is called console_frame, and inside is a single text box. I would like to make this frame 80% of the height of the screen, and another frame 20% of the height of the screen, but I would also like this to work dynamically, aka the widget continues to resize with the resizing of the window.

I tried just setting the height of the frame to 20% of the screen height, but it is not dynamic, as the widget just stays the same height after resizing.

CodePudding user response:

When you use the grid placement method, you can set the share to which each row expands or contracts when you resize the window. This might be what you are looking for:

from tkinter import *
    
root = Tk()
root.geometry("800x500")
root.rowconfigure(0, weight=1) ## Weighs 1
root.rowconfigure(1, weight=4) ## Weights 4 time as much as the first row, thereby filling 4/(1 4)=80%

top_frame = Frame(root, bg="red",width=800)
top_frame.grid(row=0, column=0, sticky="NSEW")

bottom_frame = Frame(root, bg="blue",width=800)
bottom_frame.grid(row=1, column=0, sticky="NSEW")

root.mainloop()
  • Related