|
| 1 | +''' |
| 2 | +importing all the required libraries |
| 3 | +''' |
| 4 | +import sqlite3 |
| 5 | +from sqlite3 import Error |
| 6 | +from tkinter import * |
| 7 | +import tkinter.messagebox |
| 8 | +root = Tk() |
| 9 | +root.geometry('600x370') |
| 10 | +list_of_names=[] |
| 11 | +root.title('AddressBook') |
| 12 | +Name = StringVar() |
| 13 | +Number = StringVar() |
| 14 | + |
| 15 | +""" creating a database connection to the SQLite database |
| 16 | + specified by db_file |
| 17 | + return: Connection object or None |
| 18 | + """ |
| 19 | +def create_connection(db_file): |
| 20 | + conn = None |
| 21 | + try: |
| 22 | + conn = sqlite3.connect(db_file) |
| 23 | + r_set=conn.execute('''SELECT * from tasks'''); |
| 24 | + for student in r_set: |
| 25 | + list_of_names.append(student[1]) |
| 26 | + return conn |
| 27 | + except Error as e: |
| 28 | + print(e) |
| 29 | + return conn |
| 30 | + |
| 31 | +""" create a table from the create_table_sql statement |
| 32 | + conn: Connection object |
| 33 | + create_table_sql: a CREATE TABLE statement |
| 34 | + """ |
| 35 | +def create_table(conn, create_table_sql): |
| 36 | + try: |
| 37 | + c = conn.cursor() |
| 38 | + c.execute(create_table_sql) |
| 39 | + except Error as e: |
| 40 | + print(e) |
| 41 | + return |
| 42 | + |
| 43 | +''' |
| 44 | +displaying added/deleted message |
| 45 | +''' |
| 46 | +def onClickAdded(): |
| 47 | + tkinter.messagebox.showinfo(" ",Name.get()+" got added") |
| 48 | + |
| 49 | +def onClickDeleted(): |
| 50 | + tkinter.messagebox.showinfo(" ",Name.get()+" got deleted") |
| 51 | + |
| 52 | +""" Create a new task (ie creating new row) for the given Name taking care of all conditions such as Name,phone no |
| 53 | +cannot be empty ,phone no should be 10 digits and also if Name already exist,then it cannot be inerted |
| 54 | +""" |
| 55 | +def create_task(): |
| 56 | + sql = ''' INSERT INTO tasks(name,status_id) |
| 57 | + VALUES(?,?) ''' |
| 58 | + if(Name.get() not in list_of_names): |
| 59 | + |
| 60 | + if((Name.get()=='') | (Number.get()=='') | (len(Number.get())!=10)): |
| 61 | + top = Toplevel(root) |
| 62 | + top.geometry('180x100') |
| 63 | + if((Number.get()=='') | (len(Number.get())!=10)): |
| 64 | + myLabel = Label(top, text="Phone no should be 10 digits\n") |
| 65 | + else: |
| 66 | + myLabel = Label(top, text="NAME IS EMPTY\n") |
| 67 | + myLabel.pack() |
| 68 | + mySubmitButton = Button(top, text=' Back ', command=top.destroy) |
| 69 | + mySubmitButton.pack() |
| 70 | + return |
| 71 | + onClickAdded() |
| 72 | + cur = conn.cursor() |
| 73 | + cur.execute(sql, (Name.get(),Number.get())) |
| 74 | + conn.commit() |
| 75 | + return cur.lastrowid |
| 76 | + else: |
| 77 | + top = Toplevel(root) |
| 78 | + top.geometry('180x100') |
| 79 | + if(Name.get()==''): |
| 80 | + myLabel = Label(top, text="NAME IS EMPTY\n") |
| 81 | + elif((Number.get()=='') | (len(Number.get())!=10)): |
| 82 | + myLabel = Label(top, text="Phone no should be 10 digits\n") |
| 83 | + else: |
| 84 | + myLabel = Label(top, text=Name.get()+" Already Exist\n") |
| 85 | + myLabel.pack() |
| 86 | + mySubmitButton = Button(top, text=' Back ', command=top.destroy) |
| 87 | + mySubmitButton.pack() |
| 88 | + |
| 89 | +""" |
| 90 | +Query tasks by Name, if name not found then it gives a warning saying "NOT Found" |
| 91 | +""" |
| 92 | +def select_task_by_name(): |
| 93 | + cur = conn.cursor() |
| 94 | + cur.execute("SELECT * FROM tasks WHERE name=?", (Name.get(),)) |
| 95 | + rows = cur.fetchall() |
| 96 | + if(len(rows)==0): |
| 97 | + inputDialog = MyDialog(root) |
| 98 | + root.wait_window(inputDialog.top) |
| 99 | + else: |
| 100 | + Number.set(rows[0][2]) |
| 101 | + |
| 102 | +""" |
| 103 | +Editing phone no, if name not found then it gives a warning saying "NOT Found" |
| 104 | +""" |
| 105 | +def update_task(): |
| 106 | + """ |
| 107 | + update priority, begin_date, and end date of a task |
| 108 | + :param conn: |
| 109 | + :param task: |
| 110 | + :return: project id |
| 111 | + """ |
| 112 | + sql = ''' UPDATE tasks |
| 113 | + SET status_id = ? |
| 114 | + WHERE name = ?''' |
| 115 | + if((Name.get() not in list_of_names) | (Name.get()=='')): |
| 116 | + inputDialog = MyDialog(root) |
| 117 | + root.wait_window(inputDialog.top) |
| 118 | + return |
| 119 | + cur = conn.cursor() |
| 120 | + cur.execute(sql, (Number.get(),Name.get())) |
| 121 | + conn.commit() |
| 122 | + |
| 123 | +""" |
| 124 | +Delete a task by name.if not found ,gives a warning!!! |
| 125 | +""" |
| 126 | +def delete_task(): |
| 127 | + if((Name.get() not in list_of_names) | (Name.get()=='')): |
| 128 | + inputDialog = MyDialog(root) |
| 129 | + root.wait_window(inputDialog.top) |
| 130 | + return |
| 131 | + onClickDeleted() |
| 132 | + sql = 'DELETE FROM tasks WHERE name=?' |
| 133 | + cur = conn.cursor() |
| 134 | + cur.execute(sql, (Name.get(),)) |
| 135 | + conn.commit() |
| 136 | + |
| 137 | +""" |
| 138 | +Get all rows in the tasks table |
| 139 | +""" |
| 140 | +def select_all_tasks(): |
| 141 | + r_set=conn.execute('''SELECT * from tasks'''); |
| 142 | + i=0 |
| 143 | + j=0 |
| 144 | + top = Toplevel(root) |
| 145 | + for student in r_set: |
| 146 | + list_of_names.append(student[1]) |
| 147 | + for j in range(len(student)): |
| 148 | + e = Entry(top, width=11, fg='Gray20') |
| 149 | + e.grid(row=i, column=j) |
| 150 | + e.insert(END, student[j]) |
| 151 | + i=i+1 |
| 152 | + okButton= Button(top, text=' ok ', command=top.destroy) |
| 153 | + if(j==0): |
| 154 | + j=1 |
| 155 | + okButton.grid(row=i+3, column=j-1) |
| 156 | + |
| 157 | +''' |
| 158 | +Getting the path of database and defining the table to be created |
| 159 | +''' |
| 160 | +database = r"./Address-Book/addressbook.db" |
| 161 | +sql_create_tasks_table = """CREATE TABLE IF NOT EXISTS tasks ( |
| 162 | + id integer PRIMARY KEY, |
| 163 | + name text NOT NULL, |
| 164 | + status_id integer NOT NULL |
| 165 | + |
| 166 | + );""" |
| 167 | + |
| 168 | +''' |
| 169 | +Creating connection and gives error message if connection failed |
| 170 | +''' |
| 171 | +conn = create_connection(database) |
| 172 | +if conn is not None: |
| 173 | + create_table(conn, sql_create_tasks_table) |
| 174 | +else: |
| 175 | + print("Error! cannot create the database connection.") |
| 176 | + |
| 177 | +''' |
| 178 | +creating dialog box for warnings! |
| 179 | +''' |
| 180 | +class MyDialog: |
| 181 | + def __init__(self, parent): |
| 182 | + top = self.top = Toplevel(parent) |
| 183 | + self.myLabel = Label(top, text=Name.get().upper()+" NOT FOUND!") |
| 184 | + self.myLabel.pack() |
| 185 | + self.mySubmitButton = Button(top, text='Exit', command=self.send) |
| 186 | + self.mySubmitButton.pack() |
| 187 | + |
| 188 | + def send(self): |
| 189 | + self.top.destroy() |
| 190 | + |
| 191 | +''' |
| 192 | +Exiting from the application |
| 193 | +''' |
| 194 | +def EXIT(): |
| 195 | + root.destroy() |
| 196 | + |
| 197 | +''' |
| 198 | +Resetting Name and phone no field |
| 199 | +''' |
| 200 | +def RESET(): |
| 201 | + Name.set('') |
| 202 | + Number.set('') |
| 203 | + |
| 204 | +''' |
| 205 | +Creating UI for whole application |
| 206 | +''' |
| 207 | +Label(root, text = 'NAME', font='Times 15 bold').place(x= 130, y=20) |
| 208 | +Entry(root, textvariable = Name,width=42).place(x= 200, y=25) |
| 209 | +Label(root, text = 'PHONE NO ', font='Times 15 bold').place(x= 130, y=70) |
| 210 | +Entry(root, textvariable = Number,width=35).place(x= 242, y=73) |
| 211 | +Button(root,text=" ADD", font='Times 14 bold',bg='dark gray', command = create_task,width=8).place(x= 130, y=110) |
| 212 | +Button(root,text="EDIT", font='Times 14 bold',bg='dark gray',command = update_task,width=8).place(x= 260, y=108) |
| 213 | +Button(root,text="DELETE", font='Times 14 bold',bg='dark gray',command = delete_task,width=8).place(x= 390, y=107.5) |
| 214 | +Button(root,text="VIEW ALL", font='Times 14 bold',bg='dark gray', command = select_all_tasks,width=12).place(x= 160, y=191) |
| 215 | +Button(root,text="VIEW BY NAME", font='Times 14 bold',bg='dark gray', command = select_task_by_name,width=13).place(x= 330, y=190) |
| 216 | +Button(root,text="EXIT", font='Times 14 bold',bg='dark gray', command = EXIT,width=8).place(x= 200, y=280) |
| 217 | +Button(root,text="RESET", font='Times 14 bold',bg='dark gray', command = RESET,width=8).place(x= 320, y=280) |
| 218 | +root.mainloop() |
0 commit comments