Change Icon for Tkinter MessageBox
We know many modules and one of them is Tkinter. Tkinter is a module which is a standard interface of Python to the Tk GUI toolkit. This interface Tk and the Tkinter modules, both of them are available on most of the Unix platforms. it is also available on Windows OS and many others. But it is usually a shared library or DLL file, for some cases, it is statically linked with the Python interpreter.
Default icon of MessageBox
Whenever we create a message box using the Tkinter, we always see a similar icon in the message box. Let us see it again with an example.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
In the above example, within the circle that is the default icon we get, when it is not mentioned in the code. And yes, we can change it according to our wishes. Select the photo you want to keep it as an icon. Select and then [Right click–> Properties]. Type of File you see will be in the format of (.png).
But we need it in the format of (.ico). There might be a question that why we need to convert
.ico? Why can’t we use it in the format of .png? For that, the answer will be the icon bitmap function (depending on the programming language) should be used to set a bitmap image to the window when the window is iconified. For that just search online for online icoconverter, go there, and simply convert the image you want to convert it to (.ico). So we got our image ready to the desired format, next get back to the coding part and let’s learn how to change the default icon to our selected icon follow the steps.
Step 1: Add a line, defining the icon bitmap i.e. win.iconbitmap(r”)
import tkinter as to win = tk.Tk() win.title("example") win.iconbitmap(r'') win.mainloop()
Step 2: Mentioning the file path of the image we want as an icon. Copy the File location and paste it inside “win.iconbitmap(r”)”.
Step 3: Mention the file name. Copy the file name and paste it after “\” where you mentioned the file location.
Finally, we get the complete code for how we can change the icon. Let’s put it together.