如何防止 tkinter 框架在一组框架中调整大小
How to prevent a tkinter frame from resizing in a set of frames
对于模拟应用程序,我正在 python 中使用 Tkinter 模块设计一个 UI。我目前正在尝试向框架和 window 大小添加约束,以保持良好的界面,无论 window.
的大小如何
在我的 UI 的一部分中,我有这样的东西:
from tkinter import *
root = Tk()
topframe= Frame(master=root,bg='red')
midframe= Frame(master=root,bg='blue')
bottomframe= Frame(master=root,bg='yellow')
toplabel= Label(master=topframe,bg='red',text='Must be non resizable unless window cannot fit it \n (Contains buttons)',height=10)
midlabel= Label(master=midframe,bg='blue',text='Must be resizable \n (Contains a graph)',height=10)
bottomlabel= Label(master=bottomframe,bg='yellow',text='Must be non resizable unless window cannot fit it \n (Contains simulation results)',height=10)
toplabel.pack(fill=X,expand=TRUE)
midlabel.pack(fill=X,expand=TRUE)
bottomlabel.pack(fill=X,expand=TRUE)
topframe.pack(side=TOP,fill=BOTH,expand=FALSE)
midframe.pack(side=TOP,fill=BOTH,expand=TRUE)
bottomframe.pack(side=TOP,fill=BOTH,expand=FALSE)
root.mainloop()
所以我得到 window 这样的:
The window I get
但我的问题是,当我调整 window 的大小以变小时,黄色部分缩小直到消失,但我想保持尺寸固定,蓝色部分缩小(中间的框架).有人有想法吗?
我已经尝试过 grid_propagate(False) 并且我已经看过相关问题,但是它要么没有效果,要么不适合我的示例。感谢您的帮助
使用 grid()
而不是 pack()
这可以如下所示完成:
from tkinter import *
root = Tk()
topframe = Frame(root, bg='red')
midframe = Frame(root, bg='blue')
bottomframe = Frame(root, bg='yellow')
root.rowconfigure([0,2], minsize=90) # Set min size for top and bottom
root.rowconfigure(1, weight=1) # Row 1 should adjust to window size
root.columnconfigure(0, weight=1) # Column 0 should adjust to window size
topframe.grid(row=0, column=0, sticky='nsew') # sticky='nsew' => let frame
midframe.grid(row=1, column=0, sticky='nsew') # fill available space
bottomframe.grid(row=2, column=0, sticky='nsew')
toplabel = Label(topframe, bg='red', text='Must be non resizable unless window cannot fit it \n (Contains buttons)',height=10)
midlabel = Label(midframe, bg='blue', text='Must be resizable \n (Contains a graph)',height=10)
bottomlabel = Label(bottomframe, bg='yellow', text='Must be non resizable unless window cannot fit it \n (Contains simulation results)',height=10)
toplabel.pack(fill=X,expand=TRUE)
midlabel.pack(fill=X,expand=TRUE)
bottomlabel.pack(fill=X,expand=TRUE)
root.mainloop()
您可能还想设置 window 最小尺寸。
对于模拟应用程序,我正在 python 中使用 Tkinter 模块设计一个 UI。我目前正在尝试向框架和 window 大小添加约束,以保持良好的界面,无论 window.
的大小如何在我的 UI 的一部分中,我有这样的东西:
from tkinter import *
root = Tk()
topframe= Frame(master=root,bg='red')
midframe= Frame(master=root,bg='blue')
bottomframe= Frame(master=root,bg='yellow')
toplabel= Label(master=topframe,bg='red',text='Must be non resizable unless window cannot fit it \n (Contains buttons)',height=10)
midlabel= Label(master=midframe,bg='blue',text='Must be resizable \n (Contains a graph)',height=10)
bottomlabel= Label(master=bottomframe,bg='yellow',text='Must be non resizable unless window cannot fit it \n (Contains simulation results)',height=10)
toplabel.pack(fill=X,expand=TRUE)
midlabel.pack(fill=X,expand=TRUE)
bottomlabel.pack(fill=X,expand=TRUE)
topframe.pack(side=TOP,fill=BOTH,expand=FALSE)
midframe.pack(side=TOP,fill=BOTH,expand=TRUE)
bottomframe.pack(side=TOP,fill=BOTH,expand=FALSE)
root.mainloop()
所以我得到 window 这样的:
The window I get
但我的问题是,当我调整 window 的大小以变小时,黄色部分缩小直到消失,但我想保持尺寸固定,蓝色部分缩小(中间的框架).有人有想法吗?
我已经尝试过 grid_propagate(False) 并且我已经看过相关问题,但是它要么没有效果,要么不适合我的示例。感谢您的帮助
使用 grid()
而不是 pack()
这可以如下所示完成:
from tkinter import *
root = Tk()
topframe = Frame(root, bg='red')
midframe = Frame(root, bg='blue')
bottomframe = Frame(root, bg='yellow')
root.rowconfigure([0,2], minsize=90) # Set min size for top and bottom
root.rowconfigure(1, weight=1) # Row 1 should adjust to window size
root.columnconfigure(0, weight=1) # Column 0 should adjust to window size
topframe.grid(row=0, column=0, sticky='nsew') # sticky='nsew' => let frame
midframe.grid(row=1, column=0, sticky='nsew') # fill available space
bottomframe.grid(row=2, column=0, sticky='nsew')
toplabel = Label(topframe, bg='red', text='Must be non resizable unless window cannot fit it \n (Contains buttons)',height=10)
midlabel = Label(midframe, bg='blue', text='Must be resizable \n (Contains a graph)',height=10)
bottomlabel = Label(bottomframe, bg='yellow', text='Must be non resizable unless window cannot fit it \n (Contains simulation results)',height=10)
toplabel.pack(fill=X,expand=TRUE)
midlabel.pack(fill=X,expand=TRUE)
bottomlabel.pack(fill=X,expand=TRUE)
root.mainloop()
您可能还想设置 window 最小尺寸。