Put an icon in the window of tkinter

You want to put an icon into your app made with Python and tkinter? Read this post and you’ll get to the point.

I got this icon that is a png file. I want to add it to a window of tkinter, having the image in the same directory of the code.

This to me does not work

import tkinter as tk


root = tk.Tk()
root.iconbitmap("calculator.png")

root.mainloop()

I get this result and no error message

So, I tried this

import tkinter as tk


root = tk.Tk()
root.tk.call('wm', 'iconphoto', root._w, tk.PhotoImage(file='calculator.png'))

root.mainloop()

and it worked, cool

import tkinter as tk


root = tk.Tk()
root.tk.call('wm', 'iconphoto', root._w, tk.PhotoImage(file='calculator.png'))

root.mainloop()

This was the result, as you can see I got the image in the top left of the window, as I wanted.

See ya in the next post


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.