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

时间:2013-03-02 12:02:53

标签: button tkinter label configure

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

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'

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

我愿意打赌你正在创建这样的小部件:

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

当您执行foo().bar()之类的操作时,结果就是最后一个函数返回的内容。在你的case包中(或者可能是grid)是要调用的最后一个函数,它总是返回None。因此,您的列表只包含None s。