Come impostare l'icona della finestra in Tkinter
- 
          
            root.iconbitmapper impostare l’icona della finestra
- 
          
            tk.call('wm', 'Iconphoto', )Metodo per impostare l’icona della finestra
- 
          
            root.iconphotoper impostare l’icona della finestra
 
Introdurremo i metodi per impostare l’icona della finestra in Tkinter.
- root.iconbitmapper impostare l’icona della finestra
- root.tk.call()per impostare l’icona della finestra
- root.iconphotoper impostare l’icona della finestra
root.iconbitmap per impostare l’icona della finestra
import tkinter as tk
root = tk.Tk()
root.iconbitmap("/path/to/ico/icon.ico")
root.mainloop()
iconbitmap(bitmap) imposta l’icona del widget finestra/telaio su bitmap. La bitmap deve essere di tipo ico, ma non png o jpg, altrimenti l’immagine non verrà visualizzata come icona.

L’immagine sopra mostra quando il tipo ico è usato in iconbitmap.
Se si usa il tipo png, l’icona mostrata nella finestra sarà vuota,

tk.call('wm', 'Iconphoto', ) Metodo per impostare l’icona della finestra
import tkinter as tk
root = tk.Tk()
root.tk.call("wm", "iconphoto", root._w, tk.PhotoImage(file="/path/to/ico/icon.png"))
root.mainloop()
Il metodo tk.call è l’interfaccia Tkinter per l’interprete tcl. Potremmo eseguire un comando tcl usando questo metodo call.
È utile quando il wrapper di Tkinter non può avere accesso ad alcune funzioni di tcl/tk.
wm comunica con il gestore delle finestre.
Dobbiamo impostare l’immagine come tk.PhotoImage ma non l’immagine stessa, altrimenti avremo un errore _tkinter.TclError.
root.iconphoto per impostare l’icona della finestra
Un altro metodo per impostare l’icona della finestra è quello di usare il metodo root.iconphoto() che accetta più tipi di immagini proprio come in tk.call('wm', 'iconphoto', ).
import tkinter as tk
root = tk.Tk()
root.iconphoto(False, tk.PhotoImage(file="/path/to/ico/icon.png"))
root.mainloop()
Qui, False significa che questa immagine icona si applica solo a questa specifica finestra ma non ai toplevels creati in futuro.
Se si usa True, l’immagine icona viene applicata anche a tutti i toplevels creati in futuro.
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook