Allow the user to resize the Window indiscriminately can cause various problems to your widget layouts. It is often quite beneficial to set certain “restrictions” such as specifying a minimum and/or maximum size for the Tkinter Window. This is also a better alternative than just outright disabling the resize option in your Window (which can hurt user experience).
How to set Minimum/Maximum Tkinter Window Size?
There are two functions that we can use to set the minimum and maximum sizes for your Tkinter Window respectively. Both of these functions are called as methods on the Tkinter Instance (created from the Tk()
function).
The first function is minsize()
, and takes two parameters (width and height) which define the minimum allowed dimensions. Likewise, we have the maxsize() function which takes two parameters (width and height) for the maximum allowed dimensions.
Try running the below code, and resizing the window yourself to see it in action.
import tkinter as tk
root = tk.Tk()
root.minsize(300, 200)
root.maxsize(600, 400)
root.mainloop()
It is also interesting to note, that Tkinter will automatically increase the size of the Window to the “minsize” specified when it is first displayed (even though its an empty window).
Even calls to the root.geometry()
function will be ignored if they violate the minimum and maximum sizes specified by the minsize()
and maxsize()
functions.
Hope you find this information useful.
This marks the end of the How to Set Maximum or Minimum Window Size in Tkinter? Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the Tutorial content can be asked in the comments section below.