简体   繁体   English

固定大小的框架停留在窗口底部

[英]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:我正在使用 Tk GUI,我希望我的窗口被分成 2 帧,类似这样:

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.尝试使用网格管理器来控制调整大小并使用 rowconfigure 和 columnconfigure 来控制对象如何更改大小。

Also LabelFrames need to contain some object, in this code I've used Buttons .另外LabelFrames需要包含一些对象,在这段代码中我使用了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.您可以结合relheightheight选项来控制顶部框架的高度。

And combine rely and y options to put the bottom frame at the bottom of the window:并结合relyy选项将底部框架放在窗口底部:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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