2016-11-30 119 views
0

從從OptionMenu infoWhen信息試圖環路周圍列表我嘗試循環方式試圖環路周圍列表我得到一個錯誤:從從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

您與獲取項目列表的n個main_list和使用結果作爲在同一列表中的索引。如果列表中的項目是字符串或列表,則會出現錯誤。嘗試使用下面的代碼:

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 
+0

謝謝你這麼多,有沒有可能在匹配DIFF被放在一個單獨的清單列表中每個對象? –

+0

是的。只需在你的函數內創建一個列表,如果等於dif,最後返回值,最後返回創建的列表。 – daniboy000