2017-04-16 9 views
0

私は、新しい言語を習得しながら未知語を管理するためのシンプルなGUIアプリケーションを作成しています。このアプリケーションはVocabularyと呼ばれ、XML文書との間で単語をロード/セーブします。それでも、リストに単語を追加したい場合は、その単語が既にリストに存在するかどうかを確認するセキュリティチェックを行いたいと思います。ここでアイテムが既にリストに登録されているかどうかを確認するには?

は、私がこれまで持っているものです。

def add_item(self): 

     if len(self.listBox.curselection()) > 0: 
      messagebox.showinfo('Notification', 'Please make sure you have no words selected!') 

     elif self.txt_WordOrPhrase['state'] == 'disabled': 
      messagebox.showinfo('Notification', 'Please make sure you refresh fields first!') 
     else: 
      if len(self.txt_WordOrPhrase.get("1.0", "end-1c")) == 0: 
       messagebox.showinfo('Notification', 'Please enter the word!') 
      else: 
       w = Word(self.get_word(), self.get_explanation(), self.get_translation(), self.get_example()) 
       if self.words: # check if an item already exists 
        self.words.append(w) 
        self.listBox.insert(END, w.wordorphrase) 
       else: 
        messagebox.showinfo('Notification', 'That word already exists in your vocabulary!') 

       self.clear_all() 
       self.sync() 
       self.word_count() 

     self.save_all() 

C#では、私はこのようにそれを行うだろう。ここでは

if (words.Find(x => x.WordOrPhrase == w.WordOrPhrase) == null) { 
    words.Add(w); 
    listView1.Items.Add(w.WordOrPhrase); 
} 
+0

この音は、 [この投稿]と非常によく似ています(http://stackoverflow.com/questions/43397908/removing-selected-items-from-the-listbox-and-from-the-list/43411309#43411309)。これは学校の授業ですか? – pstatix

答えて

0

全体のソリューションです:

def add_item(self): 

     if len(self.listBox.curselection()) > 0: 
      messagebox.showinfo('Notification', 'Please make sure you have no words selected!') 

     elif self.txt_WordOrPhrase['state'] == 'disabled': 
      messagebox.showinfo('Notification', 'Please make sure you refresh fields first!') 
     else: 
      if len(self.txt_WordOrPhrase.get("1.0", "end-1c")) == 0: 
       messagebox.showinfo('Notification', 'Please enter the word!') 
      else: 
       w = Word(self.get_word(), self.get_explanation(), self.get_translation(), self.get_example()) 
       if not any(x for x in self.words if x.wordorphrase == w.wordorphrase): 
        self.words.append(w) 
        self.listBox.insert(END, w.wordorphrase) 
       else: 
        messagebox.showinfo('Notification', 'That word already exists in your vocabulary!') 

       self.clear_all() 
       self.sync() 
       self.word_count() 

     self.save_all() 
関連する問題