如何設定 Tkinter 背景色
Jinku Hu
2023年1月30日

Tkinter 控制元件類中的方法 configure
和屬性 bg
或 backgroud
可用於設定 Tkinter 控制元件/視窗背景顏色。
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()
這裡,
app.configure(bg='red')
將 app
的顏色設定成 red
。你也可以用 background
來替代 bg
。
app.configure(background='red')
bg
或 background
屬性
background
或 bg
是大多數 Tkinter 控制元件中的一個屬性,可用於直接設定背景顏色。
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()
作者: Jinku Hu
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