I have this code, in which pressing each one of three buttons, label2 text should record their state.
from Tkinter import *
class App:
def __init__(self, master):
self.state = [False, False, False]
frame = Frame(master)
frame.pack()
self.label1 = Label(frame, text="buttons", fg="black").grid(row=0)
self.buttonR = Button(frame, text="RED", fg="red", command=self.controlR).grid(row=1, column=0)
self.buttonG = Button(frame, text="GREEN", fg="green", command=self.controlG).grid(row=1, column=1)
self.buttonB = Button(frame, text="BLUE", fg="blue", command=self.controlB).grid(row=1, column=2)
self.label2 = Label(frame, text="results", fg="black").grid(row=2)
def controlR(self):
self.state[0] = not self.state[0]
self.results()
def controlG(self):
self.state[1] = not self.state[1]
self.results()
def controlB(self):
self.state[2] = not self.state[2]
self.results()
def results(self):
color_list = ["RED", "GREEN", "BLUE"]
my_str = 'button '
for i in xrange(len(color_list)):
my_str += color_list[i]
if self.state[i] == False:
my_str += " is OFF \n"
else:
my_str += " is ON \n"
print my_str
print type(self.label2)
self.label2['text'] = my_str
root = Tk()
app = App(root)
root.mainloop()
root.destroy()
What I get is TypeError: 'NoneType' object does not support item assignment
because each one of the five widgets launched in the init definition are not recognized as instances
in the results definition. Thus print type(self.label2)
returns NoneType
.
Why does it happen? Any thoughts would be appreciated.