如何修复单词列表的for循环?

问题描述 投票:0回答:1

我使用 Tkinter 创建了一个弹出式窗口,可以显示列表中的所有单词。我使用了for循环,这样我就可以将所有的单词插入到我设置的Listbox中。出现的问题是,for循环把列表中的单词反过来,每个索引插入一个字母。我希望每一个索引都有一个单词以不颠倒的顺序插入。

编码

for words in list:
        index = 0

        if words != 0:
            index += 1
            listbox.insert(index, words)

输出(选中列表框内部):

list for-loop tkinter listbox python-3.8
1个回答
2
投票

首先,你应该避免使用关键字 list 作为变量。 例如,将其改为: wordlist 代替。 另外,你应该在列表框的末尾插入单词,而不是用 index:

for word in wordlist:  # better rename list to wordlist
    listbox.insert('end', word) # insert at the end of listbox instead
© www.soinside.com 2019 - 2024. All rights reserved.