| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
- from tkinter import  *
 
 - import time
 
 - root=Tk()
 
 - root.title("定时刷新颜色")
 
 - abc=["red","green","blue","indigo","yellow"]
 
 - def refresh():
 
 -       for i in abc :
 
 -             l=Label(root,text="COLOR",foreground=abc)
 
 -             root.after(1000,refresh)
 
 -             l.pack()
 
 - root.after(1000, refresh)
 
 - mainloop()
 
  复制代码 
 
错  误  在  哪  里  ?
错误在于在刷新函数中,创建了多个Label,并且没有正确设置Label的前景色属性。下面是修正后的代码: 
 
- from tkinter import *
 
 - import time
 
  
- root = Tk()
 
 - root.title("定时刷新颜色")
 
 - abc = ["red", "green", "blue", "indigo", "yellow"]
 
 - label = Label(root, text="COLOR")
 
 - label.pack()
 
  
- def refresh(index=0):
 
 -     label.config(foreground=abc[index])
 
 -     index = (index + 1) % len(abc)
 
 -     root.after(1000, refresh, index)
 
  
- root.after(1000, refresh)
 
 - mainloop()
 
 
  复制代码 
 
修正后的代码中,我们只创建了一个Label,并在刷新函数中使用 label.config(foreground=abc[index]) 来设置Label的前景色属性。通过使用 index 变量来迭代颜色列表,我们可以在每次刷新时更新Label的前景色。 
 
 
 |   
 
 
 
 |