Tkinter Set Window Size

When creating a GUI interface with the Tkinter, the windows’ size is usually a result of the size and location of the windows’ components. However, this is regulable by providing a specific width and height.

You can change the size of a Tkinter window by passing a width and height string as an input to the geometry() method. Using sample programs, we will learn how to set a particular window size for our Tkinter GUI application in Python in this article.

Size and Position of the Python Tkinter Window

The height and width of a window are referred to as its size. The window’s position on the screen is referred to as its window position. This section will look at how to change the window and position it in Python Tkinter. The method used for both goals is geometry.

The syntax is as follows:

parent_window.geometry("size_of_width x size_of_height + position_x + position_y")
  • width_size: only accepts integer values and controls the window’s horizontal space.
  • height_size: only accepts integer values and determines the window’s vertical space.
  • x_position: only accepts integer values and moves the window vertically.
  • y_position: only takes integer values and moves the window horizontally.

The window is formed with 350 widths and 450 heights in this code. It is located at x=700 and y=200. Consequently, whenever the code is run, it appears slightly to the right of the center.

from tkinter import *

window_size = Tk()
window_size.title('Codeunderscored') 
window_size.geometry('350x450+700+200')

Label(
    window_size,
    text="Code means lot more \n than you could possibly comprehend",
    font=('Times',20)
    ).pack(fill=BOTH, expand=True)

window_size.mainloop()


window is formed with 350 widths and 450 heights
the window is formed with 350 widths and 450 heights

Geometry() – syntax

When using Tkinter in Python, the geometry() function on the Tk() class variable is used to set the window size.

#import statement
from tkinter import *
#create GUI Tk() variable
code_gui = Tk()
#set window size
code_gui.geometry("widthxheight")

It would be best to substitute width and height with numbers corresponding to the window’s width and height. In the argument given to geometry(), notice an x between width and height. Please note that the width x height of the window does not include the window title.

Example 1: Set Window Size in Python Tkinter

This example will utilize the geometry() method to provide the Tk() window with a 500 by 200 pixels fixed size. The program is written in Python.

from tkinter import *

code_gui = Tk(className='Codeunderscored Python Examples - Window Size')
# set window size
code_gui.geometry("500x200")

code_gui.mainloop()

Example 2: Change the size of your GUI application’s window.

Now, let’s adjust the width and height parameters passed to the geometry() function. Let’s say 300 x 300.

from tkinter import *

code_gui = Tk(className='Codeunderscored Python Examples - Window Size')
# set window size
code_gui.geometry("300x300")

code_gui.mainloop()

How can I make a Tkinter window with a fixed size?

The goal is to create a Tkinter window with a fixed size. You cannot alter a window with a limited size to suit the user’s needs; its dimensions are fixed. On the other hand, you can resize a standard window.

Approach

Declare a Tkinter object by importing the module. Using the maxsize() and minsize() functions, create a fixed-size window.

Syntax:

minsize(height, width)

In this case, both the height and width are measured in pixels. The minsize() method in Tkinter sets the Tkinter window’s minimum size. A user can use this method to reduce the window’s initialized size to its smallest size while still being able to maximize and scale it.

maxsize(height, width)

This method is used to set the root window’s maximum size. The user will still be able to minimize the window to its smallest size.

Program 1: Making a standard window

# Import module
from tkinter import *

# Create object
root = Tk()

# Adjust size
root.geometry("400x400")

# Execute tkinter
root.mainloop()

Program 2: Creating a window with a constant size

# Import module
from tkinter import *

# Create object
root = Tk()

# Adjust size
root.geometry("400x400")

# set minimum window size value
root.minsize(400, 400)

# set maximum window size value
root.maxsize(400, 400)

# Execute tkinter
root.mainloop()

Fullscreen Python Tkinter Window

We’ll learn to create the Python Tkinter window full screen in this part. For your information, few options exist for making the app fullscreen by default. The first approach necessitates the use of the screen’s resolution.

In addition, you can directly specify height and width if you know the screen resolution. For example, in my situation, the resolution is 1920 x 1080, therefore,

window_screen.geometry("1920x1080)

The window’s width is 1920 pixels, and the height is 1080 pixels. The other alternative is changing the parent window’s property to True for full screen.

  • In this technique, the screen is set to full screen, regardless of the display size.
  • In other words, the software takes up the entire screen on all smartphones.
  • The disadvantage of this method is that you must manually build close and other buttons.
window_screen.attributes('-fullscreen', True)

We’ve shown an example of the second technique in this code, where we’ve set the entire screen to True. That is in regards to the attribute method.

from tkinter import *

window_screen = Tk()
window_screen.title('Codeunderscored') 
window_screen.attributes('-fullscreen', True)

Label(
    window_screen,
    text="Code is very interesting \n all you need is patience",
    font=('Times', 20)
    ).pack(fill=BOTH, expand=True)

window_screen.mainloop()

Fixed Window Size in Python Tkinter

We sometimes wish to fix the window size while working on the application so that widgets display in the same area where you have fixed them. This section will learn how to set fixed window sizes using Python Tkinter.

  • We’ll achieve this by bypassing (0,0) the resizable method. In the case of the width and height, 0,0 denotes False.
  • The resizable method tells the window manager whether or not you can resize this window.
  • It only accepts boolean values.

Syntax:

window_screen.resizable(0, 0)
window_screen.resizable(False, False)
window_screen.resizable(width=False, Height=False)

All of these formulations accomplish the same goal. Any of these methods are at your disposal for your use. The size of a window has been fixed in this code. This method is also known as screen locking. The user will not be able to modify the screen’s size.

from tkinter import *

window_screen = Tk()
window_screen.title('Codeunderscored') 
window_screen.geometry('350x450+700+200')
window_screen.resizable(0,0)

Label(
    window_screen,
    text="Code is more meaningful \n if you can resonate with it",
    font=('Times', 20)
    ).pack(fill=BOTH, expand=True)

window_screen.mainloop()

Two photos are displayed in this output. On the one hand, the first image shows a conventional window that can be resized by clicking on the maximize square button; on the other hand, the second image shows a locked window. As a result, the window’s size is fixed, and the user will not be able to adjust it.

Tkinter Lock Window Size in Python

We sometimes wish to fix the window size while working on the project so that widgets show in the same place you installed them. As a result, we’ll learn how to lock window size in Python Tkinter in this part.

  • The user will be unable to modify the window size if the window is locked.
  • We’ll achieve this by bypassing (0,0) the resizable method. In the case of the width and height, 0,0 denotes False.
  • The resizable method tells the window manager whether or not you can resize this window.
  • It only accepts boolean values.
  • It is the same as the previous section on the fixed window size in Python Tkinter.

Syntax:

  • window_screen.resizable(0, 0)
  • window_screen.resizable(False, False)
  • window_screen.resizable(width=False, Height=False)

All of these formulations accomplish the same goal. The choice is yours!

The window in this output is locked, which means you cannot resize it. The first image on the left is an example of a typical window in which the user can adjust the size of the window. Still, the image on the right has the maximize button disabled, indicating that the user is not permitted to change the size of the window.

Another technique to lock window size is to set minsize() and maxsize() to the same value as the geometry size.
In the coming part, we will explore them thoroughly.

Minimum Window Size for Python Tkinter

We’ll delve into establishing the minimum window size in Python Tkinter in this section. The window’s minimum size determines the number of windows that can be decreased. Without this, you can shrink the window to any size. The minsize() process sets the window’s maximum size, after which it will not shrink.

Syntax:

window_screen.minsize(width_minsize, height_minsize)
  • width_minsize() takes an integer value and prevents shrinking from the east to the west.
  • height_minsize() takes an integer number and stops the shrinking from the south to the north.

We’ve only enabled the user’s capability to reduce the window by 50 pixels in this code. As you can see, the geometry is 300400, with a minimum width of 250 pixels and a maximum height of 350 pixels. The difference between the two is 50.

Consequently, the window can be shrunk 50 percent from left to right and 50 percent from bottom to top.

from tkinter import *

window_size = Tk()
window_size.title('Codeunderscored')
window_size.geometry('300x400')

window_size.minsize(250, 350)

Label(
    window_size,
    text="Coding is  alot fun \n if you are not implored to",
    font=('Times', 23),
    bg = '#156475',
    fg = '#fff'
    ).pack(fill=BOTH, expand=True)

window_size.mainloop()

Three images are the results displayed in this output. First, when the code is executed, the top one shows the window in its original state.
.

On the other hand, 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 50 pixels, which is true for the height. As it turns out, in Python Tkinter, this is how we limit the window size.

Tkinter Max Window Size in Python

This section will teach us to set the maximum window size in Python Tkinter. The maximum window size influences how much the window can expand. In addition, the user can expand the window to any level if this is not enabled. The maxsize method also provides the maximum size beyond which the window will not expand.

Syntax:

window_size.maxsize(width_maxsize, height_maxsize)
  • width_maxsize() takes an integer number and prevents the expansion from traveling west to east.
  • height_maxsize() takes an integer number and stops the expansion from moving north to south.

We’ve only enabled users to expand the window size by 50 pixels in this code. As you can see, the geometry is 300400, with a maximum width of 350 pixels and a maximum height of 450 pixels. The difference between the two is 50. As a result, the window can be expanded by 50 percent from right to left and 50 percent from top to bottom.

from tkinter import *

window_size = Tk()
window_size.title('Codeunderscored') 
window_size.config()
window_size.geometry('300x400')

window_size.maxsize(350, 450)

Label(
    window_size,
    text="Coding means the world  \n if you reach the level I am in life now",
    font=('Times', 20),
    bg = '#156475',
    fg = '#fff'
    ).pack(fill=BOTH, expand=True)

window_size.mainloop()

The resultant three images are displayed in this output. Initially, when the code is executed, the top one shows the window in its original state-. On the flip side, the second image on the left shows that when a user wants to extend the window size from left to right, he may only do so for 50 pixels. The same is true for height. Interestingly, in Python Tkinter, this is how we limit window expansion.

Example: Showing how to set fixed window size in Tkinter

Sometimes it’s necessary to resize the window so that widgets appear in the same place you configured them. As a result, we’ll learn how to use Python Tkinter to set fixed window size in this section. We’ll do this by calling the resizable method with the value (0,0).

False denotes width and height of 0,0. The resizable technique indicates whether or not this window can change its size to the window manager. Only Boolean values are accepted. Here’s how to use the code to modify the size of a window:

from tkinter import *
window_size = Tk()
window_size.title('Codeunderscored Tkinter Set Window Size')
window_size.geometry('430x310+650+180')
window_size.resizable(0,0)
Label(
    window_size,
    text="Coding in Python is more interactive, \n and clear than other OOP-languages I have used.",
    font=('Times', 16)
    ).pack(fill=BOTH, expand=True)
window_size.mainloop()

The box has a locked window, as you can see below. The window size is fixed, and the user will be unable to change it.

Showing how to set fixed window size in Tkinter
Showing how to set fixed window size in Tkinter

Example: How to make a Python Tkinter window fullscreen

We’ll explore making the Python Tkinter window full screen in this example. To make the program fullscreen by default, you have a few alternatives. The first method necessitates the resolution of the screen. You can set the height and width explicitly if you know the screen resolution.

Another alternative is to set the parent window’s full-screen attribute to True. The screen is set to full screen regardless of the display size. In other words, the software takes over the entire screen. This approach is disadvantageous of requiring the manual operation of the close and other buttons. In the following code, we’ve set the whole screen to True:

from tkinter import *
window_size = Tk()
window_size.title(' Codeunderscored Tkinter Set Window Size ')
window_size.attributes('-fullscreen', True)
Label(
    window_size,
    text ="Coding in Python is enjoyable, the syntax is absolutely fantastic.",
    font=('Times', 24)
    ).pack(fill=BOTH, expand=True)
window_size.mainloop()

In this output, Python Tkinter is running in full-screen mode. The standard toolbars for shutting down, shrinking, and expanding the screen are missing.

How to make a Python Tkinter window fullscreen
How to make a Python Tkinter window fullscreen

Summary

We learned to set the window size for a GUI application built with Tkinter in this Python Examples tutorial. This tutorial taught us many methods for adjusting Python Tkinter window size.

The size of the window in a Tkinter GUI is usually dictated by the size and location of the components in the window. You can, however, control the window’s size by specifying its width and height. To modify the size of the Tkinter window, use the geometry() function on the window with the width and height strings as arguments.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *