Come impostare il colore di sfondo di Tkinter
Il metodo della classe del widget Tkinter configure e l’attributo bg o backgroud potrebbero essere usati per impostare il colore di sfondo del widget/finestra Tkinter.
Metodo configure(background=)
try:
import Tkinter as tk
except:
import tkinter as tk
app = tk.Tk()
app.title("configure method")
app.geometry("300x200")
app.configure(bg="red")
app.mainloop()
Qui,
app.configure(bg='red') configura il colore di sfondo di app come red. Si può anche usare background invece di bg.
app.configure(background="red")

Attributo bg o background
background o bg è un attributo nella maggior parte dei widget di Tkinter, e potrebbe essere usato per impostare direttamente il colore di sfondo.
try:
import Tkinter as tk
except:
import tkinter as tk
app = tk.Tk()
app.title("bg attribute")
app.geometry("300x200")
app["bg"] = "blue"
app.mainloop()

Codice colore Tkinter
Si può specificare il colore con i nomi noti come red, blue o green come mostrato sopra, e si può anche specificare tramite RGB come codici colore esadecimali in HTML, ad esempio, #49A o #0059b3.
try:
import Tkinter as tk
except:
import tkinter as tk
app = tk.Tk()
app.title("bg attribute")
app.geometry("300x200")
app["bg"] = "#0059b3"
app.mainloop()

try:
import Tkinter as tk
except:
import tkinter as tk
app = tk.Tk()
app.title("bg attribute")
app.geometry("300x200")
app["bg"] = "#49A"
app.mainloop()

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