TKinter如何一次配置所有标签和按钮

问题描述:

现在,我正在编写一个代码,用于在按下按钮时更改所有按钮和标签的颜色。 但我每次都会得到不同的错误。TKinter如何一次配置所有标签和按钮

windows = [] 
global windows 
buttons = [] 
global buttons 
labels = [] 
global labels 

一串代码...然后...

def flavor(c): 
    if c != 0: 
     c = 0 
     for w in windows: 
      w.config(bg = 'black') 
     for l in labels: 
      l.config(bg = 'black', fg = 'white') 
     for b in buttons: 
      b.config(bg = 'black', fg = 'white') 
    elif c != 1: 
     c = 1 
     for w in windows: 
      w.config(bg = 'dark green') 
     for l in labels: 
      l.config(bg = 'dark green', fg = 'light green') 
     for b in buttons: 
      b.config(bg = 'dark green', fg = 'light green') 

我几乎所有的时间,大部分时间的错误:

Traceback (most recent call last): 
File "C:\Users\Ahmet\Desktop\An interesting experiment\FOBBY.py", line 153, in <module> 
main() 
File "C:\Users\Ahmet\Desktop\An interesting experiment\FOBBY.py", line 118, in main 
thememenu.add_command(label="Plain",command = flavor(0)) 
File "C:\Users\Ahmet\Desktop\An interesting experiment\FOBBY.py", line 79, in flavor 
l.config(bg = 'dark green', fg = 'light green') 
AttributeError: 'NoneType' object has no attribute 'config' 

感谢您的帮助

+0

你是如何实例化这些按钮,标签以及如何将它们放入容器的? – 2013-03-02 13:10:35

我敢打赌,你正在创建这样的小部件:

l = Label(,,,).pack(...) 
labels.append(l) 

当您执行类似foo().bar()的操作时,结果是上一次函数返回的结果。在你的案例包(或者网格)中是最后一个被调用的函数,它总是返回None。因此,您的列表除None之外不包含任何内容。