Python Tkinter Window Size

In this Tutorial will we learn about Python Tkinter window size also we will cover these topics.

  • Configure window size and position in Python Tkinter.
  • How to set the window size to full screen in Python Tkinter?
  • How to set the fixed size of the window in Python Tkinter?
  • How to lock the window size in Python Tkinter?
  • How to set minimum window size?
  • How to set maximum window size?

If you new Python Tkinter, check out Python GUI Programming (Python Tkinter).

Python Tkinter Window Size and Position

  • Window size refers to the height and width of the window.
  • Window position refers to the appearing place of the window on the screen.
  • In this section, we will learn how to adjust the window & how to position the window in Python Tkinter.
  • geometry is the method used for both purposes.

Syntax:

parent_window.geometry("width_size x height_size + x_position + y_position")
  • width_size : accepts only integer value & determines the horizontal space of the window.
  • height_size: accepts only integer value & determines the vertical space of the window.
  • x_position: accepts only integer value & pushes the window in the vertical position.
  • y_position: accepts only integer value & pushes the window in the horizontal position.

Code:

In this code, the window is created with 350 widths and 450 height. It is positioned at x = 700 & y=200. So everytime code will be run, it appears slightly right to the center.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('350x450+700+200')

Label(
    ws,
    text="Life means lot more \n than you know",
    font=('Times',20)
    ).pack(fill=BOTH, expand=True)

ws.mainloop()

Output:

In this output, the window has appeared slightly to the right from the centre.

Python Tkinter Window Size and Position
Python Tkinter Window Size and Position

Python Tkinter Window Size Fullscreen

  • In this section, we will learn how to set Python Tkinter window size to full screen.
  • There are a couple of ways to set the application to fullscreen by default.
  • The first method requires the resolution of the screen.
  • If you know the screen resolution then you can directly provide height & width. Like in, my case the resolution is 1920×1080 so,
