Hi, I've written this code which i send below. now i have a listview, and when i click each item in my listview, i want it's related word to be fetched from db and shown in my textview. now it fetches all the data :(. look at the picture, for example when i click on abako in Esperanto field, i want the abacus to be shown in my textview, and so on for the other items in the list. i'm a beginner thanks a lot.

import sqlite3 as sqlite
import tkinter as tk
from tkinter import Text
from tkinter import Entry
from tkinter import Scrollbar
from tkinter import ttk

#GUI Widgets

class EsperantoDict:
    def __init__(self, master):

        master.title("EsperantoDict")
        master.resizable(False, False)
        master.configure(background='#EAFFCD')

        self.style = ttk.Style()
        self.style.configure("TFrame", background='#EAFFCD')
        self.style.configure("TButton", background='#EAFFCD')
        self.style.configure("TLabel", background='#EAFFCD')

        self.frame_header = ttk.Frame(master, relief=tk.FLAT)
        self.frame_header.pack(side=tk.TOP, padx=5, pady=5)

        self.logo = tk.PhotoImage(file=r'C:\EsperantoDict\eo.png')
        self.small_logo = self.logo.subsample(10, 10)

        ttk.Label(self.frame_header, image=self.small_logo).grid(row=0, column=0, stick="ne", padx=5, pady=5, rowspan=2)
        ttk.Label(self.frame_header, text='EsperantoDict', font=('Arial', 18, 'bold')).grid(row=0, column=1)

        self.frame_content = ttk.Frame(master)
        self.frame_content.pack()

        self.entry_search = ttk.Entry(self.frame_content)
        self.entry_search.grid(row=0, column=0)
        self.entry_search.insert(tk.END, "Type to Search")
        self.entry_search.bind('<Button-1>', self.entry_delete)

        self.button_search = ttk.Button(self.frame_content, text="Search")
        self.aks = tk.PhotoImage(file=r'C:\EsperantoDict\search.png')
        self.small_aks = self.aks.subsample(3, 3)
        self.button_search.config(image=self.small_aks, compound=tk.LEFT)
        self.button_search.grid(row=0, column=1, columnspan=2)

        self.listbox = tk.Listbox(self.frame_content, height=28)
        self.listbox.grid(row=1, column=0)
        self.scrollbar = ttk.Scrollbar(self.frame_content, orient=tk.VERTICAL, command=self.listbox.yview)
        self.scrollbar.grid(row=1, column=1, sticky='ns')
        self.listbox.config(yscrollcommand=self.scrollbar.set)
        self.listbox.bind('<<ListboxSelect>>', self.enter_meaning)

        self.textbox = tk.Text(self.frame_content, width=60, height=27)
        self.textbox.grid(row=1, column=2)

    # SQLite
        self.db = sqlite.connect(r'C:\EsperantoDict\test.db')
        self.cur = self.db.cursor()
        self.cur.execute('SELECT Esperanto FROM Words')
        for row in self.cur:
            self.listbox.insert(tk.END, row)
    # SQLite

    def enter_meaning(self, tag):
        if self.listbox.curselection():
            results = self.cur.execute("SELECT English FROM Words")
            for row in results:
                self.textbox.insert(tk.END, row)

    def entry_delete(self, tag):
        self.entry_search.delete(0, tk.END)
        return None

def main():
    root = tk.Tk()
    esperantodict = EsperantoDict(root)
    root.mainloop()

if __name__ == '__main__': main()

#db tbl name: Words
##db first field name: Esperanto
##db second field name: English

Recommended Answers

All 5 Replies

Of course it fetches all the words. That's what you are telling it to do. In order to restrict what gets returned you have to provide a WHERE clause as in

SELECT <fieldname> FROM ,tablename> WHERE <fieldname> <condition> <fieldvalue>

For example

SELECT FirstName, LastName FROM Employees WHERE EmployeeID = 221078

If you do that you get one record. If you leave out the WHERE clause you get records for all employees.

I use this and it gives me the first row, i want to know how to increament it automatically so it returns the corresponding number of row. like when i click on the first word on list, it gives the first row, and when on second, it gives the second row, and so on...

"SELECT English FROM Words WHERE rowID = 1"

How do you know that each row corresponds to the same numbered record? In your situation you should be selecting the record that contains that word. It should be set up so that the table is indexed by the word. An example structure would be

Table Words
    Esperanto
    English

SELECT English FROM Words WHERE Esperanto = 'elephanto'

Words could also be set up with a secondary index on English to facilitate a reverse lookup.

I think because the database generates an automatic rowid, so they will be returned in order don't they? or i should first sort them first? how can i set a secondary index on English?

 def enter_meaning(self, tag):
        if self.listbox.curselection():
            results = self.cur.execute("SELECT English FROM Words order by rowID")
            for row in results:
                self.textbox.insert(tk.END, row)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.