Project 1 - English Dictionary App & Library Book Management System
This project uses the skills learnt from week 1 to week 6. The aim of this project is to create an English Dictionary app that returns definitions of English words. When the application is started, it displays a menu as follows:
Main Menu
Add a new word
Find the meaning
Update a word
Exit
Enter Choice:
If item 1 is selected, it should prompt for a word and then accept its meaning. This pair of word and its meaning should be stored in a file called ‘words.txt’. When item 2 is selected, it should prompt for a word, search that word in the file ‘words.txt’ and return the meaning if it is found. If the meaning is not found, it should display an appropriate message. Similarly, when choice 3 is entered, it should accept a word and update its meaning. In all the above three cases, the prompt should return to the main menu. The words and their meanings should be stored as a dictionary in words.txt, using the concept of serialization. If choice 4 is selected, a graceful exit should be performed.
[{"metadata":{},"id":"b29609d9","cell_type":"markdown","source":"# Project 1 - English Dictionary App & Library Book Management System\n"},{"metadata":{},"id":"81ab682c","cell_type":"markdown","source":"1A: English Dictionary App\n\nThis project uses the skills learnt from week 1 to week 6. The aim of this project is to create an English Dictionary app that returns definitions of English words."},{"metadata":{"trusted":true},"id":"7eea311d","cell_type":"code","source":"import json\n\ndef load_words():\n try:\n with open('words.txt', 'r') as file:\n return json.load(file)\n except FileNotFoundError:\n return {}\n\ndef save_words(words):\n with open('words.txt', 'w') as file:\n json.dump(words, file)\n\ndef add_word(words):\n word = input(\"Enter the word: \").strip()\n meaning = input(\"Enter the meaning: \").strip()\n words[word] = meaning\n print(f'Word \"{word}\" added successfully!')\n\ndef find_word(words):\n word = input(\"Enter the word to search: \").strip()\n meaning = words.get(word)\n if meaning:\n print(f'The meaning of \"{word}\" is: {meaning}')\n else:\n print(f'Word \"{word}\" not found!')\n\ndef update_word(words):\n word = input(\"Enter the word to update: \").strip()\n if word in words:\n meaning = input(\"Enter the new meaning: \").strip()\n words[word] = meaning\n print(f'Word \"{word}\" updated successfully!')\n else:\n print(f'Word \"{word}\" not found!')\n\ndef main():\n words = load_words()\n while True:\n print(\"\\nMenu:\")\n print(\"1. Add a new word\")\n print(\"2. Find the meaning of a word\")\n print(\"3. Update the meaning of a word\")\n print(\"4. Exit\")\n\n choice = input(\"Enter your choice: \").strip()\n\n if choice == '1':\n add_word(words)\n elif choice == '2':\n find_word(words)\n elif choice == '3':\n update_word(words)\n elif choice == '4':\n save_words(words)\n print(\"Exiting the program. Goodbye!\")\n break\n else:\n print(\"Invalid choice. Please try again.\")\n\nif __name__ == \"__main__\":\n main()\n","execution_count":4,"outputs":[{"name":"stdout","output_type":"stream","text":"\nMenu:\n1. Add a new word\n2. Find the meaning of a word\n3. Update the meaning of a word\n4. Exit\nEnter your choice: 1\nEnter the word: 2\nEnter the meaning: 3\nWord \"2\" added successfully!\n\nMenu:\n1. Add a new word\n2. Find the meaning of a word\n3. Update the meaning of a word\n4. Exit\nEnter your choice: 4\nExiting the program. Goodbye!\n"}]},{"metadata":{},"id":"b20f878f","cell_type":"markdown","source":"1B: Library Book Management System\n\nIn this project, a Library Book Management System will be implemented with the front end GUI developed using Tkinter and the backend database implemented in SQLite. It is a single window tkinter GUI application which will implement CRUD( create, read, update, delete) operations on a ‘books’ database, in SQLite. The window will contain text widgets to accept / display book title, author, year of publication, ISBN (optional) and a large text area/ list box to display the records which match the search criteria."},{"metadata":{},"id":"4504c478","cell_type":"markdown","source":"setup the Database"},{"metadata":{"trusted":true},"id":"baa4a296","cell_type":"code","source":"import sqlite3\n\ndef connect():\n conn = sqlite3.connect('books.db')\n cur = conn.cursor()\n cur.execute('''\n CREATE TABLE IF NOT EXISTS book (\n id INTEGER PRIMARY KEY,\n title TEXT,\n authour TEXT,\n year INTEGER,\n isbn TEXT\n )\n ''')\n conn.commit()\n conn.close()\n \ndef view():\n conn = sqlite3.connect('books.db')\n cur = conn.cursor()\n cur.execute(\"INSERT INTO book VALUES (NULL, ?, ?, ?, ?)\",(title, author, year, isbn))\n conn.commit()\n conn.close()\n \ndef view():\n conn = sqlite3.connect('books.db')\n cur = conn.cursor()\n cur.execute(\"SELECT * FROM book\")\n rows = cur.fetchall()\n conn.close()\n return rows\n\ndef search(title=\"\", authour=\"\", year=\"\", isbn=\"\"):\n conn = sqlite3.connect('books.db')\n cur = conn.cursor()\n cur.execute(\"SELECT * FROM book WHERE title=? OR authour=? OR year=? OR isbn=?,\", (title, authour, year, isbn))\n rows = cur.fetchall()\n conn.close()\n return rows\n\ndef delete(id):\n conn = sqlite3.connect('books.db')\n cur = conn.cursor()\n cur.execute(\"DELETE FROM book WHERE id=?\", (id,))\n conn.commit()\n conn.close()\n \ndef update(id, title, author, year, isbn):\n conn = sqlite3.connect('books.db')\n cur = conn.cursuor()\n cur.execute(\"UPTATE book SET title=?, year=?, isbn=? WHERE id=?\", (title, author, year, isbn, id))\n coon.commit()\n conn.close()\n \nconnect()","execution_count":5,"outputs":[]},{"metadata":{},"id":"9419a530","cell_type":"markdown","source":"Create the GUI"},{"metadata":{"trusted":true},"id":"0f7cfec5","cell_type":"code","source":"from tkinter import *\nimport sqlite3\n\n# Backend functions to interact with the database\ndef connect():\n conn = sqlite3.connect('books.db')\n cur = conn.cursor()\n cur.execute('''\n CREATE TABLE IF NOT EXISTS book (\n id INTEGER PRIMARY KEY,\n title TEXT,\n author TEXT,\n year INTEGER,\n isbn TEXT\n )\n ''')\n conn.commit()\n conn.close()\n\ndef insert(title, author, year, isbn):\n conn = sqlite3.connect('books.db')\n cur = conn.cursor()\n cur.execute(\"INSERT INTO book VALUES (NULL, ?, ?, ?, ?)\", (title, author, year, isbn))\n conn.commit()\n conn.close()\n view()\n\ndef view():\n conn = sqlite3.connect('books.db')\n cur = conn.cursor()\n cur.execute(\"SELECT * FROM book\")\n rows = cur.fetchall()\n conn.close()\n return rows\n\ndef search(title=\"\", author=\"\", year=\"\", isbn=\"\"):\n conn = sqlite3.connect('books.db')\n cur = conn.cursor()\n cur.execute(\"SELECT * FROM book WHERE title=? OR author=? OR year=? OR isbn=?\", (title, author, year, isbn))\n rows = cur.fetchall()\n conn.close()\n return rows\n\ndef delete(id):\n conn = sqlite3.connect('books.db')\n cur = conn.cursor()\n cur.execute(\"DELETE FROM book WHERE id=?\", (id,))\n conn.commit()\n conn.close()\n view()\n\ndef update(id, title, author, year, isbn):\n conn = sqlite3.connect('books.db')\n cur = conn.cursor()\n cur.execute(\"UPDATE book SET title=?, author=?, year=?, isbn=? WHERE id=?\", (title, author, year, isbn, id))\n conn.commit()\n conn.close()\n view()\n\n# GUI functions to handle events\ndef get_selected_row(event):\n global selected_tuple\n index = list1.curselection()[0]\n selected_tuple = list1.get(index)\n e1.delete(0, END)\n e1.insert(END, selected_tuple[1])\n e2.delete(0, END)\n e2.insert(END, selected_tuple[2])\n e3.delete(0, END)\n e3.insert(END, selected_tuple[3])\n e4.delete(0, END)\n e4.insert(END, selected_tuple[4])\n\ndef view_command():\n list1.delete(0, END)\n for row in view():\n list1.insert(END, row)\n\ndef search_command():\n list1.delete(0, END)\n for row in search(title_text.get(), author_text.get(), year_text.get(), isbn_text.get()):\n list1.insert(END, row)\n\ndef add_command():\n insert(title_text.get(), author_text.get(), year_text.get(), isbn_text.get())\n list1.delete(0, END)\n list1.insert(END, (title_text.get(), author_text.get(), year_text.get(), isbn_text.get()))\n\ndef delete_command():\n delete(selected_tuple[0])\n\ndef update_command():\n update(selected_tuple[0], title_text.get(), author_text.get(), year_text.get(), isbn_text.get())\n\n# Create the main window\nwindow = Tk()\n\nwindow.wm_title(\"Library Book Management System\")\n\nl1 = Label(window, text=\"Title\")\nl1.grid(row=0, column=0)\n\nl2 = Label(window, text=\"Author\")\nl2.grid(row=0, column=2)\n\nl3 = Label(window, text=\"Year\")\nl3.grid(row=1, column=0)\n\nl4 = Label(window, text=\"ISBN\")\nl4.grid(row=1, column=2)\n\ntitle_text = StringVar()\ne1 = Entry(window, textvariable=title_text)\ne1.grid(row=0, column=1)\n\nauthor_text = StringVar()\ne2 = Entry(window, textvariable=author_text)\ne2.grid(row=0, column=3)\n\nyear_text = StringVar()\ne3 = Entry(window, textvariable=year_text)\ne3.grid(row=1, column=1)\n\nisbn_text = StringVar()\ne4 = Entry(window, textvariable=isbn_text)\ne4.grid(row=1, column=3)\n\nlist1 = Listbox(window, height=6, width=35)\nlist1.grid(row=2, column=0, rowspan=6, columnspan=2)\n\nsb1 = Scrollbar(window)\nsb1.grid(row=2, column=2, rowspan=6)\n\nlist1.configure(yscrollcommand=sb1.set)\nsb1.configure(command=list1.yview)\n\nlist1.bind('<