如何设置 Tkinter 背景色
Jinku Hu
2023年1月30日
2019年12月1日
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()
Author: 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