如何防止Tkinter框架调整为一组框架的大小

时间:2018-11-29 07:05:34

标签: python tkinter resize

对于模拟应用程序,我正在使用python中的Tkinter模块设计UI。我目前正在尝试对框架和窗口大小添加约束,以保持良好的界面,无论窗口大小如何。

在我的用户界面的一部分中,我有这样的东西:

class Demo {
  static Demo d=null;

  static {
     d=new Demo();
  }

  private Demo(){
     System.out.println("Private Constructor");
  }

  void add(){
    System.out.println("Hello I am Non-Static");
  }

  static Demo getInstance(){
    return d;
  }

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

The window I get

但是我的问题是,当我调整窗口的大小以使其更小时,黄色部分会缩小直到消失为止,但是我想保持固定大小,而蓝色部分要缩小(中间的框)。有人有想法吗?

我已经尝试过grid_propagate(False),并且已经看过相关问题,但是它没有任何作用或者不适合我的示例。谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

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

您可能还需要设置窗口的最小大小。

相关问题