| by Arround The Web | No comments

Tkinter Set Window Size

When you are building a GUI with the Tkinter library in Python, you need to consider the window size of your GUI. Various different ways and options allow you to set the size and the properties of the window created with the Tkinter library. This post will guide you through all of these different ways and methods to set the window size of Tkinter GUI.

This post will cover the following different methods:

Method 1: Set Window Size Using the geometry() Method

The most common method for setting the window size in the Tkinter GUI is to use the geometry() method. This method takes in a string as an argument that defines the width and height of the window. To define the width and height, first, type the width size in pixels, then add an “x,” and then add the height size in pixels.

To demonstrate the working of the geometry() method, take the following code snippet:

from tkinter import *
frame = Tk()
frame.title("Tkinter Window Size")
frame.geometry("550x450")
Label(frame, text="This is a 550 by 450 Window!", font=("Arial",20,"bold")).pack()
frame.mainloop()

When this code is executed, it produces the following Tkinter window:

The output image confirms that you were able to create a Tkinter window of the size 550 by 450 successfully.

Method 2: Set the Minimum Window Size With the minsize() Method

You can use the minsize() method to set the minimum possible size of the Tkinter Window. To do this, simply call the minsize() method on the Tkinter’s frame/window variable and pass in the width and height size in separate arguments as integers.

To demonstrate the working of the minsize() method, you can use the following code snippet:

from tkinter import *
frame = Tk()
frame.title("Tkinter Window Size")
frame.minsize(300,300)
Label(frame, text="This Window has Minimum of 300, 300", font=("Arial",20,"bold")).pack()
frame.mainloop()

Execute this code and try to resize the window, and you will see the following behavior:

As you can see in the above output, the window couldn’t be reduced lower than 300 by 300 because you had set the minimum window size of 200 by 200.

Method 3: Set the Window to Fullscreen With the attributes() Method

If you wish to set the size of the window to fullscreen, then you can utilize the attributes() method. Apply the attributes() method on the Tkinter frame/window variable and then pass the first argument as “-fullscreen” and the second argument as “True”.

To demonstrate the working of the attributes() method to make the window fullscreen, take the following code:

from tkinter import *
frame = Tk()
frame.title("Tkinter Window Size")
frame.attributes("-fullscreen",True)
Label(frame, text="This Window has Fullscreen Option Set with the attributes() method", font=("Arial",20,"bold")).pack()
frame.mainloop()

When this code is executed, the following window is displayed:

As you can see, the window has turned into a fullscreen window. However, this option removes the window control buttons (minimize, close) from the top bar, meaning you must create them manually.

Conclusion

To set a specific size of the Tkinter window, you can use the geometry() method. To set the minimum size of the Tkinter window, you can use the minsize() method. The minsize() method can also be used alongside the geometry() method. To make the Tkinter window() go fullscreen, you can use the attributes() method.

Share Button

Source: linuxhint.com

Leave a Reply