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