How to control automated window resizing in Tkinter?

TkinterServer Side ProgrammingProgramming

The Tkinter window can be resized manually by defining the geometry ("width × height") method. We can automate or reset the window to its original form by passing an empty value to the geometry manager. Once the empty value is passed to the method, it will get resized automatically. In this example, we will create a Tkinter application that will display a Toplevel window (Popup window) with a defined size. When we press a RESET button, it will be resized again to its default size.

Example

#Import the library
from tkinter import *
from tkinter import ttk

#Create an instance of tkinter frame
win= Tk()

#Set the window geometry
win.geometry("750x200")

#Create a Label
Label(win, text= "Tkinter is a GUI Library in Python", font=('Helvetica 15 bold')).pack(pady=20)

#Define a function to reset the window size
def reset_win():
   win.wm_geometry("750x250")
   button.destroy()

#Create a Button to Hide/ Reveal the Main Window
button= ttk.Button(win, text="RESET" ,command= reset_win)
button.pack(pady=50)

win.mainloop()

Output

Running the above code will display a window that contains a button. Once we click the RESET Button it will get resized to its default size which prevents the window to be resized manually.

Now, click the Button to reset the size of the Main window that will automatically get resized to its default size.

raja
Published on 03-May-2021 11:52:38
Advertisements