-1

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

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'

感谢帮助

4

1 回答 1

2

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

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

当您执行类似foo().bar()的操作时,结果是最后一个函数返回的任何内容。在您的情况下,包(或者可能是网格)是最后一个被调用的函数,它总是返回None. 因此,您的列表只包含Nones。

于 2013-03-02T13:55:32.713 回答