Search code examples
pythontkintericonsttkbootstrap

How to set a window icon once in Tkinter for all dialogs including messagebox on Windows?


On Linux using Tkinter and TtkBootstrap, you can set up a logo once with:

logo = tb.PhotoImage(file="images/favicon.png")
root.iconphoto(True, logo)

This ensures that the logo is shown on every Toplevel and in messagebox.showerror/info/warning dialogs.

However, on Windows, you have to use .iconbitmap('images/favicon.ico') for each Toplevel. Unfortunately, the icon does not appear in messagebox.showerror/info/warning dialogs,and it only appears the Tkinter logo or the Ttkbootstrap logo.

Is there any function in Tkinter where you can define the logo once on Windows, and more importantly, ensure that the logo appears in messagebox.showerror/info/warning dialogs?

I tried to find something by asking ChatGPT, but it doesn't know and there aren't any questions about that on the internet.


Solution

  • .iconphoto(True, logo) should also work on Windows.

    Below is an example:

    import tkinter as tk
    from tkinter import messagebox
    import ttkbootstrap as tb
    
    def show_dialog():
        messagebox.showinfo("Hello", "Hello World")
    
    def show_window():
        win = tb.Toplevel()
        win.geometry("300x100")
        win.title("Secondary Window")
    
    root = tk.Tk()
    logo = tb.PhotoImage(file="lenna.png")
    root.iconphoto(True, logo)
    
    tk.Button(root, text="Window", command=show_window).pack(padx=100, pady=30)
    tk.Button(root, text="Dialog", command=show_dialog).pack(padx=100, pady=30)
    
    root.mainloop()
    

    Result:

    enter image description here enter image description here enter image description here