首页 > 解决方案 > Python - Tkinter.Listbox <> 不工作

问题描述

我的程序中有 2 个列表框,我正在尝试获取当前选定的项目。在 setsList 中一切正常,但对于 appsList 却不行。我不知道为什么。谁能帮我?

以下是获取所选项目的功能:

def selectSet(evt):
    print("Called Select Set")
    global activeSetApps
    global activeSet
    activeSetApps.clear()
    activeSet = str(setsList.get(setsList.curselection()))
    dir = "sets/"+str(activeSet)+".txt"
    f = open(dir, "r")
    activeSetApps = f.read().split("\n")
    printApps()
    f.close()
def appSelect(evt):
    print("Called Select App")
    global activeSetApps
    global activeApp
    activeApp = str(appsList.get(appsList.curselection()))
    print(activeApp)

这是列表框定义

setsList = tk.Listbox(appSetList)
appsList = tk.Listbox(appsInSet)
setsList.bind('<<ListboxSelect>>', selectSet)
appsList.bind('<<ListboxSelect>>', appSelect)
setsList.place(relwidth=1, relheight=0.9, rely=0.1)
appsList.place(relwidth=1, relheight=0.9, rely=0.1)

当我运行我的代码并从 setsList 中选择一些东西时,它工作正常并打印:

Called Select Set

但是当我从 appsList 中选择一些东西时,它会打印:

Called Select App
Ehh.PNG
Called Select Set
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\mateu\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "F:\Git\App-runner\app-runner.py", line 37, in selectSet
    activeSet = str(setsList.get(setsList.curselection()))
  File "C:\Users\mateu\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2798, in get
    return self.tk.call(self._w, 'get', first)
_tkinter.TclError: bad listbox index "": must be active, anchor, end, @x,y, or a number

标签: pythonpython-3.xtkinterlistbox

解决方案


推荐阅读