Advertisement
Guest User

Python help

a guest
Nov 8th, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.08 KB | None | 0 0
  1. from tkinter import *
  2. import tkinter.messagebox as tkMessageBox
  3. import pymysql.cursors
  4. import tkinter.ttk as ttk
  5. import tkinter as tk
  6. import time
  7. import smtplib
  8. from collections import defaultdict
  9. from email.message import EmailMessage
  10.  
  11. root = Tk()
  12. root.title("Inventory")
  13.  
  14. width = 650
  15. height = 550
  16. screen_width = root.winfo_screenwidth()
  17. screen_height = root.winfo_screenheight()
  18. x = (screen_width / 2) - (width / 2)
  19. y = (screen_height / 2) - (height / 2)
  20. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  21. root.resizable(0, 0)
  22. root.config(bg="")
  23.  
  24. USERNAME = StringVar()
  25. PASSWORD = StringVar()
  26. PRODUCT_NAME = StringVar()
  27. PRODUCT_PRICE = IntVar()
  28. PRODUCT_QTY = IntVar()
  29. SEARCH = StringVar()
  30.  
  31.  
  32. def db():
  33.     global conn, cursor
  34.     conn = pymysql.connect(host='armondaio.trachtman.us',
  35.                            user='armond',
  36.                            password='02197186',
  37.                            db='Inventory',
  38.                            charset='utf8mb4',
  39.                            cursorclass=pymysql.cursors.DictCursor)
  40.     cursor = conn.cursor()
  41.     cursor.execute(
  42.         "SELECT * FROM `admin` WHERE `username` = '%s' AND `password` = "
  43.         "'%s'"), ((USERNAME.get), (PASSWORD.get))
  44.     conn.commit()
  45.  
  46.  
  47. def Exit():
  48.     result = tkMessageBox.askquestion('Inventory System',
  49.                                       'Are you sure you want to exit?',
  50.                                       icon="warning")
  51.     if result == 'yes':
  52.         root.destroy()
  53.         exit()
  54.  
  55.  
  56. def Exit2():
  57.     result = tkMessageBox.askquestion('Inventory System',
  58.                                       'Are you sure you want to exit?',
  59.                                       icon="warning")
  60.     if result == 'yes':
  61.         Home.destroy()
  62.         exit()
  63.  
  64.  
  65. def ShowLoginForm():
  66.     global loginform
  67.     loginform = Toplevel()
  68.     loginform.title("Account Login")
  69.     width = 600
  70.     height = 500
  71.     screen_width = root.winfo_screenwidth()
  72.     screen_height = root.winfo_screenheight()
  73.     x = (screen_width / 2) - (width / 2)
  74.     y = (screen_height / 2) - (height / 2)
  75.     loginform.resizable(0, 0)
  76.     loginform.geometry("%dx%d+%d+%d" % (width, height, x, y))
  77.     LoginForm()
  78.  
  79.  
  80. def LoginForm():
  81.     global lbl_result
  82.     TopLoginForm = Frame(loginform, width=600, height=75, bd=1, relief=SOLID)
  83.     TopLoginForm.pack(side=TOP, pady=20)
  84.     lbl_text = Label(TopLoginForm, text="Login", font=('arial', 18), width=600)
  85.     lbl_text.pack(fill=X)
  86.     MidLoginForm = Frame(loginform, width=600)
  87.     MidLoginForm.pack(side=TOP, pady=50)
  88.     lbl_username = Label(MidLoginForm, text="Username:", font=('arial', 15),
  89.                          bd=18)
  90.     lbl_username.grid(row=0)
  91.     lbl_password = Label(MidLoginForm, text="Password:", font=('arial', 15),
  92.                          bd=18)
  93.     lbl_password.grid(row=1)
  94.     lbl_result = Label(MidLoginForm, text="", font=('arial', 18))
  95.     lbl_result.grid(row=3, columnspan=2)
  96.     username = Entry(MidLoginForm, textvariable=USERNAME, font=('arial', 15),
  97.                      width=15)
  98.     username.grid(row=0, column=1)
  99.     password = Entry(MidLoginForm, textvariable=PASSWORD, font=('arial', 15),
  100.                      width=15, show="*")
  101.     password.grid(row=1, column=1)
  102.     btn_login = Button(MidLoginForm, text="Login", font=('arial', 18),
  103.                        width=30,
  104.                        command=Login)
  105.     btn_login.grid(row=2, columnspan=2, pady=20)
  106.     btn_login.bind('<Return>', Login)
  107.     return Login
  108.  
  109.  
  110. def ShowRegisterForm():
  111.     global registerform
  112.     registerform = Toplevel()
  113.     registerform.title("Register New Account")
  114.     width = 600
  115.     height = 500
  116.     screen_width = root.winfo_screenwidth()
  117.     screen_height = root.winfo_screenheight()
  118.     x = (screen_width / 2) - (width / 2)
  119.     y = (screen_height / 2) - (height / 2)
  120.     registerform.resizable(0, 0)
  121.     registerform.geometry("%dx%d+%d+%d" % (width, height, x, y))
  122.     RegisterForm()
  123.  
  124.  
  125. def RegisterForm():
  126.     global r_result
  127.     TopRegisterForm = Frame(registerform, width=600, height=75, bd=1,
  128.                             relief=SOLID)
  129.     TopRegisterForm.pack(side=TOP, pady=20)
  130.     r_text = Label(TopRegisterForm, text="Register", font=('arial', 18),
  131.                    width=600)
  132.     r_text.pack(fill=X)
  133.     MidRegisterForm = Frame(registerform, width=600)
  134.     MidRegisterForm.pack(side=TOP, pady=50)
  135.     r_username = Label(MidRegisterForm, text="Username:", font=('arial', 15),
  136.                        bd=18)
  137.     r_username.grid(row=0)
  138.     r_password = Label(MidRegisterForm, text="Password:", font=('arial', 15),
  139.                        bd=18)
  140.     r_password.grid(row=1)
  141.     r_result = Label(MidRegisterForm, text="", font=('arial', 18))
  142.     r_result.grid(row=3, columnspan=2)
  143.     username = Entry(MidRegisterForm, textvariable=USERNAME,
  144.                      font=('arial', 15),
  145.                      width=15)
  146.     username.grid(row=0, column=1)
  147.     password = Entry(MidRegisterForm, textvariable=PASSWORD,
  148.                      font=('arial', 15),
  149.                      width=15, show="*")
  150.     password.grid(row=1, column=1)
  151.     btn_register = Button(MidRegisterForm, text="Register", font=('arial', 18),
  152.                           width=30, command=Login)
  153.     btn_register.grid(row=2, columnspan=2, pady=20)
  154.     btn_register.bind('<Return>', Register)
  155.  
  156.  
  157. def Register():
  158.     global admin_id2
  159.     db()
  160.     if USERNAME.get == "" or PASSWORD.get() == "":
  161.         r_result.config(text="Please complete the required fields!", fg="red")
  162.     else:
  163.         cursor.execute(
  164.             "INSERT INTO `users` (username, password) VALUES('', '')")
  165.     conn.close()
  166.  
  167.  
  168. def Home():
  169.     global Home
  170.     global tree
  171.     Home = Tk()
  172.     Home.title("Home")
  173.     width = 750
  174.     height = 650
  175.     screen_width = Home.winfo_screenwidth()
  176.     screen_height = Home.winfo_screenheight()
  177.     x = (screen_width / 2) - (width / 2)
  178.     y = (screen_height / 2) - (height / 2)
  179.     Home.geometry("%dx%d+%d+%d" % (width, height, x, y))
  180.     Home.resizable(0, 0)
  181.     Title = Frame(Home, bd=1, relief=SOLID)
  182.     Title.pack(pady=10)
  183.     lbl_display = Label(Title, text="Products", font=('arial', 45))
  184.     lbl_display.pack()
  185.  
  186.     button = tk.Button(Title, text="Logout", command=Logout)
  187.     button.pack(side="right", fill='both', expand=True, padx=5, pady=5)
  188.  
  189.     button2 = tk.Button(Title, text="Add new", command=ShowAddNew)
  190.     button2.pack(side="left", fill='both', expand=True, padx=4, pady=4)
  191.  
  192.     menubar = Menu(Home)
  193.     filemenu = Menu(menubar, tearoff=0)
  194.     filemenu2 = Menu(menubar, tearoff=0)
  195.     filemenu.add_command(label="Exit", command=Exit2)
  196.     menubar.add_cascade(label="Account", menu=filemenu)
  197.  
  198.     # Start of searchbar and inventory
  199.  
  200.     search2 = Frame(Home, bd=2, relief=FLAT)
  201.     search2.pack(side=TOP, anchor=CENTER, padx=15, pady=15)
  202.     lbl_txtsearch = Label(Home, text="", font=('arial', 15))
  203.     lbl_txtsearch.pack(side=TOP, anchor=W)
  204.     search = Entry(Home, textvariable=SEARCH, font=('arial', 15), width=100,
  205.                    relief=SOLID)
  206.     search.pack(side=TOP, padx=10, pady=10, fill=X)
  207.     btn_search = Button(search2, text="Search", command=Search)
  208.     btn_search.pack(side=LEFT, padx=10, ipadx=40, ipady=10, pady=10, fill=NONE)
  209.     btn_reset = Button(search2, text="Reset", command=Reset)
  210.     btn_reset.pack(side=LEFT, padx=10, pady=10, ipadx=40, ipady=10, fill=NONE)
  211.     btn_delete = Button(search2, text="Delete", command=Delete)
  212.     btn_delete.pack(side=RIGHT, padx=10, pady=10, ipadx=40, ipady=10,
  213.                     fill=NONE)
  214.     scrollbarx = Scrollbar(Home, orient=HORIZONTAL)
  215.     scrollbary = Scrollbar(Home, orient=VERTICAL)
  216.     tree = ttk.Treeview(Home, columns=(
  217.         "ProductID", "Product Name", "Product Qty", "Product Price"),
  218.                         selectmode="extended", height=50,
  219.                         yscrollcommand=scrollbary.set,
  220.                         xscrollcommand=scrollbarx.set)
  221.     scrollbary.config(command=tree.yview)
  222.     scrollbary.pack(side=RIGHT, fill=Y)
  223.     scrollbarx.config(command=tree.xview)
  224.     scrollbarx.pack(side=BOTTOM, fill=X)
  225.     tree.heading('ProductID', text="ProductID", anchor=W)
  226.     tree.heading('Product Name', text="Product Name", anchor=W)
  227.     tree.heading('Product Qty', text="Product Qty", anchor=W)
  228.     tree.heading('Product Price', text="Product Price", anchor=W)
  229.     tree.column('#0', stretch=YES, minwidth=0, width=0)
  230.     tree.column('#1', stretch=YES, minwidth=0, width=120)
  231.     tree.column('#2', stretch=YES, minwidth=0, width=120)
  232.     tree.column('#3', stretch=YES, minwidth=0, width=120)
  233.     tree.column('#4', stretch=NO, minwidth=0, width=120)
  234.     tree.pack()
  235.     DisplayData()
  236.     Home.config(menu=menubar)
  237.     Home.config(bg="")
  238.  
  239.  
  240. def ShowAddNew():
  241.     global addnewform
  242.     addnewform = Toplevel()
  243.     addnewform.title("Add new")
  244.     width = 600
  245.     height = 500
  246.     screen_width = Home.winfo_screenwidth()
  247.     screen_height = Home.winfo_screenheight()
  248.     x = (screen_width / 2) - (width / 2)
  249.     y = (screen_height / 2) - (height / 2)
  250.     addnewform.geometry("%dx%d+%d+%d" % (width, height, x, y))
  251.     addnewform.resizable(0, 0)
  252.     AddNewForm()
  253.  
  254.  
  255. def AddNewForm():
  256.     TopAddNew = Frame(addnewform, width=600, height=100, bd=1, relief=SOLID)
  257.     TopAddNew.pack(side=TOP, pady=20)
  258.     lbl_text = Label(TopAddNew, text="Add New Product", font=('arial', 18),
  259.                      width=600)
  260.     lbl_text.pack(fill=X)
  261.     MidAddNew = Frame(addnewform, width=600)
  262.     MidAddNew.pack(side=TOP, pady=50)
  263.     lbl_productname = Label(MidAddNew, text="Product Name:",
  264.                             font=('arial', 15),
  265.                             bd=18)
  266.     lbl_productname.grid(row=0, sticky=W)
  267.     lbl_qty = Label(MidAddNew, text="Product Quantity:", font=('arial', 15),
  268.                     bd=18)
  269.     lbl_qty.grid(row=1, sticky=W)
  270.     lbl_price = Label(MidAddNew, text="Product Price:", font=('arial', 15),
  271.                       bd=18)
  272.     lbl_price.grid(row=2, sticky=W)
  273.     productname = Entry(MidAddNew, textvariable=PRODUCT_NAME,
  274.                         font=('arial', 15), width=18)
  275.     productname.grid(row=0, column=1)
  276.     productqty = Entry(MidAddNew, textvariable=PRODUCT_QTY, font=('arial', 15),
  277.                        width=18)
  278.     productqty.grid(row=1, column=1)
  279.     productprice = Entry(MidAddNew, textvariable=PRODUCT_PRICE,
  280.                          font=('arial', 15), width=18)
  281.     productprice.grid(row=2, column=1)
  282.     btn_add = Button(MidAddNew, text="Save", font=('arial', 18), width=30,
  283.                      bg="#009ACD", command=AddNew)
  284.     btn_add.grid(row=3, columnspan=2, pady=20)
  285.  
  286.  
  287. def AddNew():
  288.     db()
  289.     cursor.execute(
  290.         "INSERT INTO `product` (product_name, product_qty, product_price) "
  291.         "VALUES(%s, %s, %s)",
  292.         (str(PRODUCT_NAME.get()), int(PRODUCT_QTY.get()),
  293.          int(PRODUCT_PRICE.get())))
  294.     conn.commit()
  295.     PRODUCT_NAME.set("")
  296.     PRODUCT_PRICE.set("")
  297.     PRODUCT_QTY.set("")
  298.     cursor.close()
  299.     conn.close()
  300.  
  301.  
  302. def DisplayData():
  303.     db()
  304.     sql = "SELECT * FROM `product`"
  305.     cursor.execute(sql)
  306.     results = cursor.fetchall()
  307.     tree.insert("", 1, "dirIso", text="ProductID")
  308.     for results, dirIso in enumerate(results,1):
  309.         list_of_column_values = [list(_dict.values())[0] for _dict in dirIso]
  310.         tree.insert('dirIso', results, text=list_of_column_values[0],
  311.                     values=list_of_column_values[1:])
  312.     cursor.close()
  313.     conn.close()
  314.  
  315.  
  316. def Search():
  317.     if SEARCH.get() != "":
  318.         tree.delete(*tree.get_children())
  319.         db()
  320.         cursor.execute("SELECT * FROM `product` WHERE `product_name` = %s",
  321.                        ('%' + str(SEARCH.get()) + '%',))
  322.         fetch = cursor.fetchall()
  323.         for data in fetch:
  324.             tree.insert('', 'end', values=(data))
  325.         cursor.close()
  326.         conn.close()
  327.  
  328.  
  329. def Reset():
  330.     tree.delete(*tree.get_children())
  331.     DisplayData()
  332.     SEARCH.set("")
  333.  
  334.  
  335. def Delete():
  336.     if not tree.selection():
  337.         print("ERROR")
  338.     else:
  339.         result = tkMessageBox.askquestion('Inventory System',
  340.                                           'Are you sure you want to delete '
  341.                                           'this record?',
  342.                                           icon="warning")
  343.         if result == 'yes':
  344.             curItem = tree.focus()
  345.             contents = (tree.item(curItem))
  346.             selecteditem = contents['values']
  347.             tree.delete(curItem)
  348.             db()
  349.             cursor.execute(
  350.                 "DELETE FROM `product` WHERE `product_id` = %d" % selecteditem[
  351.                     0])
  352.             conn.commit()
  353.             cursor.close()
  354.             conn.close()
  355.  
  356.  
  357. def ShowView():
  358.     global viewform
  359.     viewform = Toplevel()
  360.     viewform.title("View Product")
  361.     width = 600
  362.     height = 100
  363.     screen_width = Home.winfo_screenwidth()
  364.     screen_height = Home.winfo_screenheight()
  365.     x = (screen_width / 2) - (width / 2)
  366.     y = (screen_height / 2) - (height / 2)
  367.     viewform.geometry("%dx%d+%d+%d" % (width, height, x, y))
  368.     viewform.resizable(0, 0)
  369.     ViewForm()
  370.  
  371.  
  372. def Logout():
  373.     result = tkMessageBox.askquestion('Inventory System',
  374.                                       'Are you sure you want to logout?',
  375.                                       icon="warning")
  376.     if result == 'yes':
  377.         admin_id = ""
  378.         root.deiconify()
  379.         Home.destroy()
  380.  
  381.  
  382. def Login(event=None):
  383.     global admin_id
  384.     db()
  385.     if USERNAME.get == "" or PASSWORD.get() == "":
  386.         lbl_result.config(text="Please complete the required field!", fg="red")
  387.     else:
  388.         cursor.execute(
  389.             "SELECT * FROM `admin` WHERE `username` = %s AND `password` = %s",
  390.             (USERNAME.get(), PASSWORD.get()))
  391.         if cursor.fetchone() is not None:
  392.             cursor.execute(
  393.                 "SELECT * FROM `admin` WHERE `username` = %s AND `password` = "
  394.                 "%s",
  395.                 (USERNAME.get(), PASSWORD.get()))
  396.             data = cursor.fetchone()
  397.             USERNAME.set("")
  398.             PASSWORD.set("")
  399.             lbl_result.config(text="")
  400.             ShowHome()
  401.         else:
  402.             lbl_result.config(text="Invalid username or password", fg="red")
  403.             USERNAME.set("")
  404.             PASSWORD.set("")
  405.  
  406.  
  407. def ShowHome():
  408.     root.withdraw()
  409.     Home()
  410.     loginform.destroy()
  411.  
  412.  
  413. menubar = Menu(root)
  414. filemenu = Menu(menubar, tearoff=0)
  415. filemenu.add_command(label="Exit", command=Exit)
  416. menubar.add_cascade(label="File", menu=filemenu)
  417. root.config(menu=menubar)
  418.  
  419. Title = Frame(root, bd=1, relief=SOLID)
  420. Title.pack(pady=5)
  421.  
  422. title2 = Frame(root, bd=1, relief=FLAT)
  423. title2.pack(pady=200)
  424.  
  425. button4 = tk.Button(title2, text="Login", command=ShowLoginForm)
  426. button4.pack(side="left", fill='both', expand=True, padx=10, pady=10, ipadx=60,
  427.              ipady=10)
  428.  
  429. button5 = tk.Button(title2, text="Register", command=ShowRegisterForm)
  430. button5.pack(side="right", fill='both', expand=True, padx=10, pady=10,
  431.              ipadx=50,
  432.              ipady=10)
  433.  
  434. lbl_display = Label(Title, text="Inventory System", font=('arial', 45))
  435. lbl_display.pack()
  436.  
  437. if __name__ == '__main__':
  438.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement