Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- import sqlite3
- from tkinter import messagebox
- import csv
- fp = 'words.csv' # the path to the excel file
- def csv_to_db(csv_file, conn, cur):
- """insert the csv data into the database"""
- data = csv.reader(open(fp, encoding='utf-8'))
- for d in data:
- datos = (d[0].strip(), d[1].strip(), None)
- cur.execute("""insert into DICTION values (?, ?, ?)""", datos)
- conn.commit()
- root = tk.Tk()
- root.geometry("400x500")
- root.resizable(width=True, height= True)
- root.title("Dic")
- root.configure(background='white')
- # creating a connection object to insert excel data
- conn= sqlite3.connect('GIS_DICTION.db')
- cur= conn.cursor()
- # This should be excecuted only once if you don't have the database with the csv data in it.
- csv_to_db(csv_file=fp, conn=conn, cur=cur)
- ########### DEF FOR SEARCH #######
- def search():
- try:
- conn= sqlite3.connect('GIS_DICTION.db')
- cur= conn.cursor()
- #######################################
- cur.execute("""create table if not exists DICTION (Words text, Meaning text, Image text)""")
- ######################################
- tex.delete(1.0,"end")
- data= v.get()
- cur.execute("SELECT Meaning, Image FROM DICTION WHERE Words= ?", (data,)) # I'm not calling .lower()
- var= cur.fetchone()
- # Checking if there is some value in stored in var
- if var:
- tex.insert("end", var[0]) # accessing the meaning
- #####################################
- img_path = var[1] # accessing the path to the image
- image_object = tk.PhotoImage(file=img_path) # here I'm not using PIL, but tkinter PhotoImage
- ImageLabel.config(image=image_object)
- ImageLabel.image = image_object # keep a reference to the image
- ######################################
- except Exception as e: # This makes no difference, it works the same
- messagebox.showinfo("Dic","Word not found")
- def refresh():
- entry.delete(0, "end")
- tex.delete(1.0, "end")
- ########## GUI DESIGN ##############
- v= tk.StringVar()
- entry= tk.Entry(root, width=20,font= ("Bahnschrift SemiLight",11), bg= "#FFFFFf",bd=2, textvariable= v)
- entry.place(x=50, y= 25)
- Butt= tk.Button(root, width=10, height= 1,command= search, text= "Search", bg= "#666699",fg="white",bd=0,font=("rockwell", 10))
- Butt.place(x=220, y=25)
- Refr= tk.Button(root, width=10, height= 1,command= refresh, text= "Refresh", bg= "#666699",fg="white",bd=0,font=("rockwell", 10))
- Refr.place(x=300, y=25)
- tex=tk.Text(root, font= ("Bahnschrift SemiLight",12),bg= "#999ccc",bd= 0,width=35,height= 10)
- tex.place(x=50,y=55)
- frame= tk.Frame(root, bg= "white")
- frame.place(x=40, y= 250)
- ImageLabel= tk.Label(frame,font=("calibra", 9), bg= "white")
- ImageLabel.pack(expand=True, fill= tk.BOTH,padx= 65, pady=20)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement