列表框被视为Nonetype,为什么?

时间:2013-12-16 04:09:45

标签: python listbox tkinter nonetype

self.__tracksListBox = Listbox(self.__master, width=100, height=25).grid(row=0, column=1,
                                                                           rowspan=4)

我正在尝试在列表框中显示选项,但会引发以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
    return self.func(*args)
  File "C:\Users\aawiesinger5000\Desktop\CS 110 Final Project\layoutGUI.py", line 213, in showTracks
    self.__tracksListBox.insert(END, titleStr)
AttributeError: 'NoneType' object has no attribute 'insert'

当我尝试使用这样的列表框时:

if tracks:
            titleStr = ""
            for track in tracks:
                titleStr += track + "\n"
            self.__tracksListBox.insert(END, titleStr)

有谁知道为什么?

谢谢!

1 个答案:

答案 0 :(得分:5)

grid方法不返回;隐式返回None

将以下陈述分为两个陈述:

self.__tracksListBox = Listbox(self.__master, width=100, height=25).grid(row=0, column=1,
                                                                           rowspan=4)

像:

self.__tracksListBox = Listbox(self.__master, width=100, height=25)
self.__tracksListBox.grid(row=0, column=1, rowspan=4)