Come impostare il colore di sfondo di Tkinter

Jinku Hu 21 dicembre 2022 15 febbraio 2020
  1. Metodo configure(background=)
  2. Attributo bg o background
  3. Codice colore Tkinter
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')

Come impostare Tkinter Background Color - configurare il metodo

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()

Come impostare il colore di sfondo di Tkinter - attributo bg

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()

Come impostare Tkinter Background Color - codice HTML esadecimale

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()

Come impostare Tkinter Background Color - Codice HTML esadecimale

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

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