Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
Setting the title bar icon in a Tkinter window (whether it's the main window or any top-level window) is a common practice to brand your application or to provide a visual indication of the application's purpose.
Here's a step-by-step tutorial on how to set the titlebar icon of any Tkinter or Toplevel window:
You'll need an icon file. The preferred format for icons in Tkinter is .ico
for Windows. On Linux, you can use .png
or .gif
.
import tkinter as tk
root = tk.Tk() root.title("Icon Example")
root.iconbitmap('path_to_icon.ico') # Replace 'path_to_icon.ico' with your icon file's path.
If you're creating additional windows using the Toplevel
widget, you can set their icons in a similar manner:
top = tk.Toplevel(root) top.title("Toplevel Icon Example") top.iconbitmap('path_to_icon.ico')
Combining everything, here's a simple example:
import tkinter as tk root = tk.Tk() root.title("Main Window") root.geometry("300x200") root.iconbitmap('path_to_icon.ico') def open_toplevel(): top = tk.Toplevel(root) top.title("Toplevel Window") top.geometry("300x200") top.iconbitmap('path_to_icon.ico') tk.Button(root, text="Open Toplevel", command=open_toplevel).pack(pady=20) root.mainloop()
When you run this code, you'll have a main window with a button. Clicking the button will open a top-level window. Both windows will have the same icon set in their title bars.
path_to_icon.ico
in the example) is in the correct path or provide the full path to the file..ico
files, try using a .png
or .gif
format icon and see if that resolves the problem.Python Tkinter set window icon example:
import tkinter as tk from tkinter import PhotoImage root = tk.Tk() root.title("Tkinter Window Icon Example") # Replace 'path/to/icon.png' with the path to your icon image file icon_path = 'path/to/icon.png' icon_image = PhotoImage(file=icon_path) root.iconphoto(True, icon_image) root.mainloop()
Set icon for Tkinter Toplevel window:
import tkinter as tk from tkinter import PhotoImage def open_toplevel_window(): toplevel = tk.Toplevel(root) toplevel.title("Toplevel Window") # Replace 'path/to/icon.png' with the path to your icon image file icon_path = 'path/to/icon.png' icon_image = PhotoImage(file=icon_path) toplevel.iconphoto(True, icon_image) root = tk.Tk() root.title("Set Icon for Toplevel Window Example") open_button = tk.Button(root, text="Open Toplevel Window", command=open_toplevel_window) open_button.pack(pady=20) # Replace 'path/to/icon.png' with the path to your icon image file icon_path = 'path/to/icon.png' icon_image = PhotoImage(file=icon_path) root.iconphoto(True, icon_image) root.mainloop()
Set favicon for Tkinter window:
import tkinter as tk from tkinter import PhotoImage root = tk.Tk() root.title("Set Favicon for Tkinter Window Example") # Replace 'path/to/favicon.ico' with the path to your favicon file favicon_path = 'path/to/favicon.ico' favicon_image = PhotoImage(file=favicon_path) root.tk.call('wm', 'iconphoto', root._w, favicon_image) root.mainloop()