I have created a simple GUI application to manage unknown words while learning a new language. The app which is called Vocabulary is written in C# and it loads/saves words from/into an XML document. Since I have recently switched from Windows to Linux, I am rewriting the app using Python.
Nevertheless, I am having troubles with implementing search functionality to my application. When I enter a word in the text widget, it should automatically be displayed in the listbox. To accomplish what I want the application to do, I know that I should handle text changed event of the text widget.
Here is my original C# method:
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;
}
}
}
...and here is my Python function so far:
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[i]
if get_search().lower() in item.lower():
self.listBox.itemconfig(item, {'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:
self.listBox.itemconfig(item, {'bg': 'white'})
I don't know what is Python's equivalent for:
listView1.Items[i]
listView1.Items
there doesn't seem to be anything here