2016-09-23 112 views
-2

我已經看了一下現有的問題,但到目前爲止我還沒有找到解決方案。'NoneType'對象沒有屬性'__getitem__' - Tkinter

我是新來的Python編程語言,並且已經開始使用Tk進行遊戲,但在試圖從「複選框」中獲取一個值或更改標籤值時,仍然收到以下錯誤消息:

「NoneType」對象有沒有屬性「的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() 

時收到錯誤代碼的我一個例子,我敢肯定,這一些小/愚蠢的東西,但會感激你的幫助! :)

+0

@AndrewL。是的,你當然是對的,^^這是正確的鏈接。 –

回答

1

這聽起來令人困惑,但您還沒有宣佈colour_area作爲標籤,您只是將其添加到網格中。
這裏是你的錯誤:

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

這將正常工作。

+0

完美的作品!謝謝!所以,我所有的問題都是因爲我試圖在同一個行程中打包/格式化所有問題。說得通! –

+0

@DominicFichera你可以很好地做到這一點,但*不*如果你需要訪問,配置或從小部件後獲得。 –

相關問題