Let’s start with a simple program that consists of a window:
import tkinter as tk
root = tk.Tk()
root.mainloop()
The root window has a title that defaults to tk. It also has three system buttons including Minimize, Maximize, and Close.
Let’s learn how to change the attributes of the root window.
To change the window’s title, you use the title() method like this:
window.title(new_title)
For example, the following changes the title of the root window to ‘Tkinter Window Demo’:
import tkinter as tk
root = tk.Tk()
root.title('Tkinter Window Demo')
root.mainloop()
To get the current title of a window, you use the title() method with no argument:
title = window.title()
Code language: Python (python)
In Tkinter, the position and size of a window on the screen is determined by its geometry.
The following shows the geometry specification:
widthxheight±x±y
In this specification:
To change the size and position of a window, you use the geometry() method:
window.geometry(new_geometry)
The following example changes the size of the window to 600x400 and the position of the window to 50 pixels from the top and left of the screen:
import tkinter as tk
root = tk.Tk()
root.title('Tkinter Window Demo')
root.geometry('600x400+50+50')
root.mainloop()
Sometimes, you may want to center the window on the screen. The following program illustrates how to do it:
import tkinter as tk
root = tk.Tk()
root.title('Tkinter Window - Center')
window_width = 300
window_height = 200
# get the screen dimension
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# find the center point
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
# set the position of the window to the center of the screen
root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
root.mainloop()
If you want to get the current geometry of a window, you can use the geometry() method without providing any argument:
window.geometry()
By default, you can resize the width and height of a window. To prevent the window from resizing, you can use the resizable() method:
window.resizable(width,height)
The resizable() method has two parameters that specify whether the width and height of the window can be resizable.
The following shows how to make the window with a fixed size:
import tkinter as tk
root = tk.Tk()
root.title('Tkinter Window Demo')
root.geometry('600x400+50+50')
root.resizable(False, False)
root.mainloop()
When a window is resizable, you can specify the minimum and maximum sizes using the minsize() and maxsize() methods:
window.minsize(min_width, min_height)
window.maxsize(min_height, max_height)
Tkinter allows you to specify the transparency of a window by setting its alpha channel ranging from 0.0 (fully transparent) to 1.0 (fully opaque):
window.attributes('-alpha',0.5)
The following example illustrates a window with 50% transparent:
import tkinter as tk
root = tk.Tk()
root.title('Tkinter Window Demo')
root.geometry('600x400+50+50')
root.resizable(False, False)
root.attributes('-alpha', 0.5)
root.mainloop()
The window stack order refers to the order of windows placed on the screen from bottom to top. The closer window is on the top of the stack and it overlaps the one lower.
To ensure that a window is always at the top of the stacking order, you can use the -topmost attribute like this:
window.attributes('-topmost', 1)
To move a window up or down of the stack, you can use the lift() and lower() methods:
window.lift()
window.lift(another_window)
window.lower()
window.lower(another_window)
The following example places the root window on top of all other windows. In other words, the root window is always on top:
import tkinter as tk
root = tk.Tk()
root.title('Tkinter Window Demo')
root.geometry('300x200+50+50')
root.resizable(0, 0)
root.attributes('-topmost', 1)
root.mainloop()
Tkinter window displays a default icon. To change this default icon, you follow these steps:
root = tk.Tk() root.title(‘Tkinter Window Demo’) root.geometry(‘300x200+50+50’) root.resizable(False, False) root.iconbitmap(‘./assets/pythontutorial.ico’)
root.mainloop()
```
Use the title() method to change the title of the window. Use the geometry() method to change the size and location of the window. Use the resizable() method to specify whether a window can be resizable horizontally or vertically. Use the window.attributes(‘-alpha’,0.5) to set the transparency for the window. Use the window.attributes(‘-topmost’, 1) to make the window always on top. Use lift() and lower() methods to move the window up and down of the window stacking order. Use the iconbitmap() method to change the default icon of the window.