How to Set a Tkinter Window with a Constant Size?

PythonTkinterGUI-Programming

Sometimes, the tkinter frame gets resized automatically according to the size of the widgets. To make the frame constant in size, we have to stop the widgets to resize the frame. So there are three methods,

  • Boolean pack_propagate(True/False) method prevents the resizing of the frame from the widget.

  • resizable(x,y) method prevents the window to be resized.

  • Pack(fill, expand) values which resize the window to its defined size in the geometry.

Basically, all the widgets inside the tkinter frame will be responsive and cannot be resized.

Example

from tkinter import *

win= Tk()
win.geometry("700x300")

#Don't allow the screen to be resized
win.resizable(0,0)

label= Label(win, text= "Select an option", font=('Times New Roman',12))
label.pack_propagate(0)
label.pack(fill= "both",expand=1)

def quit():
   win.destroy()

#Create two buttons
b1= Button(win, text= "Continue")
b1.pack_propagate(0)
b1.pack(fill="both", expand=1)
b2= Button(win, command= quit, text= "Quit")
b2.pack_propagate(0)
b2.pack(fill="both", expand=1)

win.mainloop()

Output

Running the above code will make the window constant to its size which is nonresizable.

raja
Published on 06-Mar-2021 08:39:35
Advertisements