Tkinter:'int'对象不支持项目分配

时间:2022-03-31 00:32:26

I have quite a problem. Some time ago I merged two codes into this one, I got rid of the problem earlier and now after merging them It's come back, I really don't know what to do now, so I'll appreciate every help I can get. I get this annoying error message:

我有一个很大的问题。前段时间我把两个代码合并到了这个代码中,我先把它解决了问题,现在合并它们之后又回来了,我真的不知道现在该做什么,所以我会感激我能得到的每一个帮助。我收到这个恼人的错误消息:

TypeError: 'int' object does not support item assignment

TypeError:'int'对象不支持项目分配

I don't understand how could it appear just now after I got rid of it.

我不明白在我摆脱它之后它是怎么出现的。

import tkinter as tk

NUM_BUTTONS = 2
button_list = [1, 2]
label_list = [1, 2]
label_text_list = [1, 2]


class Demo1:

    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.button1 = tk.Button(text='Menu', command=self.new_window)
        self.button1.grid(row=3, column=1)
        self.button2 = tk.Button(text='Quit', command=self.close_windows)
        self.button2.grid(row=3, column=3)
        self.label1 = tk.Label(text='Controller')
        self.label1.grid(row=1, column=2)

    def new_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = Demo2(self.newWindow)

    def close_windows(self):
        self.master.destroy()


class Demo2:

    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)

        for i in range(NUM_BUTTONS):

            def wrap(button_num=i):
                toggle_text(button_num)

            button = tk.Button(
                self.frame, text='WL', width=12, bg='red', command=wrap)
            button.grid(row=2, column=i)
            button_list.append(button)

            label = tk.Label(
                self.frame, text=label_text_list[i], width=12, bg='red')
            label.grid(row=1, column=i)
            label_list.append(label)
            self.frame.pack()

            def toggle_text(button_num):
                self.button = button_list[button_num]
                label = label_list[button_num]

                if button['text'] == 'WL':
                    button['text'] = 'WYL'
                    label['bg'] = 'green'

                else:
                    button['text'] = 'WL'
                    label['bg'] = 'red'

    def close_windows(self):
        self.master.destroy()


def main():
    root = tk.Tk()
    app = Demo1(root)
    root.mainloop()

if __name__ == '__main__':
    main()

1 个解决方案

#1


label is an integer

label是一个整数

label = label_list[button_num] is using the label_list from the top label_list = [1, 2]

label = label_list [button_num]正在使用顶部label_list = [1,2]中的label_list

you are then trying to set an attribute on that

然后你试图在那上面设置一个属性

 2["color"] = "green" 

clearly does not work :)

显然不起作用:)

you can fix it by changing the line at the top to

您可以通过更改顶部的行来修复它

label_list = []

#1


label is an integer

label是一个整数

label = label_list[button_num] is using the label_list from the top label_list = [1, 2]

label = label_list [button_num]正在使用顶部label_list = [1,2]中的label_list

you are then trying to set an attribute on that

然后你试图在那上面设置一个属性

 2["color"] = "green" 

clearly does not work :)

显然不起作用:)

you can fix it by changing the line at the top to

您可以通过更改顶部的行来修复它

label_list = []