【问题标题】:How to prevent a tkinter frame from resizing in a set of frames如何防止 tkinter 框架在一组框架中调整大小
【发布时间】:2018-11-29 07:05:33
【问题描述】:

对于一个模拟应用程序,我正在用 python 中的 Tkinter 模块设计一个 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()

所以我得到了这样的窗口:

The window I get

但我的问题是,当我调整窗口大小以变小时,黄色部分会缩小直到消失,但我想保持大小固定,蓝色部分缩小(中间的框架)。有人有这个想法吗?

我已经尝试过 grid_propagate(False) 并且我已经看过相关的问题,但是它要么没有效果要么不适合我的示例。感谢您的帮助

【问题讨论】:

    标签: python tkinter resize


    【解决方案1】:

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

    您可能还想设置窗口最小尺寸。

    【讨论】:

    • 是的,我已经在我的实际应用程序中应用了它,但是中间框架(蓝色框架)不能缩小到比它的初始大小更小的大小。所以它不能解决我的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    相关资源
    最近更新 更多