python - 列表框中的重复单词

标签 python list tkinter listbox

我正在创建一个简单的 GUI 应用程序来在学习新语言时管理未知单词。问题是,当我输入单词,重新启动应用程序,然后输入另一个单词并再次重新启动应用程序时 - 我输入的第一个单词本身会加倍。重复单词的数量随着输入单词的数量而增加。

Vocabulary

这是我所做的:

# Vocabulary.py
# GUI program to manage unknown words

from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import xml.etree.ElementTree as ET
import os


class Word:

    def __init__(self, wordorphrase, explanation, translation, example):
        self.wordorphrase = wordorphrase
        self.explanation = explanation
        self.example = example
        self.translation = translation

class Vocabulary(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.master = master
        self.master.resizable(width = False, height = False)
        self.master.title("Vocabulary")
        self.create_widgets()
        self.words = []
        self.load_words()

    def create_widgets(self):

        self.buttons_frame = Frame(self.master)
        self.buttons_frame.grid(row = 10, sticky = W)

        self.search_frame = Frame(self.master)
        self.search_frame.grid(row = 1, sticky = W, columnspan = 2)

        self.comboBox = ttk.Combobox(self.search_frame,
                                     width = 3)
        self.comboBox.grid(row = 0, column = 14, sticky = W)
        self.comboBox['values'] = ( 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' )

        self.btn_Add = Button(self.buttons_frame,
                              text = 'Add',
                              command = self.add_item)
        self.btn_Add.grid(row = 0, sticky = W)

        self.btn_Remove = Button(self.buttons_frame,
                                 text = 'Remove',
                                 command = self.remove_item)

        self.btn_Remove.grid(row = 0, column = 1, sticky = W)

        self.btn_Edit = Button(self.buttons_frame,
                               text = 'Edit',
                               command = self.edit_item)
        self.btn_Edit.grid(row = 0, column = 2, sticky = W)

        self.btn_Save = Button(self.buttons_frame,
                               text = 'Save',
                               command = self.save_item)
        self.btn_Save.grid(row = 0, column = 3, sticky = W)

        self.btn_Refresh = Button(self.buttons_frame,
                                  text = 'Refresh',
                                  command = self.refresh_all)
        self.btn_Refresh.grid(row = 0, column = 4, sticky = W)

        self.lblSearch = Label(self.search_frame, text = 'SEARCH: ')
        self.lblSearch.grid(row = 0, column = 5, sticky = W)

        self.txt_Search = Text(self.search_frame,
                               height = 1,
                               width = 70)
        self.txt_Search.grid(row = 0, column = 6, columnspan = 3, sticky = W)

        self.lblWordsOrPhrases = Label(self.master, text = 'WORDS/PHRASES:')
        self.lblWordsOrPhrases.grid(row = 2, column = 0)

        self.lblWordOrPhrase = Label(self.master, text = 'Word or phrase:')
        self.lblWordOrPhrase.grid(row = 2, column = 1, sticky = W)

        self.listBox = Listbox(self.master,
                               selectmode='multiple',
                               height = 34,
                               width = 38)
        self.listBox.grid(row = 3, column = 0, rowspan = 7, sticky = W)

        self.txt_WordOrPhrase = Text(self.master,
                                     height = 1,
                                     width = 40)
        self.txt_WordOrPhrase.grid(row = 3, column = 1, sticky = N)

        self.lblExplanation = Label(self.master, text = 'Explanation:')
        self.lblExplanation.grid(row = 4, column = 1, sticky = W)

        self.txt_Explanation = Text(self.master,
                                    height = 10,
                                    width = 40)
        self.txt_Explanation.grid(row = 5, column = 1, sticky = N)

        self.lblTranslation = Label(self.master, text = 'Translation:')
        self.lblTranslation.grid(row = 6, column = 1, sticky = W)

        self.txt_Translation = Text(self.master,
                                    height = 10,
                                    width = 40)
        self.txt_Translation.grid(row = 7, column = 1, sticky = N)

        self.lblExamples = Label(self.master, text = 'Example(s):')
        self.lblExamples.grid(row = 8, column = 1, sticky = W)

        self.txt_Example = Text(self.master,
                                height = 10,
                                width = 40)
        self.txt_Example.grid(row = 9, column = 1, sticky = S)

    def get_word(self):
        return self.txt_WordOrPhrase.get('1.0', '1.0 lineend')

    def get_explanation(self):
        return self.txt_Explanation.get('1.0', '1.0 lineend')

    def get_translation(self):
        return self.txt_Translation.get('1.0', '1.0 lineend')

    def get_example(self):
        return self.txt_Example.get('1.0', '1.0 lineend')

    def add_item(self):

        w = Word(self.get_word(), self.get_explanation(), self.get_translation(), self.get_example())

        self.words.append(w)

        self.listBox.insert(END, w.wordorphrase)

        self.clear_all()

        self.save_all()

    def remove_item(self):
        self.listBox.delete(ACTIVE)

    def edit_item(self):
        pass

    def save_item(self):
        pass

    def load_words(self):

        self.listBox.delete(0, END)
        self.words.clear()

        path = os.path.expanduser('~/Desktop')
        vocabulary = os.path.join(path, 'Vocabulary', 'Words.xml')

        if not os.path.exists(vocabulary):
            if not os.path.exists(os.path.dirname(vocabulary)):
                os.mkdir(os.path.dirname(vocabulary))
            doc = ET.Element('Words')
            tree = ET.ElementTree(doc)
            tree.write(vocabulary)
        else:
            tree = ET.ElementTree(file = vocabulary)

        for node in tree.findall('Word'):
            w = Word(node.find('Word').text, node.find('Explanation').text, node.find('Translation').text, node.find('Examples').text)

            self.words.append(w)
            self.listBox.insert(END, w.wordorphrase)

    def save_all(self):

        path = os.path.expanduser('~/Desktop')
        vocabulary = os.path.join(path, 'Vocabulary', 'Words.xml')

        tree = ET.ElementTree(file=vocabulary)

        for xNode in tree.findall('Words'):
            tree.remove(xNode)

        for w in self.words:
            xTop = ET.Element('Word')
            xWord = ET.Element('Word')
            xExplanation = ET.Element('Explanation')
            xTranslation = ET.Element('Translation')
            xExamples = ET.Element('Examples')

            xWord.text = w.wordorphrase
            xExplanation.text = w.explanation
            xTranslation.text = w.translation
            xExamples.text = w.example

            xTop.append(xWord)
            xTop.append(xExplanation)
            xTop.append(xTranslation)
            xTop.append(xExamples)

            tree.getroot().append(xTop)

        tree.write(vocabulary)


    def clear_all(self):
        self.txt_WordOrPhrase.delete('1.0', END)
        self.txt_Explanation.delete('1.0', END)
        self.txt_Translation.delete('1.0', END)
        self.txt_Example.delete('1.0', END)

    def refresh_all(self):
        pass

    def on_closing(self):
        self.save_all
        if messagebox.askokcancel("Quit", "Do you want to quit?"):
            self.master.destroy()

def main():
    root = Tk()
    gui = Vocabulary(root)
    root.protocol('WM_DELETE_WINDOW', gui.on_closing)
    root.mainloop()

if __name__ == '__main__':
    main()

最佳答案

您的问题不在于列表框中,而在于您在 save_all(self) 中以 XML 形式保存单词的方式。要正确重置树,您应该替换:

for xNode in tree.findall('Words'):
    tree.remove(xNode)

for xNode in tree.getroot().findall('Word'):
    tree.getroot().remove(xNode)

关于python - 列表框中的重复单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43392585/

相关文章:

python - Tkinter 按钮在我的 Mac 上不显示文本,尽管代码在其他计算机上有效

Python for 循环在第一次迭代后返回零

c# - 后台 worker - C#

python - 如何提高拆分列表的速度?

python - 循环遍历列表时获取下一个元素

Python TkInter - 为不确定数量的图像分配标签

python - 在 tkinter 中交互式验证条目小部件内容

python - 我的代码从迭代器获取数据多少次?

python - 使用 Django 和 MySQL 存储和查找大型 DNA 微阵列结果

python - 我想在我的电脑启动时运行我的程序