怎么实现:点击某个按钮,只改变这个按钮自身的颜色,而其它的按钮不变?
想要的效果:点击某个按钮,只改变这个按钮自身的颜色,而其它的按钮不变。
原代码点击会让整个颜色都改变,想不通怎么定位到只改变某一个。
原代码:
————————————————————————————————————
from tkinter import*
root = Tk()
colors=['black','yellow']
def color():
global a
b=a
a=(b+1)%2
for i in range(n):
bts['bg'] = colors
n=36
a=0
bts=[]
for i in range(n):
button1=Button(root,text=i,command=color)
bts.append(button1)
for i in range(n):
bts.grid(row=int((i/6)%6),column=i%6)
root.mainloop()
本帖最后由 xiaosi4081 于 2020-5-12 19:57 编辑
点击0试试看
from tkinter import*
root = Tk()
i=0
colors=['black','yellow']
def one():
for t in range(n):
t=t+1
button1=Button(root,text=t)
bts.append(button1)
for t in range(n):
bts.grid(row=int((t/6)%6),column=t%6)
def color():
global a
b=a
a=(b+1)%2
bts['bg'] = colors
def color1():
global a
b=a
a=(b+1)%2
bts['bg'] = colors
n=36
a=0
bts=[]
button1=Button(root,text="0",command=color)
bts.append(button1)
bts.grid(row=int((0/6)%6),column=0%6)
one()
xiaosi4081 发表于 2020-5-12 19:28
内容你自己去改
页:
[1]