python - Getting a 'bad listbox index' error when trying to return a value from cursor selection in tkinter listbox
问题描述
Trying to return the value from the cursor selection for a listbox with tkinter, but I'm getting an error:
Traceback (most recent call last):
File "C:/Users/Rachel/PycharmProjects/Final2100/FE1.py", line 36, in <module>
entvariable1.set(nations[listbox.get(listbox.curselection())]["cont"])
File "C:\Users\Rachel\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
I can't figure out why since I was using listbox.get(listbox.curselection()) just fine earlier in a similar situation.
import pickle
from tkinter import *
window = Tk()
def getDictionary(fileName):
infile = open(fileName, 'rb')
nations = pickle.load(infile)
infile.close()
return nations
nations = getDictionary("UNdict.dat")
lbnations = StringVar()
listbox = Listbox(window, width = 20, listvariable = lbnations)
listbox.grid(padx = 5, pady = 5, row = 0, column = 0, rowspan = 4, sticky = NSEW)
lbnations.set(tuple(nations))
label1 = Label(window, text = "Continent:")
label1.grid(padx = 5, pady = 5, row = 0, column = 2, sticky = E)
label2 = Label(window, text = "Population:")
label2.grid(padx = 5, pady = 5, row = 1, column = 2, sticky = E)
label3 = Label(window, text = "Area (sq. miles):")
label3.grid(padx = 5, pady = 5, row = 2, column = 2, sticky = E)
entvariable1 = StringVar()
entvariable2 = StringVar()
entvariable3 = StringVar()
contentry = Entry(window, state = "readonly", textvariable = entvariable1, width = 13)
contentry.grid(padx = 5, pady = 5, row = 0, column = 1, sticky = W)
popentry = Entry(window, state = "readonly", textvariable = entvariable2, width = 13)
popentry.grid(padx = 5, pady = 5, row = 1, column = 1, sticky = W)
areaentry = Entry(window, state = "readonly", textvariable = entvariable3, width = 13)
areaentry.grid(padx = 5, pady = 5, row = 2, column = 1, sticky = W)
##country = listbox.get(listbox.curselection())
entvariable1.set(nations[listbox.get(listbox.curselection())]["cont"])
entvariable2.set(nations[listbox.get(listbox.curselection())]["popl"])
entvariable3.set(nations[listbox.get(listbox.curselection())]["area"])
window.mainloop()
I need the entry boxes to produce the dictionary values in the .dat file, the key being the country name which is what I'm trying to get the value of from the cursor selection in the listbox.
解决方案
You are calling listbox.curselection()
just a few milliseconds after creating the listbox. Nothing is selected because the user hasn't had a chance and you're not programmatically selecting anything, so your code throws an error.
推荐阅读
- python - 多列中 groupby 和 sum 的 Python 问题
- c++ - 如何将一对迭代器转换为视图?
- qt - 未找到 Qt5Nfc - CMake
- flutter - 下拉列表中的复选框颤动
- javascript - 仅获取一次图像并多次显示
- html - 如何配置 Bootstrap textarea 元素以垂直填充父 div?
- database - Xamarin 应用程序中的 Microsoft.Data.SqlClient 或 System.Data.SqlClient 中的 PlatformNotSupportedException
- nginx - 如何在 VPS 上部署两个 pyhton/flask 应用程序,重定向到每个应用程序
- jwt - RabbitMQ + OAuth => 用户的 MQTT 登录失败
- github - 如何(或在哪里)构建(或下载)hg19.map 文件