Advertisement
Najeebsk

DICTIONARY.pyw

Oct 23rd, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.72 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import ttk
  3. import sqlite3
  4. import tkinter.font
  5. import sys
  6. import os
  7. import subprocess
  8.  
  9.  
  10. #if you want to use it as a real .exe file please remove exe()'s #
  11. def exe():
  12.     subprocess.check_call([sys.executable, '-m','pip','install','pyinstaller'])
  13.     reqs= subprocess.check_output([sys.executable, '-m','pip','freeze'])
  14.     installed_packages = [r.decode().split('==')[0] for r in reqs.split() ]
  15.     print(installed_packages)
  16.     os.system("pyinstaller --onefile -w dictionary.py")
  17.  
  18. #exe()
  19.  
  20.  
  21. #Making root configurations
  22. root = Tk()
  23. root.title("NAJEEB LEARN VACOBULARY")
  24. root.iconbitmap('dict.ico')
  25. app_height =550
  26. app_width =500
  27. root.geometry(f'{app_width}x{app_height}+{400}+{100}')
  28. root.resizable(False, False)
  29. root.config(background ="light gray")
  30.  
  31. #Connection to the our sqlite database
  32. conn = sqlite3.connect('user_dict.db')
  33. c = conn.cursor()
  34.  
  35. #We are creating here our table in db ,if is exist alreadty this block will be skipped
  36. c.execute(""" CREATE TABLE if not exists words(
  37.    word text,
  38.    meaning text
  39.  
  40. )
  41.            
  42. """)
  43.  
  44. #This is our submit function. We get entryies and inserting the values in db and table
  45. def submit():
  46.     conn = sqlite3.connect('user_dict.db')
  47.     c = conn.cursor()
  48.  
  49.     c.execute('INSERT INTO words VALUES (:wordEntry , :meaningEntry)',
  50.               {
  51.                   'wordEntry': wordEntry.get(),
  52.                   'meaningEntry': meaningEntry.get()
  53.               }
  54.               )
  55.  
  56.     conn.commit()
  57.     conn.close()
  58.  
  59.     wordEntry.delete(0 ,END)
  60.     meaningEntry.delete(0 , END)
  61.  
  62.  
  63.  
  64. #This function lead us to secand page
  65. #When clicked "Show words" button showAll function execute and it will list he words
  66. def showAll():
  67.     hideAllFrames()
  68.     showAllFrame.pack(fill="both" , expand="true")
  69.     conn = sqlite3.connect('user_dict.db')
  70.     c = conn.cursor()
  71.  
  72.     c.execute("SELECT * , oid FROM words")
  73.     records = c.fetchall()
  74.     #print(records)
  75.     print_records =''
  76.  
  77.     conn.commit()
  78.     conn.close()
  79.  
  80.     style = ttk.Style()
  81.     style.theme_use('default')
  82.     style.configure('Treeview',
  83.  
  84.                     rowheight=25,
  85.                     )
  86.     style.map('Treeview',
  87.               background=[('selected','#347083')])
  88.  
  89. #selectRecords func. is help us to select the word we wanted
  90.     def selectRecord(e):
  91.         wordEntry.delete(0 ,END)
  92.         meaningEntry.delete(0 , END)
  93.         wordIdEntry.delete(0, END)
  94.         selected = myTree.focus()
  95.         values = myTree.item(selected ,'values')
  96.         wordIdEntry.insert(0 ,values[0])
  97.         wordEntry.insert(0, values[1])
  98.         meaningEntry.insert(0, values[2])
  99.  
  100.  
  101. #When clik "Update Word" button this func. will summon
  102. #And it allows us to change the selected word
  103.     def update():
  104.         selected = myTree.focus()
  105.         myTree.item(selected , text="", values=(wordIdEntry.get(),wordEntry.get(), meaningEntry.get(),))
  106.         conn = sqlite3.connect('user_dict.db')
  107.         c = conn.cursor()
  108.  
  109.         c.execute(""" UPDATE words SET
  110.            word =:word,
  111.            meaning=:meaning
  112.        
  113.            WHERE oid = :oid""",
  114.                   {
  115.                       'word' : wordEntry.get(),
  116.                       'meaning': meaningEntry.get(),
  117.                       'oid' :wordIdEntry.get(),
  118.  
  119.                   }
  120.         )
  121.  
  122.  
  123.         conn.commit()
  124.         conn.close()
  125.  
  126. #If you clicked the "Delete Word" button it will delete the selected word
  127.     def delete():
  128.         selected = myTree.focus()
  129.         myTree.item(selected , text="", values=(wordIdEntry.get(),wordEntry.get(), meaningEntry.get(),))
  130.         conn = sqlite3.connect('user_dict.db')
  131.         c = conn.cursor()
  132.         c.execute("DELETE from words  WHERE oid= " + wordIdEntry.get())
  133.  
  134.         conn.commit()
  135.         conn.close()
  136.  
  137. #We are creating here table for our wordlist
  138.     myTree = ttk.Treeview(root)
  139.     myTree.place(x=57, y=10)
  140.  
  141.     myTree['columns'] = ("ID", "WORD", "MEANING")
  142.     myTree.column("#0", width=0, stretch=NO)
  143.     myTree.column("ID", anchor=CENTER, width=80)
  144.     myTree.column("WORD", anchor=W, width=150)
  145.     myTree.column("MEANING", anchor=W, width=150)
  146.  
  147.     myTree.heading("ID", text="ID", anchor=CENTER)
  148.     myTree.heading("WORD", text="WORD", anchor=CENTER)
  149.     myTree.heading("MEANING", text="MEANING", anchor=CENTER)
  150.  
  151.  
  152.     myTree.tag_configure('oddrow', background="white")
  153.     myTree.tag_configure('evenrow', background="lightblue")
  154.  
  155.     global  count
  156.     count= 0
  157.     for record in records:
  158.         if count % 2 ==0:
  159.             myTree.insert(parent='', index="end" ,iid=count , text="", values=(record[2],record[0],record[1],),tag=('evenrow', ))
  160.         else:
  161.             myTree.insert(parent='', index="end" ,iid=count , text="", values=(record[2],record[0],record[1],),tag=('oddrow', ))
  162.         count +=1
  163.  
  164.  
  165.  
  166.     wordIdLabel = Label(root, text="The words Id: ", bg="light gray")
  167.     wordIdLabel.place(x=109, y=340)
  168.     wordIdEntry = Entry(root, width=30, borderwidth=1, fg="blue")
  169.     wordIdEntry.place(x=185, y=340)
  170.     wordIdEntry.insert(0, "")
  171.  
  172.     wordLabel = Label(root, text="The word you want to update: ", bg="light gray")
  173.     wordLabel.place(x=22, y=370)
  174.     wordEntry = Entry(root, width=30, borderwidth=1, fg="blue")
  175.     wordEntry.place(x=185, y=370)
  176.     wordEntry.insert(0, "")
  177.  
  178.     meaningLabel = Label(root, text="Words meaning:", bg="light gray")
  179.     meaningLabel.place(x=92, y=400)
  180.     meaningEntry = Entry(root, width=30, borderwidth=1, fg="blue")
  181.     meaningEntry.place(x=185, y=400)
  182.     meaningEntry.insert(0, "")
  183.     updateButton = Button(root, text="Update Word",padx=30,pady=10,borderwidth=2,activebackground="gray",bg="#546879",
  184.                           relief=GROOVE, command=update)
  185.     updateButton.place(x=250, y = 500)
  186.     updateFrame = Frame(root, width=550, height=500, bg="light gray")
  187.     deleteButton = Button(root, text="Delete Word",padx=30,pady=10,borderwidth=2,activebackground="gray",bg="#546879",
  188.                           relief=GROOVE, command=delete)
  189.     deleteButton.place(x=80, y = 500)
  190.     myTree.bind("<ButtonRelease-1>", selectRecord ,)
  191.  
  192.  
  193. #hideAllFrames provides us to travel safe between frames.
  194. #when we click "Show words" frames which are in main page will disappear
  195.  
  196. def hideAllFrames():
  197.     insertFrame.pack_forget()
  198.     greetingFrame.pack_forget()
  199.  
  200. greetingFrame = LabelFrame(root,bg="light gray",borderwidth=0,highlightthickness=0)
  201. greetingFrame.pack()
  202. font = tkinter.font.Font(family="Helvetica", size=25, weight="bold")
  203. headerLabel = Label(greetingFrame, text="WELCOME TO" + "\n" + " LEARN VACOBULARY", font=font, bg="light gray")
  204. headerLabel.pack(side="top", fill="x", pady=20)
  205.  
  206. insertFrame = LabelFrame(root, bg="light gray")
  207. insertFrame.pack(fill="x", padx=20, pady=100)
  208.  
  209. wordLabel = Label(insertFrame, text="The word you want to insert:", bg="light gray")
  210. wordLabel.grid(row=0,column=0 ,padx=10 ,pady=10)
  211. wordEntry = Entry(insertFrame, width=30, borderwidth=1, fg="blue")
  212. wordEntry.grid(row=0,column=1,padx=10 ,pady=10)
  213. wordEntry.insert(0, "")
  214.  
  215. meaningLabel = Label(insertFrame, text="Words meaning:", bg="light gray")
  216. meaningLabel.grid(row=1,column=0,padx=10 ,pady=10)
  217. meaningEntry = Entry(insertFrame, width=30, borderwidth=1, fg="blue")
  218. meaningEntry.grid(row=1,column=1,padx=10 ,pady=10)
  219. submitButton = Button(root, text="Submit", padx=30, pady=10, borderwidth=2, activebackground="gray", bg="#546879",
  220.                           relief=GROOVE, command=submit)
  221. submitButton.place(x=100, y=450)
  222.  
  223.  
  224. showAllButton = Button(root, text="Show Words" ,padx=30,pady=10,borderwidth=2,activebackground="gray",bg="#546879",relief=GROOVE, command=showAll)
  225. showAllButton.place(x=250 , y=450)
  226. showAllFrame = Frame(root , width = 550 , height=500 , bg="light gray")
  227.  
  228.  
  229. root.mainloop()
  230.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement