When creating a GUI with Tkinter, the size of the window is usually determined by the size and location of the components in the window. However, you can regulate the window size by giving it a specific width and height. The geometry() function on the window with the width and height strings supplied as arguments can be used to change the size of the Tkinter window. In this tutorial, we have provided you with guidelines on how to specify a window size for a Tkinter GUI application in Python.

Syntax of the Geometry() Method

When using Python Tkinter, utilize the geometry() function on the Tk() class variable to set the window size. The geometry() method has the following syntax:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Set-Window-Size-1.png" data-lazy- height="34" src="data:image/svg xml,” width=”441″>

This geometry method is called the parent object, which is a Tkinter object in this case. We now know that we can size our window in Tkinter by utilizing the geometry and minsize() methods.

However, we must first import the Tkinter module into our application to use both of these methods, as we can only use the Tkinter object to call them. Height and width are used as parameters in both of these procedures, either direct or indirect.

Example 1:

In the example here, we are sizing our window using both approaches mentioned. First, we must import the Tkinter module into our application, as we are aware that this is the module used to generate the GUI in Python.

We use the import keyword for importing the module in our program, followed by the library name. After that, we’ll build a Tkinter object, which will be referred to as a parent object in this case. Both geometry() and minsize() methods can be called on this parent object.

Tkinter object “parent” can be used to call the geometry() method. Only one parameter is required, which is a string value split by the “x” operator. To put it another way, it converts the height and width into a new format.

In this scenario, the window is set to 250×250 pixels. Our window is also sized using the minsize() function. The width and height parameters are required. Because it can only be accessed by its object, it is called the Tkinter object. The first is the width size, which we are currently assigning, and the second is the height size.

Finally, the mainloop() method is called. This procedure is used to start the window in Tkinter. The Tkinter object is also called using this method. It’s critical to call; otherwise, we won’t see out the window:

from tkinter import *


parent_obj = Tk()


parent_obj.minsize(width=250, height=250)


parent_obj.geometry(“250×250”)


parent_obj.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Set-Window-Size-2.png" data-lazy- height="96" src="data:image/svg xml,” width=”538″>

Here, you can see the result:

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Set-Window-Size-3.png" height="292" src="data:image/svg xml,” width=”266″>

Example 2:

We’ll learn how to make the Python Tkinter window full screen in this example. There are a few options for making the app fullscreen by default. The first approach requires the screen’s resolution. You can directly specify height and width if you know the screen resolution.

Another option is to change the parent window’s property to True for full screen. Regardless of the display size, the screen is set to full screen in this manner. To put it another way, the software occupies the entire screen. This solution has the drawback of requiring the close and other buttons to be made manually. Here, we’ve set the full screen to True in the following code:

from tkinter import *


ws1 = Tk()


ws1.title(‘Tkinter Set Window Size Example’)


ws1.attributes(‘-fullscreen’, True)


Label(


    ws1,


    text =“Python is a high-level, interactive, and object-oriented language.”,


    font=(‘Times’, 24)


    ).pack(fill=BOTH, expand=True)


ws1.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Set-Window-Size-4.png" data-lazy- height="209" src="data:image/svg xml,” width=”568″>

Python Tkinter is running in full-screen mode in this output. The standard toolbars for shutting, reducing, and increasing the screen are noticeably absent:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Set-Window-Size-5.png" data-lazy- height="768" src="data:image/svg xml,” width=”1366″>

Example 3:

While working on some specific applications, we are sometimes required to change the window size so that widgets show in the same position where you set them. As a result, we’ll learn how to utilize Python Tkinter to set fixed window size in this part. We’ll achieve this by passing (0,0) to the resizable method.

For width and height, 0,0 denotes False. The resizable method highlights whether or not this window can change its size to the window manager. It only accepts Boolean values. Here’s how to change the size of a window with the code:

from tkinter import *


ws1 = Tk()


ws1.title(‘Tkinter Set Window Size’)


ws1.geometry(‘430×310 650 180’)


ws1.resizable(0,0)


Label(


    ws1,


    text=“Python is a high-level, interactive, n and object-oriented language.”,


    font=(‘Times’, 16)


    ).pack(fill=BOTH, expand=True)


ws1.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Set-Window-Size-6.png" data-lazy- height="211" src="data:image/svg xml,” width=”604″>

The box, as you can see below, has a locked window. The window’s size is fixed, and the user will not be able to adjust it:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Set-Window-Size-7.png" data-lazy- height="353" src="data:image/svg xml,” width=”441″>

Example 4:

We’ll learn how to establish the minimum window size in Python Tkinter in this section. The number of windows that can be decreased is determined by the minimum window size. Without this, the window can be shrunk to any size. The minsize() method is used to set the window’s maximum size, after which it will not shrink. We’ve only enabled users to reduce the window by 60 and 50 pixels in this code.

As you can see, the geometry is 280×340, with a minimum width of 220 pixels and a maximum height of 290 pixels. The difference is 60 points and 50 points, respectively. As a result, the window can be shrunk 60 percent from left to right and 50 percent from bottom to top:

from tkinter import *


ws1 = Tk()


ws1.title(‘Tkinter Set Window Size Example’)


ws1.geometry(‘280×340’)


ws1.minsize(220,290)


Label(


    ws1,


    text=“Python is a high-level, interactive, n and object-oriented language.”,


    font=(‘Times’, 12),


    bg = ‘#F0B27A’,


    ).pack(fill=BOTH, expand=True)


ws1.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Set-Window-Size-8.png" data-lazy- height="252" src="data:image/svg xml,” width=”584″>

Three images are displayed in this output. The top one shows the window in its original state when the code is executed. The second image on the left shows that when a user wants to shrink or reduce the window from the right side to the left side, he may only do so by 60 pixels and 50 pixels for the height. So, in Python Tkinter, this is how we limit the window size.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Set-Window-Size-9.png" data-lazy- height="377" src="data:image/svg xml,” width=”287″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Set-Window-Size-10.png" data-lazy- height="380" src="data:image/svg xml,” width=”226″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Set-Window-Size-11.png" data-lazy- height="330" src="data:image/svg xml,” width=”226″>

Conclusion:

With the help of well-detailed examples, we learned how to set the window size for a GUI application built with Tkinter in this post. Plus, we have added four examples for our readers to get a detailed idea of the Tkinter window size feature. We hope you found this article helpful. Check out other Linux Hint articles for more tips and ideas.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content