In this tutorial, we will learn how easily we can change the Tkinter window size using Python.
At first, I will show you how to do this using a custom function and then we will show you the basic program without using a function.
Using the geometry method we will pass the value of width and height through variables.
import tkinter as tk newWindow = tk.Tk() newWindow.geometry("300x200") def on_button_click(): width = 700 height = 700 newWindow.geometry(f"{width}x{height}") button = tk.Button(newWindow, text="Click Me", command=on_button_click) button.pack(pady=20) newWindow.mainloop()
Output
So whenever the user will click on the button, it will run the function on_button_click()
and it will make change the screen size according to the value of variable width and height.
To change the default window size of a Tkinter window
Using the geometry method of the Tk class.
HereĀ is an example of how to set the default size of a Tkinter window:
import tkinter as tk root = tk.Tk() root.geometry("800x600") root.mainloop()
In this example, the root.geometry (“800×600”) line sets the default size of the window to 800 pixels wide and 600 pixels tall.
You can adjust the dimensions to your preferred size.