【发布时间】:2013-05-30 10:13:28
【问题描述】:
一直用这个把我的头撞到墙上。刚刚获得 Tkinter 的基本知识,按照教程获得基础知识,现在正在努力实现我自己的东西。我为我正在做的一些工作创建了一个查询界面。我在屏幕上有三个列表框,我需要在单击按钮时从所有三个列表框中获取选择,这样我就可以生成查询并显示一些数据。
我收到的错误似乎是说它看不到mapLBox,这表明存在范围问题。如果我将代码更改为像print self.mapLBox.get(Tkinter.ACTIVE) 这样的简单代码,它仍然会抛出相同的属性错误。所有的框和滚动条都正确地绘制到屏幕上,并且注释掉了错误的行(#90),它运行良好。
有两个类,simpleApp_tk (PasteBin),下面所有代码都属于它们,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
【问题讨论】: