Tkinter 列表框获取属性错误 [英] Tkinter listbox get attribute error

查看:28
本文介绍了Tkinter 列表框获取属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Been banging my head against a wall with this one. Just getting the ropes of Tkinter, followed a tutorial to get the basics, now working forward to implement my own stuff. I creating a query interface for some work I'm doing. I have three list boxes on screen, and I need to get the selections from all three on button click, so I can generate a query and display some data.

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

The error I'm receiving seems to say it can't see mapLBox, indicating a scope problem. If I change the code to something simple like print self.mapLBox.get(Tkinter.ACTIVE) it will still throw the same attribute error. All boxes and scrollbars draw correctly to the screen, and with the erroneous line (#90) commented out, it runs fine.

有两个类,simpleApp_tk(PasteBin),下面的所有代码属于和 dbtools 对数据库运行查询并返回结果.

There are two classes, simpleApp_tk (PasteBin), which all the below code belongs to, and dbtools which runs queries on the DB and returns results.

错误:

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__ 运行)中,创建了列表和按钮:

Inside my initialise method (run from __init__) the lists and button are created:

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 方法:

The OnButtonClick method attached to my button:

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

推荐答案

您正在访问 self.mapLBox,但没有定义 self.mapLBox.仅仅因为您创建名为 mapLBox 的变量并不意味着它会自动成为对象的属性.

You are accessing self.mapLBox but you aren't defining self.mapLBox. Just because you create variable named mapLBox doesn't mean it automatically becomes an attribute of the object.

你需要改变这个:

mapLBox = Tkinter.Listbox(...)

...到这个:

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

... 当然,还要更改引用 mapLBox 的其他位置.

... and, of course, change the other places where you reference mapLBox.

这篇关于Tkinter 列表框获取属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