daily pastebin goal
43%
SHARE
TWEET

Python help2

a guest Nov 8th, 2018 174 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  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.     fields = [item[0] for item in cursor.description]
  308.     for data in results:
  309.         tree.insert('', 'end', values=(fields))
  310.     cursor.close()
  311.     conn.close()
  312.  
  313. def Search():
  314.     if SEARCH.get() != "":
  315.         tree.delete(*tree.get_children())
  316.         db()
  317.         cursor.execute("SELECT * FROM `product` WHERE `product_name` = %s",
  318.                        ('%' + str(SEARCH.get()) + '%',))
  319.         fetch = cursor.fetchall()
  320.         for data in fetch:
  321.             tree.insert('', 'end', values=(data))
  322.         cursor.close()
  323.         conn.close()
  324.  
  325.  
  326. def Reset():
  327.     tree.delete(*tree.get_children())
  328.     DisplayData()
  329.     SEARCH.set("")
  330.  
  331.  
  332. def Delete():
  333.     if not tree.selection():
  334.         print("ERROR")
  335.     else:
  336.         result = tkMessageBox.askquestion('Inventory System',
  337.                                           'Are you sure you want to delete '
  338.                                           'this record?',
  339.                                           icon="warning")
  340.         if result == 'yes':
  341.             curItem = tree.focus()
  342.             contents = (tree.item(curItem))
  343.             selecteditem = contents['values']
  344.             tree.delete(curItem)
  345.             db()
  346.             cursor.execute(
  347.                 "DELETE FROM `product` WHERE `product_name` = %d" % selecteditem[
  348.                     0])
  349.             conn.commit()
  350.             cursor.close()
  351.             conn.close()
  352.  
  353.  
  354. def ShowView():
  355.     global viewform
  356.     viewform = Toplevel()
  357.     viewform.title("View Product")
  358.     width = 600
  359.     height = 100
  360.     screen_width = Home.winfo_screenwidth()
  361.     screen_height = Home.winfo_screenheight()
  362.     x = (screen_width / 2) - (width / 2)
  363.     y = (screen_height / 2) - (height / 2)
  364.     viewform.geometry("%dx%d+%d+%d" % (width, height, x, y))
  365.     viewform.resizable(0, 0)
  366.     ViewForm()
  367.  
  368.  
  369. def Logout():
  370.     result = tkMessageBox.askquestion('Inventory System',
  371.                                       'Are you sure you want to logout?',
  372.                                       icon="warning")
  373.     if result == 'yes':
  374.         admin_id = ""
  375.         root.deiconify()
  376.         Home.destroy()
  377.  
  378.  
  379. def Login(event=None):
  380.     global admin_id
  381.     db()
  382.     if USERNAME.get == "" or PASSWORD.get() == "":
  383.         lbl_result.config(text="Please complete the required field!", fg="red")
  384.     else:
  385.         cursor.execute(
  386.             "SELECT * FROM `admin` WHERE `username` = %s AND `password` = %s",
  387.             (USERNAME.get(), PASSWORD.get()))
  388.         if cursor.fetchone() is not None:
  389.             cursor.execute(
  390.                 "SELECT * FROM `admin` WHERE `username` = %s AND `password` = "
  391.                 "%s",
  392.                 (USERNAME.get(), PASSWORD.get()))
  393.             data = cursor.fetchone()
  394.             USERNAME.set("")
  395.             PASSWORD.set("")
  396.             lbl_result.config(text="")
  397.             ShowHome()
  398.         else:
  399.             lbl_result.config(text="Invalid username or password", fg="red")
  400.             USERNAME.set("")
  401.             PASSWORD.set("")
  402.  
  403.  
  404. def ShowHome():
  405.     root.withdraw()
  406.     Home()
  407.     loginform.destroy()
  408.  
  409.  
  410. menubar = Menu(root)
  411. filemenu = Menu(menubar, tearoff=0)
  412. filemenu.add_command(label="Exit", command=Exit)
  413. menubar.add_cascade(label="File", menu=filemenu)
  414. root.config(menu=menubar)
  415.  
  416. Title = Frame(root, bd=1, relief=SOLID)
  417. Title.pack(pady=5)
  418.  
  419. title2 = Frame(root, bd=1, relief=FLAT)
  420. title2.pack(pady=200)
  421.  
  422. button4 = tk.Button(title2, text="Login", command=ShowLoginForm)
  423. button4.pack(side="left", fill='both', expand=True, padx=10, pady=10, ipadx=60,
  424.              ipady=10)
  425.  
  426. button5 = tk.Button(title2, text="Register", command=ShowRegisterForm)
  427. button5.pack(side="right", fill='both', expand=True, padx=10, pady=10,
  428.              ipadx=50,
  429.              ipady=10)
  430.  
  431. lbl_display = Label(Title, text="Inventory System", font=('arial', 45))
  432. lbl_display.pack()
  433.  
  434. if __name__ == '__main__':
  435.     root.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top