尝试使用OptionMenu信息中的信息循环列表当我尝试循环时出现错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:\Users\Dominic\Desktop\Homework creator\The accuall program.py", line 79, in create_button
selecting_qs()
File "C:\Users\Dominic\Desktop\Homework creator\The accuall program.py", line 88, in selecting_qs
if dif == main_list[n][1]:
TypeError: list indices must be integers, not list
我的列表由数字和字母组成,所以我不确定我的选项菜单是否必须是intiger或字符串才能搜索列表中的特定部分。 这是代码:
def selecting_qs():
main_list = main_file_info()
dif = difficulty.get()
for n in main_list:
if dif == main_list[n][1]:
print(main_list[n][1])
#delating empty strings and /n from list
def rm(list_):
return [x for x in list_ if x != '\n' and x != ' ']
#main list order:
#Question ID , Difficulty , Topic , Answer ID , Picture ID , Frequency
def main_file_info():
main_list=[]
main_file = open("main_file.txt","r")
main_file_info=main_file.readlines()
for line in main_file_info:
main_list.append(rm(line.split("@")))
main_file.close()
return(main_list)
答案 0 :(得分:0)
您将获得带有 for main_list 中的n的项目列表,并将结果用作同一列表中的索引。如果列表中的项目是字符串或列表,则会出现错误。尝试使用以下代码:
def selecting_qs():
main_list = main_file_info()
dif = difficulty.get()
resp = []
for n in main_list:
if dif == n[1]:
resp.append(n[1])
return resp