Python Tkinter – Set Window Size
Usually, when you create a gui using Tkinter, the size of the window depends on the size and placement of components in the window. But, you can also control the window size by setting a specific width and height to the window.
You can set the size of Tkinter window by calling geometry() function on the window with width and height string passed as argument.
In this tutorial, we shall learn how to set a specific window size to our Tkinter GUI application in Python, using example programs.
Syntax – geometry()
To set a specific size to the window when using Python tkinter, use geometry() function on the Tk() class variable.
#import statement from tkinter import * #create GUI Tk() variable gui = Tk() #set window size gui.geometry("widthxheight")
where width and height should be replaced with integers that represent the width and height of the window respectively. Observe that there is
x between width and height in the argument provided to
Note: Please note that window size width x height does not include the window title.
Example 1: Set Window Size in Python tkinter
In this example, we will use geometry() method to set a fixed window size of 500 by 200 to the Tk() window.
from tkinter import * gui = Tk(className='Python Examples - Window Size') # set window size gui.geometry("500x200") gui.mainloop()
When you run this program, the following GUI window will open, with the window size specified in
geometry() – width of 500 and height of 200.
Example 2: Set Window Size to your GUI application
Now, let us change the width and height provided to geometry() function. Say 300 by 300.
from tkinter import * gui = Tk(className='Python Examples - Window Size') # set window size gui.geometry("300x300") gui.mainloop()
In this tutorial of Python Examples, we learned how to set the window size for a GUI application built using tkinter, with the help of well detailed examples.