Advertisement
EmaSMach

Some fixes

Apr 3rd, 2020
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. import tkinter as tk
  2. import sqlite3
  3. from tkinter import messagebox
  4. import csv
  5.  
  6.  
  7. fp = 'words.csv' # the path to the excel file
  8. def csv_to_db(csv_file, conn, cur):
  9.     """insert the csv data into the database"""
  10.     data = csv.reader(open(fp, encoding='utf-8'))
  11.     for d in data:
  12.         datos = (d[0].strip(), d[1].strip(), None)
  13.         cur.execute("""insert into DICTION values (?, ?, ?)""", datos)
  14.         conn.commit()
  15.  
  16.  
  17. root = tk.Tk()
  18. root.geometry("400x500")
  19. root.resizable(width=True, height= True)
  20. root.title("Dic")
  21. root.configure(background='white')
  22.  
  23. # creating a connection object to insert excel data
  24. conn= sqlite3.connect('GIS_DICTION.db')
  25. cur= conn.cursor()
  26. # This should be excecuted only once if you don't have the database with the csv data in it.
  27. csv_to_db(csv_file=fp, conn=conn, cur=cur)
  28. ########### DEF FOR SEARCH #######
  29. def search():
  30.     try:
  31.         conn= sqlite3.connect('GIS_DICTION.db')
  32.         cur= conn.cursor()
  33.         #######################################
  34.         cur.execute("""create table if not exists DICTION (Words text, Meaning text, Image text)""")
  35.         ######################################
  36.         tex.delete(1.0,"end")
  37.         data= v.get()
  38.         cur.execute("SELECT Meaning, Image FROM DICTION WHERE Words= ?", (data,)) # I'm not calling .lower()
  39.         var= cur.fetchone()
  40.         # Checking if there is some value in stored in var
  41.         if var:
  42.             tex.insert("end", var[0])  # accessing the meaning
  43.         #####################################
  44.         img_path = var[1]                            # accessing the path to the image
  45.         image_object = tk.PhotoImage(file=img_path)  # here I'm not using PIL, but tkinter PhotoImage
  46.         ImageLabel.config(image=image_object)
  47.         ImageLabel.image = image_object              # keep a reference to the image
  48.     ######################################
  49.     except Exception as e: # This makes no difference, it works the same
  50.         messagebox.showinfo("Dic","Word not found")
  51.  
  52. def refresh():
  53.     entry.delete(0, "end")
  54.     tex.delete(1.0, "end")
  55.  
  56.  
  57.  
  58. ########## GUI DESIGN ##############
  59. v= tk.StringVar()
  60. entry= tk.Entry(root, width=20,font= ("Bahnschrift SemiLight",11), bg= "#FFFFFf",bd=2, textvariable= v)
  61. entry.place(x=50, y= 25)
  62.  
  63. Butt= tk.Button(root, width=10, height= 1,command= search, text= "Search", bg= "#666699",fg="white",bd=0,font=("rockwell", 10))
  64. Butt.place(x=220, y=25)
  65. Refr= tk.Button(root, width=10, height= 1,command= refresh, text= "Refresh", bg= "#666699",fg="white",bd=0,font=("rockwell", 10))
  66. Refr.place(x=300, y=25)
  67. tex=tk.Text(root, font= ("Bahnschrift SemiLight",12),bg= "#999ccc",bd= 0,width=35,height= 10)
  68. tex.place(x=50,y=55)
  69. frame= tk.Frame(root,  bg= "white")
  70. frame.place(x=40, y= 250)
  71. ImageLabel= tk.Label(frame,font=("calibra", 9),  bg= "white")
  72. ImageLabel.pack(expand=True, fill= tk.BOTH,padx= 65, pady=20)
  73.  
  74.  
  75.  
  76. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement