鱼C论坛

 找回密码
 立即注册
查看: 1270|回复: 2

[已解决]怎么实现:点击某个按钮,只改变这个按钮自身的颜色,而其它的按钮不变?

[复制链接]
发表于 2020-5-12 15:00:32 | 显示全部楼层 |阅读模式
2鱼币
想要的效果:
点击某个按钮,只改变这个按钮自身的颜色,而其它的按钮不变。


原代码点击会让整个颜色都改变,想不通怎么定位到只改变某一个。

原代码:
————————————————————————————————————
from tkinter import*

root = Tk()

colors=['black','yellow']

def color():
    global a
    b=a
    a=(b+1)%2
    for i in range(n):
        bts[i]['bg'] = colors[a]
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[i].grid(row=int((i/6)%6),column=i%6)


root.mainloop()


最佳答案
2020-5-12 15:00:33
本帖最后由 xiaosi4081 于 2020-5-12 19:57 编辑

点击0试试看
  1. from tkinter import*

  2. root = Tk()
  3. i=0
  4. colors=['black','yellow']
  5. def one():
  6.    for t in range(n):
  7.        t=t+1
  8.        button1=Button(root,text=t)            
  9.        bts.append(button1)
  10.    for t in range(n):
  11.        bts[t].grid(row=int((t/6)%6),column=t%6)
  12. def color():
  13.     global a
  14.     b=a
  15.     a=(b+1)%2

  16.     bts[i]['bg'] = colors[a]
  17. def color1():
  18.     global a
  19.     b=a
  20.     a=(b+1)%2
  21.    
  22.     bts[i]['bg'] = colors[a]
  23. n=36
  24. a=0
  25. bts=[]
  26. button1=Button(root,text="0",command=color)
  27. bts.append(button1)

  28. bts[0].grid(row=int((0/6)%6),column=0%6)
  29. one()

复制代码

最佳答案

查看完整内容

点击0试试看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-12 15:00:33 | 显示全部楼层    本楼为最佳答案   
本帖最后由 xiaosi4081 于 2020-5-12 19:57 编辑

点击0试试看
  1. from tkinter import*

  2. root = Tk()
  3. i=0
  4. colors=['black','yellow']
  5. def one():
  6.    for t in range(n):
  7.        t=t+1
  8.        button1=Button(root,text=t)            
  9.        bts.append(button1)
  10.    for t in range(n):
  11.        bts[t].grid(row=int((t/6)%6),column=t%6)
  12. def color():
  13.     global a
  14.     b=a
  15.     a=(b+1)%2

  16.     bts[i]['bg'] = colors[a]
  17. def color1():
  18.     global a
  19.     b=a
  20.     a=(b+1)%2
  21.    
  22.     bts[i]['bg'] = colors[a]
  23. n=36
  24. a=0
  25. bts=[]
  26. button1=Button(root,text="0",command=color)
  27. bts.append(button1)

  28. bts[0].grid(row=int((0/6)%6),column=0%6)
  29. one()

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-12 19:42:29 | 显示全部楼层

内容你自己去改
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-19 07:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表