-2

I've taken a look through the existing questions, but I haven't been able to find a solution so far.

I'm new to the Python programming language and have started playing around with Tk, but keep receiving the following error message when trying to either 'get' a value (from a checkbox) or change a value of a label:

'NoneType' object has no attribute 'getitem'

Below is an example of my code in which I receive the error when clicking a button

from Tkinter import *

the_window = Tk()

the_window.title('Button Change Colour')

def change_to_red():
    colour_area['text']='Red'

colour_area = Label(the_window, bg='Grey', text = 'test', width = 40, height = 5).grid(row = 1, column = 1, padx = 5, pady = 5)
red_button = Button(the_window, text='Red', width = 5, command = change_to_red).grid(row = 2, column = 1)

the_window.mainloop()

I'm sure it's something small/silly, but would appreciate your help nonetheless! :)

1 Answers1

1

It sounds confusing but you had not declared colour_area as a label, you just added it to the grid.
here's your error:

from Tkinter import *

the_window = Tk()

the_window.title('Button Change Colour')

def change_to_red():
    colour_area['text']='Red'

# initializing colour_area as a Tk.Label
colour_area = Label(the_window, bg='Grey', text = 'test', width = 40, height = 5)
# adding it to the grid
colour_area.grid(row = 1, column = 1, padx = 5, pady = 5)
red_button = Button(the_window, text='Red', width = 5, command = change_to_red).grid(row = 2, column = 1)

the_window.mainloop()

This will work properly.

lycuid
  • 2,555
  • 1
  • 18
  • 28
  • The works perfectly! Thank you! So, all of my issues are caused because I try to pack/grid everything in the same go. Makes sense! – Dominic Fichera Sep 23 '16 at 03:59
  • @DominicFichera You can very well do it one go, but *not* if you need to access, configure or get from the widget afterwards. – Jacob Vlijm Sep 23 '16 at 06:42