How to Create a Tkinter Window with a Constant Size

The Tkinter window size is by default resizable even if you assign the width and height when the window instance is initiated.

import tkinter as tk

app = tk.Tk()

app.geometry('200x200')

app.mainloop()

You could drag the window created by the above codes to get different windows sizes. resizable function should be used to restrict the width and height size.

import tkinter as tk

app = tk.Tk()

app.geometry('200x200')
app.resizable(width=0, height=0)

app.mainloop()

app.resizable(width=0, height=0) doesn’t allow both width and height to be resized.

Set the frame with a constant size

The frame inside the window is a bit similar to the window, in the sense that it automatically resizes itself even if you define the width and height of that frame.

import tkinter as tk

app = tk.Tk()

app.geometry('200x200')
app.resizable(0, 0)

backFrame = tk.Frame(master=app,
                    width=200,
                    height=200,
                    bg='blue').pack()

button1 = tk.Button(master=backFrame, 
                    text='Button 1', 
                    bg='blue', 
                    fg='red').pack()
button2 = tk.Button(master=backFrame, 
                    text='Button 2', 
                    bg='blue', 
                    fg='green').pack()
button3 = tk.Label(master=backFrame, 
                   text='Button 3', 
                   bg='red',
                   fg='blue').pack()

app.mainloop()

The GUI what we expected is like below,

Tkinter Frame Not Resizable

But what we get after executing the above codes is,

Tkinter Frame Resizable

The frame backFrame resizes automatically to fit the widgets attached to it. Or in other words, the widgets inside backFrame control their parent frame’s size.

You could disable the frame to resize to its widgets by setting pack_propagate(0).

The correct code to freeze the frame size is,

import tkinter as tk

app = tk.Tk()

app.geometry('200x200')
app.resizable(0, 0)

backFrame = tk.Frame(master=app,
                    width=200,
                    height=200,
                    bg='blue')
backFrame.pack()
backFrame.propagate(0)

button1 = tk.Button(master=backFrame, 
                    text='Button 1', 
                    bg='blue', 
                    fg='red').pack()
button2 = tk.Button(master=backFrame, 
                    text='Button 2', 
                    bg='blue', 
                    fg='green').pack()
button3 = tk.Label(master=backFrame, 
                   text='Button 3', 
                   bg='red',
                   fg='blue').pack()

app.mainloop()