On 06/08/18 20:50, Ali M wrote:
> If i delete this line "self.textbox.delete(1.0, tk.END)" in my
> enter_meaning function the listbox items won't lower down to 1 item and i
> just want 1 item to be shown after user searches the item in entrybox.

So just display 1 item.
What you are currently doing is displaying all the items and then
deleting them except for the last item. Instead, just display
the last item by using an index of -1. It's a lot less work.

> what i want to do is: when user searches in entrybox, the listbox items
> lowers down to that searched item only, 

But what if more than one item is found?
Or can that absolutely never happen? Are you sure?
Does Esperanto not have the concept of synonyms?

> remaining item, the results from db prints in text widget, i want to be
> able to also use the button to print it. how can i configure the button for
> this? 

It should simply be a case of binding the same function to both the
entry and button. You just need to ensure that you create a function
that stands alone to populate the text box. (I'm assuming that when
you say print you really mean "display on screen" and not "print on
paper"?) In other words your enter_meaning method should be assigned
to both events.

Looking at your code you never seem to bind any commands to the
button, therefore it presumably does nothing.


>     # SQLite
>     def enter_meaning(self, tag):
>         for index in self.listbox.curselection():
>             global esperanto
>             global results
>             esperanto = self.listbox.get(index)
>             results = self.cur.execute("SELECT English FROM Words WHERE
> Esperanto = ?", (esperanto,))
>         for row in results:
>             self.textbox.delete(1.0, tk.END)
>             self.textbox.insert(tk.END, row[0])

Replace the first for loop with

index = self.listbox.curselection()[-1]
esperanto = self.listbox.get(index)
results = self.cur.execute("SELECT English
                            FROM Words
                            WHERE Esperanto = ?", (esperanto,))

And replace the last for loop with

self.textbox.delete(1,0,tk.END)
self.textbox.insert(tk.END, results[-1][0])

It should have the same effect with a lot less processing.
To display the first item use 0 as the index rather
than -1 in the second "loop".

Incidentally, why make esperanto and results global?
You only use them inside this function and they are assigned
new values each time so you may as well make them local.

Also the first loop assumes your user wants you to
search based on the last item selected (if more
than one). Is that correct? Should it perhaps be
the first item? Or all of them?

Or should you configure the listbox so that only one
item at a time can be selected?(ie selectmode=tk.SINGLE)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to