Python 改变按钮的颜色

时间:2020-12-19 10:14:50

标签: python tkinter button colors

当我按下其中一个按钮时,颜色总是适用于最后一个按钮。你可以帮帮我吗。谢谢^^

import tkinter as tk
from tkinter import ttk
from tkinter import colorchooser

tabStyle = tk.Tk()
tabStyle['background'] = "black"
texts = ['Background', 'Foreground', 'Cursor' , '...']
labels = []
buttons = []

def AskColor(c):
    color = colorchooser.askcolor()[1]
    buttons[c]['background'] = color

for c, s in enumerate(texts):
    labels.append(tk.Label(tabStyle, text=s, fg="white", bg="black"))
    buttons.append(tk.Button(tabStyle, width=5, bg="white", command=lambda : AskColor(c)))

for ((l, label), (b, button)) in zip(enumerate(labels), enumerate(buttons)):
    label.grid(column=0, row=l, sticky="W")
    button.grid(column=1, row=b, padx=2)

tabStyle.mainloop()

0 个答案:

没有答案