Reputation: 7
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
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()
Upvotes: 0
Views: 1081
Reputation: 113930
label
is an integer
label = label_list[button_num]
is using the label_list
from the top label_list = [1, 2]
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 = []
Upvotes: 2