app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 |
from tkinter import * import mysql.connector from tkinter.ttk import Scrollbar import tkinter.messagebox as MessageBox from mysql.connector import Error from PIL import ImageTk, Image from operator import itemgetter import smtplib from random import randint def registerUser(): # Submit for user def submit(): u_name = user_name.get() passw = password.get() c_passw = confirm_password.get() f_name = first_name.get() l_name = last_name.get() gen = var.get() zipc = zipcode.get() que = queNumber.get() p_no = phone_no.get() em = email.get() answer = ans.get() if u_name == '' or passw == '' or c_passw == '' or f_name == '' or gen == '' or zipc == '' or p_no == '' or answer == '' or em == '': MessageBox.showinfo("Warning", "All the fields marked with (*) are compulsory") if passw != c_passw: MessageBox.showinfo( 'Warning', 'Password and confirm password does not match') else: conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) c = conn.cursor() defaultIsLoggedInValue = 'F' # Insert the record into the table user_data = "insert into user_info(user_name, password, first_name, last_name, gender, zipcode, email, que, ans, phone_no, isLoggedIn) values('"+u_name+"','"+passw+"','" + \ f_name+"','"+l_name+"','"+gen+"','"+zipc+"','" + \ em+"','"+que+"','"+answer+"','"+p_no+"','"+defaultIsLoggedInValue+"')" c.execute(user_data) # Commit Changes conn.commit() # Close the connection to the database conn.close() reset() showUserSubmitted = Toplevel() showUserSubmitted.geometry("300x300") showUserSubmitted.title("Submission") Label(showUserSubmitted, text="Registration Successfull").pack() def reset(): user_name.delete(0, 'end') password.delete(0, 'end') confirm_password.delete(0, 'end') first_name.delete(0, 'end') last_name.delete(0, 'end') zipcode.delete(0, 'end') email.delete(0, 'end') phone_no.delete(0, 'end') ans.delete(0, 'end') register = Toplevel() register.title('Login System') register.geometry('500x500') var = StringVar(register) queNumber = StringVar(register) # Entries user_name = Entry(register, width=30) user_name.grid(row=0, column=1, padx=20) password = Entry(register, width=30, show="*") password.grid(row=1, column=1, padx=20) confirm_password = Entry(register, width=30, show="*") confirm_password.grid(row=2, column=1, padx=20) first_name = Entry(register, width=30) first_name.grid(row=3, column=1, padx=20) last_name = Entry(register, width=30) last_name.grid(row=4, column=1, padx=20) zipcode = Entry(register, width=30) zipcode.grid(row=9, column=1, padx=20) email = Entry(register, width=30) email.grid(row=10, column=1, padx=20) options = ('----None----', 'What was your favorite sport in high school?', "What is your pet's name?", "In what year was your father born?", "In what year was your mother born?", "In what country where you born?", "What is your favorite movie?", "What is your favorite sport?", "What is the name of your hometown?", "What was your favourite subject in school?") queNumber.set(options[0]) drop = OptionMenu(register, queNumber, *options) drop.grid(row=11, column=1) ans = Entry(register, width=30) ans.grid(row=12, column=1, padx=20) phone_no = Entry(register, width=30) phone_no.grid(row=13, column=1, padx=20) # Labels Label(register, text="Username*").grid(row=0, column=0) Label(register, text="Password*").grid(row=1, column=0) Label( register, text="Confirm Password*").grid(row=2, column=0) Label( register, text="First Name*").grid(row=3, column=0) Label(register, text="Last Name").grid(row=4, column=0) Label(register, text='Gender*').grid(row=5, column=0) gender_of_male = Radiobutton( register, indicatoron=0, variable=var, text='Male', value='M') gender_of_male.grid(row=6, column=1, sticky="nsew") gender_female = Radiobutton( register, indicatoron=0, variable=var, text='Female', value='F') gender_female.grid(row=7, column=1, sticky="nsew") gender_trans = Radiobutton( register, indicatoron=0, variable=var, text='Other', value='T') gender_trans.grid(row=8, column=1, sticky="nsew") Label(register, text="Pincode*").grid(row=9, column=0) Label(register, text="Email*").grid(row=10, column=0) Label( register, text="Security question*").grid(row=11, column=0) Label(register, text="Answer*").grid(row=12, column=0) Label(register, text="Phone No*").grid(row=13, column=0) Label( register, text="All fields marked with (*) are compulsory").place(x=40, y=400) # Create Submit Button submit_btn = Button(register, text="Register", command=submit) submit_btn.place(x=100, y=350) reset_btn = Button(register, text="Reset", command=reset) reset_btn.place(x=300, y=350) # Execute the register GUI register.mainloop() number = [] def login_user(): def checkUsernamePassword(): def product_category(): loginUser.destroy() conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) c = conn.cursor() c.execute("SELECT * FROM products") allRecords = c.fetchall() productIDs = list(map(itemgetter(0), allRecords)) productNames = list(map(itemgetter(1), allRecords)) productInfo = list(map(itemgetter(2), allRecords)) productPrice = list(map(itemgetter(3), allRecords)) productImages = list(map(itemgetter(4), allRecords)) productCategories = list(map(itemgetter(5), allRecords)) recordOfItemsInCart1 = [] def myCart(): def onFrameConfigure(event): canvas.configure(scrollregion=canvas.bbox("all")) myCart1 = Toplevel() myCart1.title("View Cart") myCart1.geometry("650x650") conn1 = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) canvas = Canvas(myCart1) canvas.grid(sticky=N+S+E+W) yscrollbar = Scrollbar(myCart1, command=canvas.yview) yscrollbar.grid(row=0, column=3, sticky=N+S) canvas.configure(yscrollcommand= yscrollbar.set) frame = Frame(canvas) canvas.create_window((0, 0), window=frame, anchor='nw') frame.bind("<Configure>", onFrameConfigure) myCart1.grid_rowconfigure(0, weight=1) myCart1.grid_columnconfigure(0, weight=1) everyProduct = IntVar() sumOfEveryItem = 0 cur = conn1.cursor() cur.execute("SELECT * FROM cartItems where userID=('"+str(username)+"')") records=cur.fetchall() productIDsInCart = list(map(itemgetter(1), records)) for item in range(len(productIDsInCart)): sql = "SELECT * FROM products where pid=('"+str(productIDsInCart[item])+"')" cur.execute(sql) recordOfItemsInCart1.append(cur.fetchone()) recordOfItemsInCart = list(dict.fromkeys(recordOfItemsInCart1)) namesOfProductsInCart = list(map(itemgetter(1), recordOfItemsInCart)) priceOfProductsInCart = list(map(itemgetter(3), recordOfItemsInCart)) pathOfProductsInCart = list(map(itemgetter(4), recordOfItemsInCart)) counters = [IntVar() for _ in range(len(namesOfProductsInCart))] priceLists = [IntVar() for _ in range(len(namesOfProductsInCart))] priceOfEvery = [] sum1 = 0 totalPrice=IntVar() for i in range(len(namesOfProductsInCart)): counters[i].set(1) sum1 += priceOfProductsInCart[i] sql1 = "UPDATE cartItems set price=('"+str(priceOfProductsInCart[i])+"') where (userID=('"+str(username)+"') and productID=('"+str(productIDsInCart[i])+"'))" cur.execute(sql1) conn1.commit() totalPrice.set(sum1) def increase_stat(event=None, counter=None, mul=None, index=None): counter.set(counter.get() + 1) mul.set(mul.get() + priceOfProductsInCart[index]) sql1 = "UPDATE cartItems set quantity=('"+str(counter.get())+"') where (userID=('"+str(username)+"') and productID=('"+str(productIDsInCart[index])+"'))" sql2 = "UPDATE cartItems set price=('"+str(mul.get())+"') where (userID=('"+str(username)+"') and productID=('"+str(productIDsInCart[index])+"'))" try: cur.execute(sql1) conn1.commit() cur.execute(sql2) conn1.commit() except: MessageBox.showinfo("Error", "Unable to process request SQL") conn1.rollback() def decrease_stat(event=None, counter=None, mul=None ,index=None): counter.set(counter.get() - 1) mul.set(mul.get() - priceOfProductsInCart[index]) sql1 = "UPDATE cartItems set quantity=('"+str(counter.get())+"') where (userID=('"+str(username)+"') and productID=('"+str(productIDsInCart[index])+"'))" sql2 = "UPDATE cartItems set price=('"+str(mul.get())+"') where (userID=('"+str(username)+"') and productID=('"+str(productIDsInCart[index])+"'))" try: cur.execute(sql1) conn1.commit() cur.execute(sql2) conn1.commit() except: MessageBox.showinfo("Error", "Unable to process request") conn1.rollback() def calcPrice(): cur.execute("SELECT sum(price) from cartItems where userID=('"+username+"')") rec = cur.fetchone() totalPrice.set(rec[0]) def checkOut(): orderid = randint(10000, 99999) conn2 = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) c2 = conn2.cursor() sql0 = "SELECT price FROM cartItems where userID=('"+str(username)+"')" c2.execute(sql0) rec1 = c2.fetchall() totalPriceList = list(map(itemgetter(0), rec1)) print(totalPriceList) sql2 = "SELECT * FROM showorders" c2.execute(sql2) rec = c2.fetchall() orderIDList = list(map(itemgetter(2), rec)) if orderid not in orderIDList: for item in range(len(productIDsInCart)): sql1 = "INSERT INTO showorders(userID, productID, orderID, price) values('"+str(username)+"','"+str(productIDsInCart[item])+"','"+str(orderid)+"','"+str(totalPriceList[item])+"')" c2.execute(sql1) conn2.commit() sql3 = "DELETE FROM cartItems where userID=('"+str(username)+"')" c2.execute(sql3) conn2.commit() MessageBox.showinfo("CHECKED OUT", "Your Order ID is "+str(orderid)+" and yet to be confirmed by admin") conn2.close() conn1.close() myCart1.destroy() else: orderid = randint(10000, 99999) checkOut() for i in range(len(namesOfProductsInCart)): priceLists[i].set(priceOfProductsInCart[i]) row = 1 Label(frame, text="Image").grid(row=0, column=0) Label(frame, text="Name").grid(row=0, column=1) Label(frame, text="Quantity").grid(row=0, column=3) Label(frame, text="Price").grid(row=0, column=5) n = 2 for everyItem in range(len(productIDsInCart)): path = pathOfProductsInCart[everyItem] image = Image.open(path) [imageSizeWidth, imageSizeHeight] = [45, 75] same = True newImageSizeWidth = int(imageSizeWidth*n) if same: newImageSizeHeight = int(imageSizeHeight*n) else: newImageSizeHeight = int(imageSizeHeight/n) image = image.resize((newImageSizeWidth, newImageSizeHeight), Image.ANTIALIAS) img = ImageTk.PhotoImage(image) imgCanvas = Label(frame, image=img) imgCanvas.image = img imgCanvas.grid(row=row, column=0, padx=10, pady=10) Label(frame, text=namesOfProductsInCart[everyItem]).grid(row=row, column=1) Label(frame, textvariable=counters[everyItem]).grid(row=row, column=3) Button(frame, text="+", command=lambda counter=counters[everyItem], mul=priceLists[everyItem], index=everyItem: increase_stat(counter=counter, index=index, mul=mul)).grid(row=row, column=4) Button(frame, text="-", command=lambda counter=counters[everyItem], mul=priceLists[everyItem], index=everyItem: decrease_stat(counter=counter, index=index, mul=mul)).grid(row=row, column=2) priceLabel = Label(frame, textvariable=priceLists[everyItem]) priceLabel.grid(row=row, column=5) row+= 1 calcPriceBtn = Button(frame, text="Calculate Price", command=calcPrice) checkOutBtn = Button(frame, text="Check Out", command=checkOut) Label(frame, text="Total", font='Times 16 bold').grid(row=row+1, column=5) totalPriceLabel = Label(frame, textvariable=totalPrice, font='Times 16 bold') totalPriceLabel.grid(row=row+1, column=6) calcPriceBtn.grid(row=row, column=5) checkOutBtn.grid(row=row+2, column=5) frame.mainloop() canvas.configure(scrollregion=canvas.bbox("all")) def allCategoryContents(row, x, y, categoryList, category): def addToCart(): if my_btn['state'] == ACTIVE: my_btn.configure(state=DISABLED, text="Added to the Cart", image=None, compound=CENTER) my_btn.image = None conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) c = conn.cursor() sql = "INSERT INTO cartItems(userID, productID) values('"+str(username)+"','"+str(productIDs[category])+"')" try: c.execute(sql) MessageBox.showinfo("Product Added!", "Product Added to the Cart") conn.commit() #print(productIDs) except: MessageBox.showinfo('Error','Unable to add product') conn.rollback() finally: conn.close() n=2 cartBtnPath = "/home/dell/Desktop/VSCode/Python_proj/Images/CartIcon.ppm" path = productImages[category] image = Image.open(path) [imageSizeWidth, imageSizeHeight] = [45, 75] same = True newImageSizeWidth = int(imageSizeWidth*n) if same: newImageSizeHeight = int(imageSizeHeight*n) else: newImageSizeHeight = int(imageSizeHeight/n) image = image.resize((newImageSizeWidth, newImageSizeHeight), Image.ANTIALIAS) img = ImageTk.PhotoImage(image) imgCanvas = Label(categoryList, image=img) imgCanvas.image = img imgCanvas.grid(row=row, column=0, padx=10, pady=10) cartBtnImg1 = Image.open(cartBtnPath) cartBtnImg1 = cartBtnImg1.resize((20, 20), Image.ANTIALIAS) cartBtnImg = ImageTk.PhotoImage(cartBtnImg1) Label(categoryList, text=productNames[category], font="Times 16").grid(row=row, column=1, padx=10, pady=10) my_btn = Button(categoryList, text="Add to Cart", font="Times 14", image=cartBtnImg, compound=LEFT, command=addToCart) my_btn.image = cartBtnImg my_btn.grid(row=row, column=2, padx=10, pady=10) def allCategoryMainPage(parentTk, text, categoryNumber): def onFrameConfigure(event): canvas.configure(scrollregion=canvas.bbox("all")) canvas = Canvas(parentTk) canvas.grid(sticky=N+S+E+W) yscrollbar = Scrollbar(parentTk, command=canvas.yview) yscrollbar.grid(row=0, column=3, sticky=N+S) canvas.configure(yscrollcommand= yscrollbar.set) frame = Frame(canvas) canvas.create_window((0, 0), window=frame, anchor='nw') frame.bind("<Configure>", onFrameConfigure) parentTk.grid_rowconfigure(0, weight=1) parentTk.grid_columnconfigure(0, weight=1) cartBtnPath = "/home/dell/Desktop/VSCode/Python_proj/Images/CartIcon.ppm" Label(frame, text=text, font="Times 20").grid(row=0, column=1) cartBtnImg1 = Image.open(cartBtnPath) cartBtnImg1 = cartBtnImg1.resize((20, 20), Image.ANTIALIAS) cartBtnImg = ImageTk.PhotoImage(cartBtnImg1) cartContents = Button(frame, text="My Cart", image=cartBtnImg, compound=LEFT, command=myCart, font="Times 16") cartContents.image = cartBtnImg cartContents.grid(row=0, column=2) row=1 x=0 y=0 for category in range(len(productCategories)): if productCategories[category] == categoryNumber: allCategoryContents(row, x, y, frame, category) row+=1 x+=50 y+=50 canvas.configure(scrollregion=canvas.bbox("all")) def mobileCategory(): mobilesList = Toplevel() mobilesList.title("Mobiles") mobilesList.geometry("600x600") allCategoryMainPage(mobilesList, "MOBILES LIST", 6666) mobilesList.mainloop() def fashionCategory(): fashionList = Toplevel() fashionList.title("Fashion List") fashionList.geometry("600x600") allCategoryMainPage(fashionList, "FASHION LIST", 1111) fashionList.mainloop() def drugCategory(): drugList = Toplevel() drugList.title("Medicines and Drugs") drugList.geometry("600x600") allCategoryMainPage(drugList, "MEDICINES AND DRUGS", 3333) drugList.mainloop() def elecCategory(): elecList = Toplevel() elecList.title("Electronics") elecList.geometry("600x600") allCategoryMainPage(elecList, "ELECTRONICS LIST", 2222) elecList.mainloop() def beautyAndCare(): careList = Toplevel() careList.title("Beauty and Care") careList.geometry("600x600") allCategoryMainPage(careList, "BEAUTY AND CARE", 4444) careList.mainloop() def grocery(): groceryList = Toplevel() groceryList.title("Grocery List") groceryList.geometry("600x600") allCategoryMainPage(groceryList, "GROCERIES", 5555) groceryList.mainloop() def logout_command(): productCategory.destroy() conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) mycursor = conn.cursor() mycursor.execute("SELECT * FROM user_info") myresult = mycursor.fetchall() logInDetail = 'F' mycursor.execute( "UPDATE user_info set isLoggedIn = ('"+logInDetail+"') where user_name=('"+username+"')") conn.commit() MessageBox.showinfo('Logged Out', 'You have successfully logged out') productCategory = Toplevel() productCategory.title("Product Category") productCategory.geometry("700x200") menubar = Menubutton( productCategory, text="---", relief=FLAT, justify=LEFT) menubar.place(x=0, y=0) menubar.menu = Menu(menubar) menubar["menu"] = menubar.menu menubar.menu.add_checkbutton(label=username) menubar.menu.add_checkbutton( label="Logout", command=logout_command) menubar.grid(row=0, sticky=W+E) mobiles = Button(productCategory, text="Mobiles", command=mobileCategory) mobiles.grid(row=2, column=1) fashion = Button(productCategory, text="Fashion", command=fashionCategory) fashion.grid(row=2, column=2) home = Button(productCategory, text="Medicines and Drugs", command=drugCategory) home.grid(row=2, column=3) toys = Button(productCategory, text="Electronics", command=elecCategory) toys.grid(row=2, column=0) groceryBtn = Button(productCategory, text="Groceries", command=grocery) groceryBtn.grid(row=2, column=4) careBtn = Button(productCategory, text="Beauty and Care", command=beautyAndCare) careBtn.grid(row=2, column=5) productCategory.mainloop() username = username_entry.get() password = password_entry.get() try: conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) mycursor = conn.cursor() mycursor.execute("SELECT * FROM user_info") myresult = mycursor.fetchall() username_list = list(map(itemgetter(0), myresult)) password_list = list(map(itemgetter(1), myresult)) isLoggedIn_list = list(map(itemgetter(10), myresult)) if username in username_list: index_of_username = username_list.index(username) if password in password_list[index_of_username]: logInDetail = isLoggedIn_list[index_of_username] logInDetail = 'T' username = username_entry.get() conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) mycursor = conn.cursor() mycursor.execute("SELECT * FROM user_info") myresult = mycursor.fetchall() mycursor.execute( "UPDATE user_info set isLoggedIn = ('"+logInDetail+"') where user_name=('"+username+"')") conn.commit() product_category() else: MessageBox.showinfo("Incorrect password", "Password entered by the user is incorrect") else: MessageBox.showinfo( "Warning!", "Given username does not exist") except Error as e: print('MySQL connection is not established', e) finally: conn.close() def forgotPassword(): def sendOTP(): username = username_entry.get() email = email_id_entry.get() server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login("10bparth@gmail.com", "*********") otpValue = str(randint(1000, 9999)) msg = "{}".format(otpValue) conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) mycursor = conn.cursor(buffered=True) mycursor.execute("SELECT * FROM user_info") sql= "UPDATE user_info SET otp=('"+otpValue+"') WHERE user_name=('"+username+"')" try: mycursor.execute(sql) conn.commit() except Exception as e: conn.rollback() finally: server.sendmail("10bparth@gmail.com", email, msg) server.quit() conn.close() def update(): username = username_entry.get() email = email_id_entry.get() newPassword = new_password_entry.get() confirmNewPassword = confirm_password_entry.get() otp = otp_entry.get() answer = ans.get() if newPassword != confirmNewPassword: MessageBox.showinfo("Warning!", "New password and confirm new password does not match") else: try: conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) mycursor = conn.cursor() mycursor.execute("SELECT * FROM user_info") myresult = mycursor.fetchall() username_list = list(map(itemgetter(0), myresult)) email_id_list = list(map(itemgetter(6), myresult)) answers_list = list(map(itemgetter(8), myresult)) otp_list = list(map(itemgetter(11), myresult)) if username in username_list: index_of_username = username_list.index(username) if email in email_id_list[index_of_username]: if otp in otp_list[index_of_username]: if answer in answers_list[index_of_username]: sql = "UPDATE user_info SET password=('" + \ newPassword + \ "') WHERE user_name=('"+username+"')" try: mycursor.execute(sql) conn.commit() except Error as e: MessageBox.showinfo( "Error", "Error while connecting with database") conn.rollback() else: MessageBox.showinfo( "Warning!", "Answer entered is incorrect") else: MessageBox.showinfo( "Warning!", "OTP entered is incorrect") reset_forgot() else: MessageBox.showinfo("Incorrect email", "Email id entered by the user is incorrect") else: MessageBox.showinfo( "Warning!", "Given username does not exist") except Error as e: print("Error while connecting to database", e) finally: conn.close() reset_forgot() def reset_forgot(): username_entry.delete(0, 'end') password_entry.delete(0, 'end') new_password_entry.delete(0, 'end') confirm_password_entry.delete(0, 'end') ans.delete(0, 'end') queNumber = StringVar() passwordForget = Toplevel() passwordForget.title("Forgot Password") passwordForget.geometry("300x650") Label(passwordForget, text="Username*").pack() username_entry = Entry(passwordForget, width=20) username_entry.pack() Label(passwordForget, text="").pack() Label(passwordForget, text="Email ID*").pack() email_id_entry = Entry(passwordForget, width=20) email_id_entry.pack() Label(passwordForget, text="").pack() otp_btn = Button(passwordForget, text='Send OTP', command=sendOTP) otp_btn.pack() Label(passwordForget, text="").pack() Label(passwordForget, text="OTP*").pack() otp_entry = Entry(passwordForget, width=20, show='*') otp_entry.pack() Label(passwordForget, text="").pack() Label(passwordForget, text="New Password*").pack() new_password_entry = Entry(passwordForget, width=20, show='*') new_password_entry.pack() Label(passwordForget, text="").pack() Label(passwordForget, text="Confirm Password*").pack() confirm_password_entry = Entry(passwordForget, width=20, show='*') confirm_password_entry.pack() Label(passwordForget, text="").pack() Label(passwordForget, text="Security Question*").pack() options = ('----None----', 'What was your favorite sport in high school?', "What is your pet's name?", "In what year was your father born?", "In what year was your mother born?", "In what country where you born?", "What is your favorite movie?", "What is your favorite sport?", "What is the name of your hometown?", "What was your favourite subject in school?") queNumber.set(options[0]) drop = OptionMenu(passwordForget, queNumber, *options) drop.pack() Label(passwordForget, text="").pack() Label(passwordForget, text="Answer*").pack() ans = Entry(passwordForget, width=30) ans.pack() Label(passwordForget, text="").pack() update_btn = Button(passwordForget, text='Update', command=update) update_btn.pack() Label(passwordForget, text="").pack() reset_btn = Button(passwordForget, text="Reset", command=reset_forgot) reset_btn.pack() def reset(): username_entry.delete(0, 'end') password_entry.delete(0, 'end') loginUser = Toplevel() loginUser.title("Login") loginUser.geometry("300x300") Label(loginUser, text="Please enter login details").pack() Label(loginUser, text="").pack() username_label = Label(loginUser, text="Username*").pack() username_entry = Entry(loginUser, width=20) username_entry.pack() Label(loginUser, text="").pack() password_label = Label(loginUser, text="Password*").pack() password_entry = Entry(loginUser, width=20, show='*') password_entry.pack() Label(loginUser, text="").pack() login_button = Button(loginUser, text="Login", width=10, height=1, command=checkUsernamePassword) login_button.pack() Label(loginUser, text="").pack() forgot_password = Button( loginUser, text="Forgot Password", width=15, height=1, command=forgotPassword) forgot_password.pack() Label(loginUser, text="").pack() reset_everything = Button(loginUser, text="Reset", width=10, height=1, command=reset) reset_everything.pack() loginUser.mainloop() def login_admin(): def checkUsernamePassword(): def logout_command(): logOut = Toplevel() conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) mycursor = conn.cursor() mycursor.execute("SELECT * FROM admin_info") myresult = mycursor.fetchall() logInDetail = 'F' mycursor.execute( "UPDATE admin_info set isLoggedIn = ('"+logInDetail+"') where admin_id=('"+username+"')") conn.commit() Label(logOut, text="You have successfully logged out").pack() logOut.mainloop() username = username_entry.get() password = password_entry.get() try: conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) mycursor = conn.cursor() mycursor.execute("SELECT * FROM admin_info") myresult = mycursor.fetchall() username_list = list(map(itemgetter(0), myresult)) password_list = list(map(itemgetter(1), myresult)) isLoggedIn_list = list(map(itemgetter(6), myresult)) if username in username_list: index_of_username = username_list.index(username) if password in password_list[index_of_username]: logInDetail = isLoggedIn_list[index_of_username] logInDetail = 'T' username = username_entry.get() conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) mycursor = conn.cursor() mycursor.execute("SELECT * FROM admin_info") myresult = mycursor.fetchall() mycursor.execute( "UPDATE admin_info set isLoggedIn = ('"+logInDetail+"') where admin_id=('"+username+"')") conn.commit() Login() else: MessageBox.showinfo("Incorrect password", "Password entered by the user is incorrect") else: MessageBox.showinfo( "Warning!", "Given username does not exist") except Error as e: print('MySQL connection is not established', e) finally: conn.close() def forgotPassword(): def sendOTP(): username = username_entry.get() email = email_id_entry.get() server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login("10bparth@gmail.com", "*********") otpValue = str(randint(1000, 9999)) msg = "{}".format(otpValue) conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) mycursor = conn.cursor(buffered=True) mycursor.execute("SELECT * FROM admin_info") sql= "UPDATE admin_info SET otp=('"+otpValue+"') WHERE admin_id=('"+username+"')" try: mycursor.execute(sql) conn.commit() except Exception as e: conn.rollback() finally: server.sendmail("10bparth@gmail.com", email, msg) server.quit() conn.close() def update(): username = username_entry.get() emailId = email_id_entry.get() newPassword = new_password_entry.get() confirmNewPassword = confirm_password_entry.get() otpValue = otp_entry.get() if newPassword != confirmNewPassword: MessageBox.showinfo("Warning!", "New password and confirm new password does not match") else: try: conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) mycursor = conn.cursor() mycursor.execute("SELECT * FROM admin_info") myresult = mycursor.fetchall() username_list = list(map(itemgetter(0), myresult)) otp_list = list(map(itemgetter(8), myresult)) email_id_list = list(map(itemgetter(7), myresult)) if username in username_list: index_of_username = username_list.index(username) if emailId in email_id_list[index_of_username]: if otpValue in otp_list[index_of_username]: sql = "UPDATE admin_info SET password=('" + \ newPassword + \ "') WHERE admin_id=('"+username+"')" try: mycursor.execute(sql) conn.commit() except Error as e: MessageBox.showinfo( "Error", "Error while connecting with database") conn.rollback() else: MessageBox.showinfo("Incorrect OTP", "OTP entered by the user is incorrect") else: MessageBox.showinfo("Incorrect email", "Email ID entered by the user is incorrect") else: MessageBox.showinfo( "Warning!", "Given username does not exist") except Error as e: print("Error while connecting to database", e) finally: conn.close() reset_forgot() def reset_forgot(): username_entry.delete(0, 'end') password_entry.delete(0, 'end') new_password_entry.delete(0, 'end') confirm_password_entry.delete(0, 'end') queNumber = StringVar() passwordForget = Toplevel() passwordForget.geometry("300x450") Label(passwordForget, text="Username*").pack() username_entry = Entry(passwordForget, width=20) username_entry.pack() Label(passwordForget, text="").pack() Label(passwordForget, text="Email ID*").pack() email_id_entry = Entry(passwordForget, width=20) email_id_entry.pack() Label(passwordForget, text="").pack() otp_btn = Button(passwordForget, text='Send OTP', command=sendOTP) otp_btn.pack() Label(passwordForget, text="").pack() Label(passwordForget, text="OTP*").pack() otp_entry = Entry(passwordForget, width=20, show='*') otp_entry.pack() Label(loginAdmin, text="").pack() Label(passwordForget, text="New Password*").pack() new_password_entry = Entry(passwordForget, width=20, show='*') new_password_entry.pack() Label(passwordForget, text="").pack() Label(passwordForget, text="Confirm Password*").pack() confirm_password_entry = Entry(passwordForget, width=20, show='*') confirm_password_entry.pack() Label(passwordForget, text="").pack() update_btn = Button(passwordForget, text='Update', command=update) update_btn.pack() Label(passwordForget, text="").pack() reset_btn = Button(passwordForget, text="Reset", command=reset_forgot) reset_btn.pack() def reset(): username_entry.delete(0, 'end') password_entry.delete(0, 'end') def show_details(): def onFrameConfigure(event): canvas.configure(scrollregion=canvas.bbox("all")) detailsUser= Toplevel() canvas = Canvas(detailsUser) canvas.grid(sticky=N+S+E+W) yscrollbar = Scrollbar(detailsUser, command=canvas.yview) yscrollbar.grid(row=0, column=3, sticky=N+S) canvas.configure(yscrollcommand= yscrollbar.set) frame = Frame(canvas) canvas.create_window((0, 0), window=frame, anchor='nw') frame.bind("<Configure>", onFrameConfigure) detailsUser.grid_rowconfigure(0, weight=1) detailsUser.grid_columnconfigure(0, weight=1) detailsUser.geometry("500x500") Label(frame,text = "Entries",font ="comic 20 bold underline").grid(row=0, column=1) conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython", ) c = conn.cursor() c.execute("select * from user_info") row=1 Label(frame, text="Username").grid(row=row, column=0) Label(frame, text="Passwords").grid(row=row, column=1) Label(frame, text="First Name").grid(row=row, column=2) row += 1 result = c.fetchall() usernames = list(map(itemgetter(0), result)) passwords = list(map(itemgetter(1), result)) firstNames = list(map(itemgetter(2), result)) for record in range(len(result)): Label(frame, text=usernames[record]).grid(row=row, column=0) Label(frame, text=passwords[record]).grid(row=row, column=1) Label(frame, text=firstNames[record]).grid(row=row, column=2) row += 1 canvas.configure(scrollregion=canvas.bbox("all")) detailsUser.mainloop() def modification(): def viewpendingorder(): def onFrameConfigure(event): canvas.configure(scrollregion=canvas.bbox("all")) p= Toplevel() p.geometry("500x500") p.title("View pending orders") canvas = Canvas(p) canvas.grid(sticky=N+S+E+W) yscrollbar = Scrollbar(p, command=canvas.yview) yscrollbar.grid(row=0, column=4, sticky=N+S) canvas.configure(yscrollcommand= yscrollbar.set) frame = Frame(canvas) canvas.create_window((0, 0), window=frame, anchor='nw') frame.bind("<Configure>", onFrameConfigure) p.grid_rowconfigure(0, weight=1) p.grid_columnconfigure(0, weight=1) conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) c = conn.cursor() c.execute("select * from showorders where isApproved=('F')") result = c.fetchall() userID = list(map(itemgetter(0), result)) productID = list(map(itemgetter(1), result)) orderID = list(map(itemgetter(2), result)) price = list(map(itemgetter(3) ,result)) Label(frame, text="PENDING ORDERS").grid(row=0, column=1) row=1 Label(frame, text="Username").grid(row=row, column=0) Label(frame, text="Product ID").grid(row=row, column=1) Label(frame, text="Order ID").grid(row=row, column=2) Label(frame, text="Price").grid(row=row, column=3) row += 1 for record in range(len(result)): Label(frame, text=userID[record]).grid(row=row, column=0) Label(frame, text=productID[record]).grid(row=row, column=1) Label(frame, text=orderID[record]).grid(row=row, column=2) Label(frame, text=price[record]).grid(row=row, column=3) row += 1 canvas.configure(scrollregion=canvas.bbox("all")) p.mainloop() def viewcompleteorder(): def onFrameConfigure(event): canvas.configure(scrollregion=canvas.bbox("all")) s= Toplevel() s.geometry("500x500") s.title("View completed orders") canvas = Canvas(s) canvas.grid(sticky=N+S+E+W) yscrollbar = Scrollbar(s, command=canvas.yview) yscrollbar.grid(row=0, column=4, sticky=N+S) canvas.configure(yscrollcommand= yscrollbar.set) frame = Frame(canvas) canvas.create_window((0, 0), window=frame, anchor='nw') frame.bind("<Configure>", onFrameConfigure) s.grid_rowconfigure(0, weight=1) s.grid_columnconfigure(0, weight=1) conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) c = conn.cursor() c.execute("select * from showorders where isApproved=('T')") result = c.fetchall() userID = list(map(itemgetter(0), result)) productID = list(map(itemgetter(1), result)) orderID = list(map(itemgetter(2), result)) price = list(map(itemgetter(3) ,result)) Label(frame, text="COMPLETED ORDERS").grid(row=0, column=1) row=1 Label(frame, text="Username").grid(row=row, column=0) Label(frame, text="Product ID").grid(row=row, column=1) Label(frame, text="Order ID").grid(row=row, column=2) Label(frame, text="Price").grid(row=row, column=3) row += 1 for record in range(len(result)): Label(frame, text=userID[record]).grid(row=row, column=0) Label(frame, text=productID[record]).grid(row=row, column=1) Label(frame, text=orderID[record]).grid(row=row, column=2) Label(frame, text=price[record]).grid(row=row, column=3) row += 1 canvas.configure(scrollregion=canvas.bbox("all")) s.mainloop() m = Toplevel() m.geometry("300x300") pending = Button(m,text="VIEW PENDING ORDERS",command=viewpendingorder) pending.place(x=150, y=70,anchor=CENTER) confirm = Button(m,text="VIEW FINAL ORDERS",command=viewcompleteorder) confirm.place(x=150, y=120,anchor=CENTER) m.mainloop() def update1(): def deleteorder(): d=Toplevel() d.geometry("400x400") d.title("Delete order") orderID = orderID_entry.get() conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) c = conn.cursor() c.execute("select * from showorders") all_records = c.fetchall() orderList = list(map(itemgetter(2), all_records)) if str(orderID) in str(orderList) : sql = "delete from showorders where orderID =('"+str(orderID)+"')" try: c.execute(sql) conn.commit() MessageBox.showinfo("Success", "order is cancelled") except: conn.rollback() finally: conn.close() else : MessageBox.showinfo("order id is not present in the database") d.mainloop() def confirm_order(): c1 = Toplevel() c1.geometry("400x400") orderID1 =orderID_entry.get() conn = mysql.connector.connect( host="localhost", user="root", passwd="Parth@123", database="loginForPython" ) c = conn.cursor() c.execute("select * from showorders") res = c.fetchall() confirmlist = list(map(itemgetter(2), res)) if str(orderID1) in str(confirmlist): sql = "UPDATE showorders SET isApproved =('T') WHERE orderID =('"+str(orderID1)+"')" sql1 = "select email from user_info where user_name in (select s.userID from showorders as s where s.orderID in (select o.orderID from showorders as o where o.isApproved='T'))" try: c.execute(sql) conn.commit() c.execute(sql1) rec1 = c.fetchall() emailList = list(map(itemgetter(0), rec1)) email = emailList[0] server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login("10bparth@gmail.com", "********") msg = "Your order ID number "+str(orderID1)+" is confirmed " server.sendmail("10bparth@gmail.com", email, msg) server.quit() MessageBox.showinfo("Success", "order confirmed") except: conn.rollback() finally: conn.close() else : MessageBox.showinfo("Error", "orderid is not present in the database") c1.mainloop() m = Toplevel() m.geometry("300x300") Label(m,text ="OrderID",font = "comic 10 bold").grid(row= 0,column=1) orderID_entry = Entry(m, width=20) orderID_entry.grid(row =0,column=2) delete_btn = Button(m,text="DELETE_ORDER",command=deleteorder) delete_btn.grid(row=2, column=1) final_btn = Button(m,text="CONFIRM_ORDER",command =confirm_order) final_btn.grid(row=2, column=2) m.mainloop() def Login(): MessageBox.askquestion("Confirm","Are u sure to visit that page") Afterlogin = Toplevel() Afterlogin.title("Modification") Afterlogin.geometry("500x500") Label(Afterlogin,text ="Administrator work",font ="comic 20 bold").place(x=250,y=20,anchor=CENTER) show_btn = Button(Afterlogin,text = "show logins",command=show_details) show_btn.place(x= 250,y=100,anchor=CENTER) updt_btn = Button(Afterlogin,text="modify orders", command=update1) updt_btn.place(x=250, y=200,anchor=CENTER) vieworder_btn =Button(Afterlogin,text="view orders", command=modification) vieworder_btn.place(x=250,y= 300,anchor=CENTER) loginAdmin = Toplevel() loginAdmin.title("Login") loginAdmin.geometry("300x300") Label(loginAdmin, text="Please enter login details").pack() Label(loginAdmin, text="").pack() username_label = Label(loginAdmin, text="Username*").pack() username_entry = Entry(loginAdmin, width=20) username_entry.pack() Label(loginAdmin, text="").pack() password_label = Label(loginAdmin, text="Password*").pack() password_entry = Entry(loginAdmin, width=20, show='*') password_entry.pack() Label(loginAdmin, text="").pack() login_button = Button(loginAdmin, text="Login", width=10, height=1, command=checkUsernamePassword) login_button.pack() Label(loginAdmin, text="").pack() forgot_password = Button( loginAdmin, text="Forgot Password", width=15, height=1, command=forgotPassword) forgot_password.pack() Label(loginAdmin, text="").pack() reset_everything = Button(loginAdmin, text="Reset", width=10, height=1, command=reset) reset_everything.pack() loginAdmin.mainloop() if __name__=='__main__': main = Tk() main.geometry("500x400") main.title("Main Screen") login_for_admin = Button(main, text="Login as admin", command=login_admin) login_for_admin.place(x=20, y=20, height=40, width=200) login_for_user = Button(main, text="Login as user", command=login_user) login_for_user.place(x=240, y=20, height=40, width=200) register_for_user = Button( main, text="Register as user", command=registerUser) register_for_user.place(x=120, y=120, width=200, height=40) main.mainloop() |