2017-04-18 7 views
1

新しい言語を習得しながら未知語を管理する簡単なGUIアプリケーションを作成しました。 Vocabularyと呼ばれるアプリはC#で書かれており、XML文書から単語をロード/保存します。私は最近WindowsからLinuxに切り替えたので、Pythonを使用してアプリケーションを書き直しています。アプリケーションに自動検索機能を実装するにはどうすればよいですか?

しかし、自分のアプリケーションに検索機能を実装する際に問題が発生しています。テキストウィジェットに単語を入力すると、自動的にリストボックスに表示されます。私がアプリケーションにしたいことを達成するために、テキストウィジェットのテキスト変更イベントを処理する必要があることを知っています。ここで

は私の元のC#メソッドです:

private void txt_Search_TextChanged(object sender, EventArgs e) 
{ 
    if (txt_Search.Text != "") 
    { 
     for (int i = listView1.Items.Count - 1; i >= 0; i--) 
     { 
      var item = listView1.Items[i]; 
      if (item.Text.ToLower().Contains(txt_Search.Text.ToLower())) 
      { 
       item.BackColor = SystemColors.Highlight; 
       item.ForeColor = SystemColors.HighlightText; 
      } 
      else 
      { 
       listView1.Items.Remove(item); 
      } 
     } 
     if (listView1.SelectedItems.Count == 1) 
     { 
      listView1.Focus(); 
     } 
    } 
    else 
    { 
     LoadWords(); 
     RefreshAll(); 
     foreach (ListViewItem item in listView1.Items) 
     { 
      item.BackColor = SystemColors.Window; 
      item.ForeColor = SystemColors.WindowText; 
     } 
    } 
} 

...と、ここで、これまでの私のPythonの関数である:

def txt_Search_text_changed(self, event = None): 

    if self.get_search() != None: 

     i = self.listBox.size() - 1 

     for x in range(i, 0, -1): 
      item = self.listBox.get(0, "end")[i] 
      if self.get_search().lower() in item.lower(): 
       self.listBox.itemconfig(i, {'bg': 'red'}) 

      else: 
       self.listBox.delete(item) 

     if len(self.listBox.curselection()) == 1: 
      self.listBox.focus() 

    else: 
     self.load_words() 
     self.refresh_all() 
     for item in self.listBox.get(0, "end"): 
      self.listBox.itemconfig(i, {'bg': 'white'}) 

私はのためのPythonの同等であるかわからない:

listView1.Items[i]; listView1.Items

+0

あなたが「自動的にリストボックスに表示される」と言うとき、あなたはそれが自動的に追加されるべきであることを意味しています既存のリストボックス?または、単語がすでにリストボックスにあり、検索機能が単にリストボックスを検索して、検索された単語を強調表示する必要があると言っていますか? –

+0

リストボックスを検索し、検索した単語を強調表示するだけです。 –

+0

'self.listBox.get(0、" end ")[i]'を実行する理由はありません。ドキュメントを読むと、 'self.listBox.get(i)'がうまくいくことがわかります。 –

答えて

0

リストボックスのすべてのアイテムを簡単にlistBox.get(0, "end")で取得できます。そのリストを使って、通常のpythonメソッドを使って検索を行うことができます。

ここで値が変わる検索を行うには、変数のトレースを使用する例です:

import tkinter as tk 

class Example(tk.Frame): 
    def __init__(self, parent): 
     tk.Frame.__init__(self, parent) 

     self.search_var = tk.StringVar() 
     self.entry = tk.Entry(self, textvariable=self.search_var) 
     self.listbox = tk.Listbox(self) 
     self.entry.pack(side="top", fill="x") 
     self.listbox.pack(side="left",fill="both", expand=True) 

     self.search_var.trace("w", self.autosearch) 

     self.listbox.insert("end", "four", "fourteen", "fourty-four", "four hundred") 

    def autosearch(self, *args): 
     search_string = self.entry.get().lower() 
     values = [item.lower() for item in self.listbox.get(0, "end")] 

     for index in range(len(values)): 
      # reset background to the default 
      self.listbox.itemconfigure(index, background="") 

     if len(search_string) > 0: 
      for index, item in enumerate(values): 
       if search_string in item: 
        self.listbox.itemconfigure(index, background="pink") 

root = tk.Tk() 
Example(root).pack(fill="both", expand=True) 
root.mainloop() 
+0

ブライアン、ありがとう。どうもありがとうございました。 –

+0

私のコードにあなたの例を実装しようとしました。すべて正常に動作しますが、アイテムを検索した後、すべてのアイテムは色付きです。どうすれば問題を解決できますか? –

+0

@popokatepetl:私はあなたのコードを見ることができないので、私が私の例でやったことをやり直して色をリセットする以外に、何を伝えるべきか分かりません。 –

関連する問題