python - 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
解决方案
推荐阅读
- biztalk - 带有 biztalk 服务器 2016 的 Azure 服务总线
- html - 内容从主容器溢出
- python - 如何通过返回汇总的值来更新字典中的值?
- html - 当我在笔记本电脑(小屏幕)上打开网页时,网页会变形,但在台式电脑(大屏幕)上打开它时仍然可以)
- apache-spark - 使用python将databricks数据帧写入S3
- java - 更正应用程序的类路径,使其包含一个兼容的 javax.persistence.Table 版本
- dynamics-nav - 我们如何在 NAV 2016 中使用 C/AL 执行 CRUD 和 Join 操作?
- python - 我不能在生成器中使用其他人的模型来生成我的输入。我该如何解决?
- python - 比较来自多个数据帧的数据帧以过滤数据并提取相关特征
- sql-server - 更改 sql server 表问题