35

The size of Tkinter windows can be controlled via the following methods:

.minsize()
.maxsize()
.resizable()

Are there equivalent ways to control the size of Tkinter or ttk Frames?

@Bryan: I changed your frame1.pack code to the following:

frame1.pack(fill='both', expand=True)
frame1.bind( '<Configure>', maxsize )

And I added this event handler:

# attempt to prevent frame from growing past a certain size
def maxsize( event=None ):
    print frame1.winfo_width()
    if frame1.winfo_width() > 200:
        print 'frame1 wider than 200 pixels'
        frame1.pack_propagate(0)
        frame1.config( width=200 )
        return 'break'

The above event handler detects that a frame's width is too big, but is unable to prevent the increase in size from happening. Is this a limitation of Tkinter or have I misunderstood your explanation?

nbro
  • 15,395
  • 32
  • 113
  • 196
Malcolm
  • 5,125
  • 10
  • 52
  • 75
  • 2
    I think you misunderstood. My explanation was pointing out that if you pack with expand set to False and fill set to None, the frame will have a static width and won't grow or shrink. If the frame changes size because of its parent, setting pack_propagate on the frame has no effect, you need to set it on the parent. pack_propagate only sets whether the frame changes size based on its content, not based on its parent. – Bryan Oakley Dec 09 '10 at 17:32

3 Answers3

36

There is no single magic function to force a frame to a minimum or fixed size. However, you can certainly force the size of a frame by giving the frame a width and height. You then have to do potentially two more things: when you put this window in a container you need to make sure the geometry manager doesn't shrink or expand the window. Two, if the frame is a container for other widget, turn grid or pack propagation off so that the frame doesn't shrink or expand to fit its own contents.

Note, however, that this won't prevent you from resizing a window to be smaller than an internal frame. In that case the frame will just be clipped.

import Tkinter as tk

root = tk.Tk()
frame1 = tk.Frame(root, width=100, height=100, background="bisque")
frame2 = tk.Frame(root, width=50, height = 50, background="#b22222")

frame1.pack(fill=None, expand=False)
frame2.place(relx=.5, rely=.5, anchor="c")

root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
7

A workaround - at least for the minimum size: You can use grid to manage the frames contained in root and make them follow the grid size by setting sticky='nsew'. Then you can use root.grid_rowconfigure and root.grid_columnconfigure to set values for minsize like so:

from tkinter import Frame, Tk

class MyApp():
    def __init__(self):
        self.root = Tk()

        self.my_frame_red = Frame(self.root, bg='red')
        self.my_frame_red.grid(row=0, column=0, sticky='nsew')

        self.my_frame_blue = Frame(self.root, bg='blue')
        self.my_frame_blue.grid(row=0, column=1, sticky='nsew')

        self.root.grid_rowconfigure(0, minsize=200, weight=1)
        self.root.grid_columnconfigure(0, minsize=200, weight=1)
        self.root.grid_columnconfigure(1, weight=1)

        self.root.mainloop()

if __name__ == '__main__':
    app = MyApp()

But as Brian wrote (in 2010 :D) you can still resize the window to be smaller than the frame if you don't limit its minsize.

Richard
  • 709
  • 1
  • 6
  • 15
1
# this code works for me

import tkinter as tk


class tk(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()


window = tk()
window.master.title("window")
window.master.minsize(100, 100)
window.master.maxsize(300, 300)
window.mainloop()
Ali Leo
  • 11
  • 1
  • That was 10 yrs ago. Because of that, you are using Python 3, but will not work on Python 2. So you don't have to worry looking back 10 yrs ago. – toyota Supra Mar 01 '23 at 13:52
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 05 '23 at 20:06