2013-05-30 50 views
0

一直在用这个撞墙撞我的头。刚刚拿到Tkinter的绳索,跟着一个教程来获得基础知识,现在正在努力实现我自己的东西。我为我正在做的一些工作创建了一个查询界面。我在屏幕上有三个列表框,并且我需要从按钮单击中的所有三个选择中选择,以便可以生成查询并显示一些数据。Tkinter listbox获取属性错误

我收到的错误似乎说它看不到mapLBox,表明范围问题。如果我将代码更改为诸如print self.mapLBox.get(Tkinter.ACTIVE)之类的简单代码,它仍会抛出相同的属性错误。所有框和滚动条都正确绘制到屏幕上,并且错误的线条(#90)注释掉了,它运行良好。

有两个类,simpleApp_tkPasteBin),以下所有代码都属于,dbtools在数据库上运行查询并返回结果。

错误:

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1473, in __call__ 
     return self.func(*args) 
    File "test.py", line 90, in OnButtonClick 
     self.labelVar.set(self.mapLBox.get(self.mapLBox.curselection()[0])) 
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1829, in __getattr__ 
     return getattr(self.tk, attr) 
AttributeError: mapLBox 

里面我initialise方法(从__init__运行)的列表和按钮创建:

button = Tkinter.Button(self,text=u"Click Me",command=self.OnButtonClick) 
button.grid(column=1,row=0) 

# Make a scrollbar for the maps list 
scrollbar2 = Tkinter.Scrollbar(self,orient=Tkinter.VERTICAL) 
scrollbar2.grid(column=2,row=2,sticky='EW') 

# Create list of maps 
mapLBox = Tkinter.Listbox(self,selectmode=Tkinter.SINGLE,exportselection=0, yscrollcommand=scrollbar2.set) 
scrollbar2.config(command=mapLBox.yview) 
mapLBox.grid(column=2,row=2,sticky='EW') 

# Populate map list 
nameList = self.db.getMapNameList() 
IDList = self.db.getMapIDList() 
for count, name in enumerate(nameList): 
    nameFormat = str(IDList[count][0])+': '+name[0] 
     mapLBox.insert(Tkinter.END,nameFormat) 

self.grid_columnconfigure(0,weight=1) # Allow resizing of window 
self.resizable(True,True) # Contrain to only horizontal 
self.update() 
self.geometry(self.geometry()) 

OnButtonClick方法连接到我的按钮:

def OnButtonClick(self): 
    self.labelVar.set(self.mapLBox.get(self.mapLBox.curselection()[0])) 
    return 

回答

1

您正在访问self.mapLBox但您并未定义self.mapLBox。仅仅因为你创建了名为mapLBox的变量并不意味着它会自动成为对象的一个​​属性。

您需要更改此:

mapLBox = Tkinter.Listbox(...) 

...这样的:

self.mapLBox = Tkinter.Listbox(...) 

...,当然下,更改引用mapLBox其他地方。

+0

一个明显的,我怎么错过了!谢谢! – Tomassino