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. Example:
Python3
import tkinter as tk win = tk.Tk() # as it does not have any mentions # of the icon we get a default icon. win.title("example") win.mainloop() |
Output : 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 .png to .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 iconconverter, 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”)”.
win.iconbitmap(r'C:\Users\Madhusudan\Downloads\')
Step 3: Mention the file name. Copy the file name and paste it after “\” where you mentioned the file location.
win.iconbitmap(r'C:\Users\Madhusudan\Downloads\favicon(2).ico')
Finally, we get the complete code for how we can change the icon. Let’s put it together.
Python3
import tkinter as tk win = tk.Tk() win.title("example") win.iconbitmap(r 'C:\Users\Madhusudan\Downloads\favicon(2).ico' ) win.mainloop() |
Output :