简体   繁体   中英

Frame of fixed size staying at the bottom of the window

I am using Tk GUI and I want my window to be split into 2 frames, something like that:

import tkinter as tk

root = tk.Tk()
root.geometry('1000x800')

df_frame = tk.LabelFrame(root)

df_frame.place(relwidth = 1, height = 720)

open_file_frame = tk.LabelFrame(root)
open_file_frame.place(x = 0, y = 720, relwidth = 1, height = 80)

root.mainloop()

My problem is that I don't know how to make it adaptable to the size of the window. If the user enlarges the size of the window, I want the second frame to stay at the bottom, and the first frame to enlarge accordingly. Thanks in advance for your help.

Try using grid manager for control over resizing and use rowconfigure and columnconfigure to control how objects change size.

Also LabelFrames need to contain some object, in this code I've used Buttons .

import tkinter as tk

def flexx( o, r = 0, c = 0, rw = 1, cw = 1):
    o.rowconfigure(r, weight = rw)
    o.columnconfigure(c, weight = cw)

root = tk.Tk()
root.geometry('1000x800')

# make contents of root resizable
flexx(root)

df_frame = tk.LabelFrame(root)
df_frame.grid(sticky = tk.NSEW)

# You need some object in labelframe
tk.Button(df_frame, text = "Any object to occupy labelframe").grid(sticky = tk.NSEW)

# make contents of df_frame resizable
flexx(df_frame)

open_file_frame = tk.LabelFrame(root)
open_file_frame.grid(sticky = tk.NSEW)

# You need some object in labelframe
tk.Button(open_file_frame, text = "Any other object").grid(sticky = tk.NSEW)

# make contents of open_file_frame resizable
flexx(open_file_frame)

root.mainloop()

You can combine relheight and height options to control the height of the top frame.

And combine rely and y options to put the bottom frame at the bottom of the window:

import tkinter as tk

root = tk.Tk()
root.geometry('1000x800')

df_frame = tk.LabelFrame(root)
# frame_height = window_height - 80
df_frame.place(relwidth=1, relheight=1, height=-80)

open_file_frame = tk.LabelFrame(root)
# frame y position = 80 pixel from the bottom
open_file_frame.place(rely=1, y=-80, relwidth=1, height=80)

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM