[Tutor] Getting current listbox item index and bind it to button (Tkinter)

Alan Gauld alan.gauld at yahoo.co.uk
Sat Aug 4 08:14:58 EDT 2018


On 04/08/18 09:07, Ali M wrote:
> I have taken the delete statement out of the for loop, but still nothing
> happens when i click the button. 

You never assign the search_word function to a widget either
via the command option or via a bind statement.

If you don't bind the function to a widget it will never
be called.

> i want that button to take the current
> list item index and do what the curselection does (which is printing the
> result in text widget).

I'm not sure what you mean. curselection returns a list of
indexes of the items currently selected. It doesn't display
anything. Even the code where you use it (enter_meaning)
only uses it to fetch the Esperanto strings for use in the SQL.

Incidentally, you have the same delete bug in that function too.
If you have more than one item selected it will only show
the last item.



> class EsperantoDict:
>     def __init__(self, master):
...
>         self.search_var = tk.StringVar()
>         self.search_var.trace("w", lambda name, index, mode:
> self.update_list())
> 
...
>         self.entry_search = ttk.Entry(self.frame_content,
> textvariable=self.search_var, width=30)
>         self.entry_search.bind('<FocusIn>', self.entry_delete)
>         self.entry_search.bind('<FocusOut>', self.entry_insert)
>         self.entry_search.grid(row=0, column=0, padx=5)
>         self.entry_search.focus()
>         self.entry_search.bind("<KeyRelease>", self.edit_input)
> 
>         self.button_search = ttk.Button(self.frame_content, text=u"Serĉu")
>         self.photo_search =
> tk.PhotoImage(file=r'C:\EsperantoDict\search.png')
>         self.small_photo_search = self.photo_search.subsample(3, 3)
>         self.button_search.config(image=self.small_photo_search,
> compound=tk.LEFT, style="TButton")
>         self.button_search.grid(row=0, column=2, columnspan=1, sticky='nw',
> padx=5)
>         self.button_search.bind('<Return>', self.enter_meaning)

Is that the function you intended to put there?

>         self.listbox = tk.Listbox(self.frame_content, height=30, width=30)
>         self.listbox.grid(row=1, column=0, padx=5)
>         self.scrollbar = ttk.Scrollbar(self.frame_content,
> orient=tk.VERTICAL, command=self.listbox.yview)
>         self.scrollbar.grid(row=1, column=1, sticky='nsw')
>         self.listbox.config(yscrollcommand=self.scrollbar.set)
>         self.listbox.bind('<<ListboxSelect>>', self.enter_meaning)
> 
>         self.textbox = tk.Text(self.frame_content, relief=tk.GROOVE,
> width=60, height=30, borderwidth=2)
>         self.textbox.config(wrap='word')
>         self.textbox.grid(row=1, column=2, sticky='w', padx=5)
> 
>         # SQLite
>         self.db = sqlite.connect(r'C:\EsperantoDict\test.db')
>         self.cur = self.db.cursor()
>         self.cur.execute("SELECT Esperanto FROM Words ORDER BY Esperanto")
>         for row in self.cur:
>             self.listbox.insert(tk.END, row)
>             self.update_list()
> 
...
>     # SQLite
>     def enter_meaning(self, tag):
>         for index in self.listbox.curselection():
>             esperanto = self.listbox.get(index)
>             global results
>             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])

Note the delete inside the loop.

...
>     def search_word(self, tag):
>         esperanto = self.listbox.selection_set(0)
>         results = self.cur.execute("SELECT English FROM Words WHERE
> Esperanto = ?", (esperanto,))
>         self.textbox.delete(1.0, tk.END)
>         for row in results:
>             self.textbox.insert(tk.END, row[0])

-- 
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




More information about the Tutor mailing list