在Tkinter上更改按钮和标签的颜色可以通过以下步骤实现:
import tkinter as tk
window = tk.Tk()
button = tk.Button(window, text="按钮")
label = tk.Label(window, text="标签")
button.config(bg="red", fg="white")
label.config(bg="blue", fg="white")
window.mainloop()
完整的示例代码如下:
import tkinter as tk
def change_color():
button.config(bg="green", fg="white")
label.config(bg="yellow", fg="black")
window = tk.Tk()
button = tk.Button(window, text="按钮", command=change_color)
label = tk.Label(window, text="标签")
button.pack()
label.pack()
window.mainloop()
这个示例代码创建了一个窗口,包含一个按钮和一个标签。当点击按钮时,按钮和标签的颜色会被更改为绿色背景、白色文本和黄色背景、黑色文本。
领取专属 10元无门槛券
手把手带您无忧上云