我想从一个列表框中进行 Select ,并使用所选值检索其他数据,这些数据也将插入到另一个列表框中进行 Select .当 Select 第一个列表框时,它成功地工作,没有任何错误.第二个列表框在 Select 第一个时也会更新新列表.当 Select 第二个列表框时,问题就出现了,除了附带的错误异常外,所有预期的一切都正常工作.
from tkinter import *
root= Tk()
books = ['Genesis', 'Exodus', 'Leviticus']
chapter = [1,2,3,4,5]
biblelist = Listbox(root)
biblelist.pack(side=LEFT)
biblelist.insert(END, 0)
chapter = Listbox(root)
chapters.pack(side=LEFT)
def get_selected(e):
global book_selected
index = int(biblelist.curselection()[0])
value=biblelist.get(index)
book_selected = '%s' %(value)
if book_selected == books[0]
for x in chapter:
chapters.insert(END, x)
def get_selected_chapter(event):
ind = int(chapters.curselection()[0])
val=chapters.get(ind)
chapter_selected = '%s' %(val)
print(book_selected + chapter_selected)
biblelist.bind('<<ListboxSelect>>',get_selected)
chapters.bind('<<ListboxSelect>>', get_selected_chapter)
root.mainloop()
代码真正给了我我想要的,但我不舒服的错误.
这就是错误:
File "C:\Users\a\Desktop\emma\PROJECT\index.py", line 685, in get_selected index = int(biblelist.curselection()[0]) ~~~~~~~~~~~~~~~~~~~~~~~~^^^ IndexError: tuple index out of range
我try 了所有的错误,但仍然失败.我真的需要帮助.我也是Python编程的新手.