Python Tkinter列表框获取属性错误
用这个把我的头撞到墙上。刚刚掌握了Tkinter的诀窍,跟随教程学习了基本知识,现在正在努力实现我自己的东西。我正在为我正在做的一些工作创建一个查询接口。我在屏幕上有三个列表框,我需要在点击按钮时从所有三个列表框中获取选择,以便生成查询并显示一些数据 我收到的错误似乎表明它看不到Python Tkinter列表框获取属性错误,python,tkinter,Python,Tkinter,用这个把我的头撞到墙上。刚刚掌握了Tkinter的诀窍,跟随教程学习了基本知识,现在正在努力实现我自己的东西。我正在为我正在做的一些工作创建一个查询接口。我在屏幕上有三个列表框,我需要在点击按钮时从所有三个列表框中获取选择,以便生成查询并显示一些数据 我收到的错误似乎表明它看不到mapLBox,这表明存在范围问题。如果我将代码更改为类似print self.mapLBox.get(Tkinter.ACTIVE)这样的简单代码,它仍然会抛出相同的属性错误。所有框和滚动条都正确地绘制到屏幕上,并且注
mapLBox
,这表明存在范围问题。如果我将代码更改为类似print self.mapLBox.get(Tkinter.ACTIVE)
这样的简单代码,它仍然会抛出相同的属性错误。所有框和滚动条都正确地绘制到屏幕上,并且注释掉了错误的行(#90),运行正常
有两个类,simpleApp_tk
(),下面的所有代码都属于这两个类,以及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
在我的初始化
方法中(从\uuuuu init\uuuuu
运行),创建了列表和按钮:
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())
onbutton单击“我的按钮”附带的方法:
def OnButtonClick(self):
self.labelVar.set(self.mapLBox.get(self.mapLBox.curselection()[0]))
return
您正在访问self.mapLBox
,但没有定义self.mapLBox
。创建名为mapLBox
的变量并不意味着它会自动成为对象的属性
您需要更改以下内容:
mapLBox = Tkinter.Listbox(...)
。。。为此:
self.mapLBox = Tkinter.Listbox(...)
。。。当然,还可以更改引用mapLBox
的其他位置 很明显,我怎么会错过呢!谢谢