ws.geometry("1920x1080)

Here, 1920 is the width of the window and 1080 is the height.

  • Another way is to set the attribute of the parent window to True for full screen.
  • In this method, the screen is set to full screen irrespective of the display size.
  • In other words, the application goes to full screen on all the devices.
  • The drawback of using this is you have to manually create close and other buttons.
ws.attributes('-fullscreen', True)

Code:

READ:  Python check if a variable is an integer

In this code, we have displayed the example of the second method wherein we have set the full screen to True for the attribute method.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.attributes('-fullscreen', True)

Label(
    ws,
    text="Life means lot more \n than you know",
    font=('Times', 20)
    ).pack(fill=BOTH, expand=True)

ws.mainloop()

Output:

In this output, Python tkinter is in full screen mode. Also you won’t notice regular toolbars for closing , minimising or maximising the screen.

python tkinter windows fullscreen
Python Tkinter Window Size Fullscreen

Python Tkinter Fixed Window Size

  • While working on the application, at times, we want to fix the window size so that widgets appear at the same place where you have fixed them.
  • So, in this section, we will learn how to set fixed window size in Python Tkinter.
  • To do so, we will pass (0,0) in the resizable method. Here 0,0 refers to False for width & Height.
  • resizable method instructs window manager if this window can be resized or not.
  • It accepts only boolean values.

Syntax:

ws.resizable(0, 0)

ws.resizable(False, False)

ws.resizable(width=False, Height=False)

All these expressions do the same thing. You can use anyone method out of these.

Code:

In this code, we have fixed the size of a window. This method is also called locking of screen. User will not allowed to change the size of the screen.

from tkinter import *

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

Label(
    ws,
    text="Life means lot more \n than you know",
    font=('Times', 20)
    ).pack(fill=BOTH, expand=True)

ws.mainloop()

Output:

In this output, there are two images displayed. first image displays the regular window wherein user can change the size of window by clicking on the maximisie square button on the other hand, the other boxhas locked window. The size of the window is fixed and user won’t be able to change it.

python tkinter windows fixed size
Python Tkinter Fixed Window Size

Python Tkinter Lock Window Size

  • While working on the application, at times, we want to fix the window size so that widgets appear at the same place where you have put them.
  • So, in this section, we will learn how to lock window size in Python Tkinter.
  • Locking window size simply means the user won’t be able to change the window size.
  • To do so, we will pass (0,0) in the resizable method. Here 0,0 refers to False for width & Height.
  • resizable method instructs window manager if this window can be resized or not.
  • It accepts only boolean values.
  • This is the same as our previous section on the python Tkinter fixed window size.
READ:  Python Get Current Folder Name

Syntax:

ws.resizable(0, 0)

ws.resizable(False, False)

ws.resizable(width=False, Height=False)

All these expression do the same thing. You can use anyone method out of these.

Output:

In this output, window is locked that means it can’t be resized. First image on the left is an example of regular window wherein user can change the size of the window but the side side image has maximise button disabled that means user is not allowed to change the size of the window.

python tkinter windows fixed size
Python Tkinter Lock Window Size

Another way of locking window size is by providing minsize() and maxsize() same as geometry size. We have covered about these in our next section.

Python Tkinter Minimum Window Size

  • In this section, we will learn to set the minimum window size in Python Tkinter.
  • The minimum window size determines the number of windows that can be shrunk. Without this user can shrink the window to any level.
  • minsize() method is used to set the limit after which the window won’t shrink.

Syntax:

ws.minsize(width_minsize, height_minsize)
  • width_minsize() accepts integer value & it stops the shrink coming from East to west direction.
  • height_minsize() accepts integer value & stops the shrink coming from south to North direction.

Code:

In this code, we have allowed users to shrink the window for only 50 pixels only. Ask you can see the geometry is 300×400 and minsize is 250 for width & 350 for height. the difference for both is 50. So window can be shrunk 50 from the left to right & 50 from bottom to top.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('300x400')

ws.minsize(250, 350)

Label(
    ws,
    text="Life means lot more \n than you know",
    font=('Times', 20),
    bg = '#156475',
    fg = '#fff'
    ).pack(fill=BOTH, expand=True)

ws.mainloop()

Output:

  • In this output, three pictures are displayed. The first one on the top depicts the original form of the window when code is run.
  • The second picture on the left side depicts that when a user tries to reduce or shrink the window from the right side to the left side then he is only able to reduce it for 50 pixels same happens with the height.
  • So this is how we restrict the window shrunk in Python Tkinter.
python tkinter window minsize
Python Tkinter Minimum Window Size

Python Tkinter Max Window Size

  • In this section, we will learn to set the maximum window size in Python Tkinter.
  • The maximum window size determines the maximum amount of window expansion.
  • Without this user can expand the window to any level.
  • maxsize method is used to set the limit after which the window won’t expand.
READ:  Matplotlib scatter marker

Syntax:

ws.maxsize(width_maxsize, height_maxsize)
  • width_maxsize() accepts integer value & it stops the expansion going from west to east direction.
  • height_maxsize() accepts integer value & stops the expansion going from North to South direction.

Code:

In this code, we have allowed users to expand the window size for only 50 pixels only. as you can see the geometry is 300×400 and maxsize is 350 for width & 450 for height. the difference for both is 50. So window can be expand to 50 from the right to left & 50 from top to bottom.

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.config()
ws.geometry('300x400')

ws.maxsize(350, 450)

Label(
    ws,
    text="Life means lot more \n than you know",
    font=('Times', 20),
    bg = '#156475',
    fg = '#fff'
    ).pack(fill=BOTH, expand=True)

ws.mainloop()

Output:

  • In this output, three pictures are displayed. The first one on the top depicts the original form of the window when code is run.
  • The second picture on the left side depicts that when a user tries to expand the window size from the left side to the right side then he is only able to expand it for 50 pixels same happens with the height.
  • So this is how we restrict the window expansion in Python Tkinter.
python tkinter window maxsize
Python Tkinter Max Window Size

You may like the following Python Tkinter tutorials:

In this tutorial, we have learned about different ways of configuring Python Tkinter window size. Also, we have covered these topics.

  • Configure window size and position in Python Tkinter.
  • How to set the window size to full screen in Python Tkinter.
  • How to set the fixed size of the window in Python Tkinter.
  • How to lock the window size in Python Tkinter.
  • How to set minimum window size.
  • How to set maximum window size.