Python ';非类型';对象没有属性'__getitem';-编程

Python ';非类型';对象没有属性'__getitem';-编程,python,tkinter,tk,Python,Tkinter,Tk,我已经查看了现有的问题,但到目前为止还没有找到解决方案 我是Python编程语言的新手,已经开始使用Tk,但在尝试“获取”值(从复选框)或更改标签值时,会不断收到以下错误消息: “非类型”对象没有属性“getitem” 下面是我的代码示例,我在其中单击按钮时收到错误 from Tkinter import * the_window = Tk() the_window.title('Button Change Colour') def change_to_red(): colour_

我已经查看了现有的问题,但到目前为止还没有找到解决方案

我是Python编程语言的新手,已经开始使用Tk,但在尝试“获取”值(从复选框)或更改标签值时,会不断收到以下错误消息:

“非类型”对象没有属性“getitem

下面是我的代码示例,我在其中单击按钮时收到错误

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()

我相信这是件小事/愚蠢的事,但还是会感谢你的帮助!:)

听起来很混乱,但您没有将
颜色区域声明为标签,您只是将其添加到网格中。
以下是您的错误:

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()

这将正常工作。

这台机器工作正常!非常感谢。所以,我所有的问题都是因为我试图在同一个go中打包/网格化所有内容。有道理@DominicFichera您可以一次完成,但如果您需要访问、配置或以后从小部件获取,则不能这样做。@AndrewL。是的,你当然是对的,^^这是一个可以链接到的规范。