1 | initial version |
The most effective way to automatically adjust the size of icons in Tkinter is to use the tkinter.PhotoImage
method to create the icon, then set its subsample
or zoom
value to a number that scales the image to the desired size.
For example, if the desired icon size is 50x50 pixels and the original image size is 100x100 pixels, use the subsample
method with a value of 2 to scale the image down to the desired size:
from tkinter import *
root = Tk()
icon = PhotoImage(file="my_icon.png")
icon = icon.subsample(2) # Scale down to 50x50 pixels
root.iconphoto(True, icon)
root.mainloop()
Alternatively, use the zoom
method to scale the image up or down to the desired size:
from tkinter import *
root = Tk()
icon = PhotoImage(file="my_icon.png")
icon = icon.zoom(2) # Scale up to 200x200 pixels
icon = icon.subsample(4) # Scale back down to 50x50 pixels
root.iconphoto(True, icon)
root.mainloop()