Advertisement
Najeebsk

CONTACT-MANAGEMENT.pyw

Oct 23rd, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.54 KB | None | 0 0
  1. from tkinter import *
  2. import sqlite3
  3. import tkinter.ttk as ttk
  4. import tkinter.messagebox as tkMessageBox
  5.  
  6. #DEVELOPED BY Ashutosh Maurya
  7. root = Tk()
  8. root.title("Najeeb Contact List")
  9. width = 700
  10. height = 400
  11. screen_width = root.winfo_screenwidth()
  12. screen_height = root.winfo_screenheight()
  13. x = (screen_width/2) - (width/2)
  14. y = (screen_height/2) - (height/2)
  15. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  16. root.resizable(0, 0)
  17. root.config(bg="#6666ff")
  18.  
  19. #============================VARIABLES===================================
  20. FIRSTNAME = StringVar()
  21. LASTNAME = StringVar()
  22. GENDER = StringVar()
  23. AGE = StringVar()
  24. ADDRESS = StringVar()
  25. CONTACT = StringVar()
  26.  
  27.  
  28.  
  29. #============================METHODS=====================================
  30.  
  31. def Database():
  32.     conn = sqlite3.connect("pythontut.db")
  33.     cursor = conn.cursor()
  34.     cursor.execute("CREATE TABLE IF NOT EXISTS `member` (mem_id INTEGER NOT NULL  PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT, gender TEXT, age TEXT, address TEXT, contact TEXT)")
  35.     cursor.execute("SELECT * FROM `member` ORDER BY `lastname` ASC")
  36.     fetch = cursor.fetchall()
  37.     for data in fetch:
  38.         tree.insert('', 'end', values=(data))
  39.     cursor.close()
  40.     conn.close()
  41.  
  42. def SubmitData():
  43.     if  FIRSTNAME.get() == "" or LASTNAME.get() == "" or GENDER.get() == "" or AGE.get() == "" or ADDRESS.get() == "" or CONTACT.get() == "":
  44.         result = tkMessageBox.showwarning('', 'Please Complete The Required Field', icon="warning")
  45.     else:
  46.         tree.delete(*tree.get_children())
  47.         conn = sqlite3.connect("pythontut.db")
  48.         cursor = conn.cursor()
  49.         cursor.execute("INSERT INTO `member` (firstname, lastname, gender, age, address, contact) VALUES(?, ?, ?, ?, ?, ?)", (str(FIRSTNAME.get()), str(LASTNAME.get()), str(GENDER.get()), int(AGE.get()), str(ADDRESS.get()), str(CONTACT.get())))
  50.         conn.commit()
  51.         cursor.execute("SELECT * FROM `member` ORDER BY `lastname` ASC")
  52.         fetch = cursor.fetchall()
  53.         for data in fetch:
  54.             tree.insert('', 'end', values=(data))
  55.         cursor.close()
  56.         conn.close()
  57.         FIRSTNAME.set("")
  58.         LASTNAME.set("")
  59.         GENDER.set("")
  60.         AGE.set("")
  61.         ADDRESS.set("")
  62.         CONTACT.set("")
  63.  
  64. def UpdateData():
  65.     if GENDER.get() == "":
  66.        result = tkMessageBox.showwarning('', 'Please Complete The Required Field', icon="warning")
  67.     else:
  68.         tree.delete(*tree.get_children())
  69.         conn = sqlite3.connect("pythontut.db")
  70.         cursor = conn.cursor()
  71.         cursor.execute("UPDATE `member` SET `firstname` = ?, `lastname` = ?, `gender` =?, `age` = ?,  `address` = ?, `contact` = ? WHERE `mem_id` = ?", (str(FIRSTNAME.get()), str(LASTNAME.get()), str(GENDER.get()), str(AGE.get()), str(ADDRESS.get()), str(CONTACT.get()), int(mem_id)))
  72.         conn.commit()
  73.         cursor.execute("SELECT * FROM `member` ORDER BY `lastname` ASC")
  74.         fetch = cursor.fetchall()
  75.         for data in fetch:
  76.             tree.insert('', 'end', values=(data))
  77.         cursor.close()
  78.         conn.close()
  79.         FIRSTNAME.set("")
  80.         LASTNAME.set("")
  81.         GENDER.set("")
  82.         AGE.set("")
  83.         ADDRESS.set("")
  84.         CONTACT.set("")
  85.        
  86.    
  87. def OnSelected(event):
  88.     global mem_id, UpdateWindow
  89.     curItem = tree.focus()
  90.     contents =(tree.item(curItem))
  91.     selecteditem = contents['values']
  92.     mem_id = selecteditem[0]
  93.     FIRSTNAME.set("")
  94.     LASTNAME.set("")
  95.     GENDER.set("")
  96.     AGE.set("")
  97.     ADDRESS.set("")
  98.     CONTACT.set("")
  99.     FIRSTNAME.set(selecteditem[1])
  100.     LASTNAME.set(selecteditem[2])
  101.     AGE.set(selecteditem[4])
  102.     ADDRESS.set(selecteditem[5])
  103.     CONTACT.set(selecteditem[6])
  104.     UpdateWindow = Toplevel()
  105.     UpdateWindow.title("Contact List")
  106.     width = 400
  107.     height = 300
  108.     screen_width = root.winfo_screenwidth()
  109.     screen_height = root.winfo_screenheight()
  110.     x = ((screen_width/2) + 450) - (width/2)
  111.     y = ((screen_height/2) + 20) - (height/2)
  112.     UpdateWindow.resizable(0, 0)
  113.     UpdateWindow.geometry("%dx%d+%d+%d" % (width, height, x, y))
  114.     if 'NewWindow' in globals():
  115.         NewWindow.destroy()
  116.  
  117.     #===================FRAMES==============================
  118.     FormTitle = Frame(UpdateWindow)
  119.     FormTitle.pack(side=TOP)
  120.     ContactForm = Frame(UpdateWindow)
  121.     ContactForm.pack(side=TOP, pady=10)
  122.     RadioGroup = Frame(ContactForm)
  123.     Male = Radiobutton(RadioGroup, text="Male", variable=GENDER, value="Male",  font=('arial', 14)).pack(side=LEFT)
  124.     Female = Radiobutton(RadioGroup, text="Female", variable=GENDER, value="Female",  font=('arial', 14)).pack(side=LEFT)
  125.    
  126.     #===================LABELS==============================
  127.     lbl_title = Label(FormTitle, text="Updating Contacts", font=('arial', 16), bg="orange",  width = 300)
  128.     lbl_title.pack(fill=X)
  129.     lbl_firstname = Label(ContactForm, text="Firstname", font=('arial', 14), bd=5)
  130.     lbl_firstname.grid(row=0, sticky=W)
  131.     lbl_lastname = Label(ContactForm, text="Lastname", font=('arial', 14), bd=5)
  132.     lbl_lastname.grid(row=1, sticky=W)
  133.     lbl_gender = Label(ContactForm, text="Gender", font=('arial', 14), bd=5)
  134.     lbl_gender.grid(row=2, sticky=W)
  135.     lbl_age = Label(ContactForm, text="Age", font=('arial', 14), bd=5)
  136.     lbl_age.grid(row=3, sticky=W)
  137.     lbl_address = Label(ContactForm, text="Address", font=('arial', 14), bd=5)
  138.     lbl_address.grid(row=4, sticky=W)
  139.     lbl_contact = Label(ContactForm, text="Contact", font=('arial', 14), bd=5)
  140.     lbl_contact.grid(row=5, sticky=W)
  141.  
  142.     #===================ENTRY===============================
  143.     firstname = Entry(ContactForm, textvariable=FIRSTNAME, font=('arial', 14))
  144.     firstname.grid(row=0, column=1)
  145.     lastname = Entry(ContactForm, textvariable=LASTNAME, font=('arial', 14))
  146.     lastname.grid(row=1, column=1)
  147.     RadioGroup.grid(row=2, column=1)
  148.     age = Entry(ContactForm, textvariable=AGE,  font=('arial', 14))
  149.     age.grid(row=3, column=1)
  150.     address = Entry(ContactForm, textvariable=ADDRESS,  font=('arial', 14))
  151.     address.grid(row=4, column=1)
  152.     contact = Entry(ContactForm, textvariable=CONTACT,  font=('arial', 14))
  153.     contact.grid(row=5, column=1)
  154.    
  155.  
  156.     #==================BUTTONS==============================
  157.     btn_updatecon = Button(ContactForm, text="Update", width=50, command=UpdateData)
  158.     btn_updatecon.grid(row=6, columnspan=2, pady=10)
  159.  
  160.  
  161. #fn1353p    
  162. def DeleteData():
  163.     if not tree.selection():
  164.        result = tkMessageBox.showwarning('', 'Please Select Something First!', icon="warning")
  165.     else:
  166.         result = tkMessageBox.askquestion('', 'Are you sure you want to delete this record?', icon="warning")
  167.         if result == 'yes':
  168.             curItem = tree.focus()
  169.             contents =(tree.item(curItem))
  170.             selecteditem = contents['values']
  171.             tree.delete(curItem)
  172.             conn = sqlite3.connect("pythontut.db")
  173.             cursor = conn.cursor()
  174.             cursor.execute("DELETE FROM `member` WHERE `mem_id` = %d" % selecteditem[0])
  175.             conn.commit()
  176.             cursor.close()
  177.             conn.close()
  178.    
  179. def AddNewWindow():
  180.     global NewWindow
  181.     FIRSTNAME.set("")
  182.     LASTNAME.set("")
  183.     GENDER.set("")
  184.     AGE.set("")
  185.     ADDRESS.set("")
  186.     CONTACT.set("")
  187.     NewWindow = Toplevel()
  188.     NewWindow.title("Contact List")
  189.     width = 400
  190.     height = 300
  191.     screen_width = root.winfo_screenwidth()
  192.     screen_height = root.winfo_screenheight()
  193.     x = ((screen_width/2) - 455) - (width/2)
  194.     y = ((screen_height/2) + 20) - (height/2)
  195.     NewWindow.resizable(0, 0)
  196.     NewWindow.geometry("%dx%d+%d+%d" % (width, height, x, y))
  197.     if 'UpdateWindow' in globals():
  198.         UpdateWindow.destroy()
  199.    
  200.     #===================FRAMES==============================
  201.     FormTitle = Frame(NewWindow)
  202.     FormTitle.pack(side=TOP)
  203.     ContactForm = Frame(NewWindow)
  204.     ContactForm.pack(side=TOP, pady=10)
  205.     RadioGroup = Frame(ContactForm)
  206.     Male = Radiobutton(RadioGroup, text="Male", variable=GENDER, value="Male",  font=('arial', 14)).pack(side=LEFT)
  207.     Female = Radiobutton(RadioGroup, text="Female", variable=GENDER, value="Female",  font=('arial', 14)).pack(side=LEFT)
  208.    
  209.     #===================LABELS==============================
  210.     lbl_title = Label(FormTitle, text="Adding New Contacts", font=('arial', 16), bg="#66ff66",  width = 300)
  211.     lbl_title.pack(fill=X)
  212.     lbl_firstname = Label(ContactForm, text="Firstname", font=('arial', 14), bd=5)
  213.     lbl_firstname.grid(row=0, sticky=W)
  214.     lbl_lastname = Label(ContactForm, text="Lastname", font=('arial', 14), bd=5)
  215.     lbl_lastname.grid(row=1, sticky=W)
  216.     lbl_gender = Label(ContactForm, text="Gender", font=('arial', 14), bd=5)
  217.     lbl_gender.grid(row=2, sticky=W)
  218.     lbl_age = Label(ContactForm, text="Age", font=('arial', 14), bd=5)
  219.     lbl_age.grid(row=3, sticky=W)
  220.     lbl_address = Label(ContactForm, text="Address", font=('arial', 14), bd=5)
  221.     lbl_address.grid(row=4, sticky=W)
  222.     lbl_contact = Label(ContactForm, text="Contact", font=('arial', 14), bd=5)
  223.     lbl_contact.grid(row=5, sticky=W)
  224.  
  225.     #===================ENTRY===============================
  226.     firstname = Entry(ContactForm, textvariable=FIRSTNAME, font=('arial', 14))
  227.     firstname.grid(row=0, column=1)
  228.     lastname = Entry(ContactForm, textvariable=LASTNAME, font=('arial', 14))
  229.     lastname.grid(row=1, column=1)
  230.     RadioGroup.grid(row=2, column=1)
  231.     age = Entry(ContactForm, textvariable=AGE,  font=('arial', 14))
  232.     age.grid(row=3, column=1)
  233.     address = Entry(ContactForm, textvariable=ADDRESS,  font=('arial', 14))
  234.     address.grid(row=4, column=1)
  235.     contact = Entry(ContactForm, textvariable=CONTACT,  font=('arial', 14))
  236.     contact.grid(row=5, column=1)
  237.    
  238.  
  239.     #==================BUTTONS==============================
  240.     btn_addcon = Button(ContactForm, text="Save", width=50, command=SubmitData)
  241.     btn_addcon.grid(row=6, columnspan=2, pady=10)
  242.  
  243.  
  244.  
  245.  
  246.    
  247. #============================FRAMES======================================
  248. Top = Frame(root, width=500, bd=1, relief=SOLID)
  249. Top.pack(side=TOP)
  250. Mid = Frame(root, width=500,  bg="#6666ff")
  251. Mid.pack(side=TOP)
  252. MidLeft = Frame(Mid, width=100)
  253. MidLeft.pack(side=LEFT, pady=10)
  254. MidLeftPadding = Frame(Mid, width=370, bg="#6666ff")
  255. MidLeftPadding.pack(side=LEFT)
  256. MidRight = Frame(Mid, width=100)
  257. MidRight.pack(side=RIGHT, pady=10)
  258. TableMargin = Frame(root, width=500)
  259. TableMargin.pack(side=TOP)
  260. #============================LABELS======================================
  261. lbl_title = Label(Top, text="Najeeb Contact Management System", font=('arial', 16), width=500)
  262. lbl_title.pack(fill=X)
  263.  
  264. #============================ENTRY=======================================
  265.  
  266. #============================BUTTONS=====================================
  267. btn_add = Button(MidLeft, text="+ ADD NEW", bg="#66ff66", command=AddNewWindow)
  268. btn_add.pack()
  269. btn_delete = Button(MidRight, text="DELETE", bg="red", command=DeleteData)
  270. btn_delete.pack(side=RIGHT)
  271. btn_Message = Button(MidLeftPadding, text="MESSAGE", bg="red", command=AddNewWindow)
  272. btn_Message.pack(side=RIGHT)
  273.  
  274. #============================TABLES======================================
  275. scrollbarx = Scrollbar(TableMargin, orient=HORIZONTAL)
  276. scrollbary = Scrollbar(TableMargin, orient=VERTICAL)
  277. tree = ttk.Treeview(TableMargin, columns=("MemberID", "Firstname", "Lastname", "Gender", "Age", "Address", "Contact"), height=400, selectmode="extended", yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
  278. scrollbary.config(command=tree.yview)
  279. scrollbary.pack(side=RIGHT, fill=Y)
  280. scrollbarx.config(command=tree.xview)
  281. scrollbarx.pack(side=BOTTOM, fill=X)
  282. tree.heading('MemberID', text="MemberID", anchor=W)
  283. tree.heading('Firstname', text="Firstname", anchor=W)
  284. tree.heading('Lastname', text="Lastname", anchor=W)
  285. tree.heading('Gender', text="Gender", anchor=W)
  286. tree.heading('Age', text="Age", anchor=W)
  287. tree.heading('Address', text="Address", anchor=W)
  288. tree.heading('Contact', text="Contact", anchor=W)
  289. tree.column('#0', stretch=NO, minwidth=0, width=0)
  290. tree.column('#1', stretch=NO, minwidth=0, width=0)
  291. tree.column('#2', stretch=NO, minwidth=0, width=80)
  292. tree.column('#3', stretch=NO, minwidth=0, width=120)
  293. tree.column('#4', stretch=NO, minwidth=0, width=90)
  294. tree.column('#5', stretch=NO, minwidth=0, width=80)
  295. tree.column('#6', stretch=NO, minwidth=0, width=120)
  296. tree.column('#7', stretch=NO, minwidth=0, width=120)
  297. tree.pack()
  298. tree.bind('<Double-Button-1>', OnSelected)
  299.  
  300. #============================INITIALIZATION==============================
  301. if __name__ == '__main__':
  302.     Database()
  303.     root.mainloop()
  304.    
  305.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement