Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import tkinter.messagebox as tkMessageBox
- import pymysql.cursors
- import tkinter.ttk as ttk
- import tkinter as tk
- import time
- import smtplib
- from collections import defaultdict
- from email.message import EmailMessage
- root = Tk()
- root.title("Inventory")
- width = 650
- height = 550
- screen_width = root.winfo_screenwidth()
- screen_height = root.winfo_screenheight()
- x = (screen_width / 2) - (width / 2)
- y = (screen_height / 2) - (height / 2)
- root.geometry("%dx%d+%d+%d" % (width, height, x, y))
- root.resizable(0, 0)
- root.config(bg="")
- USERNAME = StringVar()
- PASSWORD = StringVar()
- PRODUCT_NAME = StringVar()
- PRODUCT_PRICE = IntVar()
- PRODUCT_QTY = IntVar()
- SEARCH = StringVar()
- def db():
- global conn, cursor
- conn = pymysql.connect(host='armondaio.trachtman.us',
- user='armond',
- password='02197186',
- db='Inventory',
- charset='utf8mb4',
- cursorclass=pymysql.cursors.DictCursor)
- cursor = conn.cursor()
- cursor.execute(
- "SELECT * FROM `admin` WHERE `username` = '%s' AND `password` = "
- "'%s'"), ((USERNAME.get), (PASSWORD.get))
- conn.commit()
- def Exit():
- result = tkMessageBox.askquestion('Inventory System',
- 'Are you sure you want to exit?',
- icon="warning")
- if result == 'yes':
- root.destroy()
- exit()
- def Exit2():
- result = tkMessageBox.askquestion('Inventory System',
- 'Are you sure you want to exit?',
- icon="warning")
- if result == 'yes':
- Home.destroy()
- exit()
- def ShowLoginForm():
- global loginform
- loginform = Toplevel()
- loginform.title("Account Login")
- width = 600
- height = 500
- screen_width = root.winfo_screenwidth()
- screen_height = root.winfo_screenheight()
- x = (screen_width / 2) - (width / 2)
- y = (screen_height / 2) - (height / 2)
- loginform.resizable(0, 0)
- loginform.geometry("%dx%d+%d+%d" % (width, height, x, y))
- LoginForm()
- def LoginForm():
- global lbl_result
- TopLoginForm = Frame(loginform, width=600, height=75, bd=1, relief=SOLID)
- TopLoginForm.pack(side=TOP, pady=20)
- lbl_text = Label(TopLoginForm, text="Login", font=('arial', 18), width=600)
- lbl_text.pack(fill=X)
- MidLoginForm = Frame(loginform, width=600)
- MidLoginForm.pack(side=TOP, pady=50)
- lbl_username = Label(MidLoginForm, text="Username:", font=('arial', 15),
- bd=18)
- lbl_username.grid(row=0)
- lbl_password = Label(MidLoginForm, text="Password:", font=('arial', 15),
- bd=18)
- lbl_password.grid(row=1)
- lbl_result = Label(MidLoginForm, text="", font=('arial', 18))
- lbl_result.grid(row=3, columnspan=2)
- username = Entry(MidLoginForm, textvariable=USERNAME, font=('arial', 15),
- width=15)
- username.grid(row=0, column=1)
- password = Entry(MidLoginForm, textvariable=PASSWORD, font=('arial', 15),
- width=15, show="*")
- password.grid(row=1, column=1)
- btn_login = Button(MidLoginForm, text="Login", font=('arial', 18),
- width=30,
- command=Login)
- btn_login.grid(row=2, columnspan=2, pady=20)
- btn_login.bind('<Return>', Login)
- return Login
- def ShowRegisterForm():
- global registerform
- registerform = Toplevel()
- registerform.title("Register New Account")
- width = 600
- height = 500
- screen_width = root.winfo_screenwidth()
- screen_height = root.winfo_screenheight()
- x = (screen_width / 2) - (width / 2)
- y = (screen_height / 2) - (height / 2)
- registerform.resizable(0, 0)
- registerform.geometry("%dx%d+%d+%d" % (width, height, x, y))
- RegisterForm()
- def RegisterForm():
- global r_result
- TopRegisterForm = Frame(registerform, width=600, height=75, bd=1,
- relief=SOLID)
- TopRegisterForm.pack(side=TOP, pady=20)
- r_text = Label(TopRegisterForm, text="Register", font=('arial', 18),
- width=600)
- r_text.pack(fill=X)
- MidRegisterForm = Frame(registerform, width=600)
- MidRegisterForm.pack(side=TOP, pady=50)
- r_username = Label(MidRegisterForm, text="Username:", font=('arial', 15),
- bd=18)
- r_username.grid(row=0)
- r_password = Label(MidRegisterForm, text="Password:", font=('arial', 15),
- bd=18)
- r_password.grid(row=1)
- r_result = Label(MidRegisterForm, text="", font=('arial', 18))
- r_result.grid(row=3, columnspan=2)
- username = Entry(MidRegisterForm, textvariable=USERNAME,
- font=('arial', 15),
- width=15)
- username.grid(row=0, column=1)
- password = Entry(MidRegisterForm, textvariable=PASSWORD,
- font=('arial', 15),
- width=15, show="*")
- password.grid(row=1, column=1)
- btn_register = Button(MidRegisterForm, text="Register", font=('arial', 18),
- width=30, command=Login)
- btn_register.grid(row=2, columnspan=2, pady=20)
- btn_register.bind('<Return>', Register)
- def Register():
- global admin_id2
- db()
- if USERNAME.get == "" or PASSWORD.get() == "":
- r_result.config(text="Please complete the required fields!", fg="red")
- else:
- cursor.execute(
- "INSERT INTO `users` (username, password) VALUES('', '')")
- conn.close()
- def Home():
- global Home
- global tree
- Home = Tk()
- Home.title("Home")
- width = 750
- height = 650
- screen_width = Home.winfo_screenwidth()
- screen_height = Home.winfo_screenheight()
- x = (screen_width / 2) - (width / 2)
- y = (screen_height / 2) - (height / 2)
- Home.geometry("%dx%d+%d+%d" % (width, height, x, y))
- Home.resizable(0, 0)
- Title = Frame(Home, bd=1, relief=SOLID)
- Title.pack(pady=10)
- lbl_display = Label(Title, text="Products", font=('arial', 45))
- lbl_display.pack()
- button = tk.Button(Title, text="Logout", command=Logout)
- button.pack(side="right", fill='both', expand=True, padx=5, pady=5)
- button2 = tk.Button(Title, text="Add new", command=ShowAddNew)
- button2.pack(side="left", fill='both', expand=True, padx=4, pady=4)
- menubar = Menu(Home)
- filemenu = Menu(menubar, tearoff=0)
- filemenu2 = Menu(menubar, tearoff=0)
- filemenu.add_command(label="Exit", command=Exit2)
- menubar.add_cascade(label="Account", menu=filemenu)
- # Start of searchbar and inventory
- search2 = Frame(Home, bd=2, relief=FLAT)
- search2.pack(side=TOP, anchor=CENTER, padx=15, pady=15)
- lbl_txtsearch = Label(Home, text="", font=('arial', 15))
- lbl_txtsearch.pack(side=TOP, anchor=W)
- search = Entry(Home, textvariable=SEARCH, font=('arial', 15), width=100,
- relief=SOLID)
- search.pack(side=TOP, padx=10, pady=10, fill=X)
- btn_search = Button(search2, text="Search", command=Search)
- btn_search.pack(side=LEFT, padx=10, ipadx=40, ipady=10, pady=10, fill=NONE)
- btn_reset = Button(search2, text="Reset", command=Reset)
- btn_reset.pack(side=LEFT, padx=10, pady=10, ipadx=40, ipady=10, fill=NONE)
- btn_delete = Button(search2, text="Delete", command=Delete)
- btn_delete.pack(side=RIGHT, padx=10, pady=10, ipadx=40, ipady=10,
- fill=NONE)
- scrollbarx = Scrollbar(Home, orient=HORIZONTAL)
- scrollbary = Scrollbar(Home, orient=VERTICAL)
- tree = ttk.Treeview(Home, columns=(
- "ProductID", "Product Name", "Product Qty", "Product Price"),
- selectmode="extended", height=50,
- yscrollcommand=scrollbary.set,
- xscrollcommand=scrollbarx.set)
- scrollbary.config(command=tree.yview)
- scrollbary.pack(side=RIGHT, fill=Y)
- scrollbarx.config(command=tree.xview)
- scrollbarx.pack(side=BOTTOM, fill=X)
- tree.heading('ProductID', text="ProductID", anchor=W)
- tree.heading('Product Name', text="Product Name", anchor=W)
- tree.heading('Product Qty', text="Product Qty", anchor=W)
- tree.heading('Product Price', text="Product Price", anchor=W)
- tree.column('#0', stretch=YES, minwidth=0, width=0)
- tree.column('#1', stretch=YES, minwidth=0, width=120)
- tree.column('#2', stretch=YES, minwidth=0, width=120)
- tree.column('#3', stretch=YES, minwidth=0, width=120)
- tree.column('#4', stretch=NO, minwidth=0, width=120)
- tree.pack()
- DisplayData()
- Home.config(menu=menubar)
- Home.config(bg="")
- def ShowAddNew():
- global addnewform
- addnewform = Toplevel()
- addnewform.title("Add new")
- width = 600
- height = 500
- screen_width = Home.winfo_screenwidth()
- screen_height = Home.winfo_screenheight()
- x = (screen_width / 2) - (width / 2)
- y = (screen_height / 2) - (height / 2)
- addnewform.geometry("%dx%d+%d+%d" % (width, height, x, y))
- addnewform.resizable(0, 0)
- AddNewForm()
- def AddNewForm():
- TopAddNew = Frame(addnewform, width=600, height=100, bd=1, relief=SOLID)
- TopAddNew.pack(side=TOP, pady=20)
- lbl_text = Label(TopAddNew, text="Add New Product", font=('arial', 18),
- width=600)
- lbl_text.pack(fill=X)
- MidAddNew = Frame(addnewform, width=600)
- MidAddNew.pack(side=TOP, pady=50)
- lbl_productname = Label(MidAddNew, text="Product Name:",
- font=('arial', 15),
- bd=18)
- lbl_productname.grid(row=0, sticky=W)
- lbl_qty = Label(MidAddNew, text="Product Quantity:", font=('arial', 15),
- bd=18)
- lbl_qty.grid(row=1, sticky=W)
- lbl_price = Label(MidAddNew, text="Product Price:", font=('arial', 15),
- bd=18)
- lbl_price.grid(row=2, sticky=W)
- productname = Entry(MidAddNew, textvariable=PRODUCT_NAME,
- font=('arial', 15), width=18)
- productname.grid(row=0, column=1)
- productqty = Entry(MidAddNew, textvariable=PRODUCT_QTY, font=('arial', 15),
- width=18)
- productqty.grid(row=1, column=1)
- productprice = Entry(MidAddNew, textvariable=PRODUCT_PRICE,
- font=('arial', 15), width=18)
- productprice.grid(row=2, column=1)
- btn_add = Button(MidAddNew, text="Save", font=('arial', 18), width=30,
- bg="#009ACD", command=AddNew)
- btn_add.grid(row=3, columnspan=2, pady=20)
- def AddNew():
- db()
- cursor.execute(
- "INSERT INTO `product` (product_name, product_qty, product_price) "
- "VALUES(%s, %s, %s)",
- (str(PRODUCT_NAME.get()), int(PRODUCT_QTY.get()),
- int(PRODUCT_PRICE.get())))
- conn.commit()
- PRODUCT_NAME.set("")
- PRODUCT_PRICE.set("")
- PRODUCT_QTY.set("")
- cursor.close()
- conn.close()
- def DisplayData():
- db()
- sql = "SELECT * FROM `product`"
- cursor.execute(sql)
- results = cursor.fetchall()
- tree.insert("", 1, "dirIso", text="ProductID")
- for results, dirIso in enumerate(results,1):
- list_of_column_values = [list(_dict.values())[0] for _dict in dirIso]
- tree.insert('dirIso', results, text=list_of_column_values[0],
- values=list_of_column_values[1:])
- cursor.close()
- conn.close()
- def Search():
- if SEARCH.get() != "":
- tree.delete(*tree.get_children())
- db()
- cursor.execute("SELECT * FROM `product` WHERE `product_name` = %s",
- ('%' + str(SEARCH.get()) + '%',))
- fetch = cursor.fetchall()
- for data in fetch:
- tree.insert('', 'end', values=(data))
- cursor.close()
- conn.close()
- def Reset():
- tree.delete(*tree.get_children())
- DisplayData()
- SEARCH.set("")
- def Delete():
- if not tree.selection():
- print("ERROR")
- else:
- result = tkMessageBox.askquestion('Inventory System',
- 'Are you sure you want to delete '
- 'this record?',
- icon="warning")
- if result == 'yes':
- curItem = tree.focus()
- contents = (tree.item(curItem))
- selecteditem = contents['values']
- tree.delete(curItem)
- db()
- cursor.execute(
- "DELETE FROM `product` WHERE `product_id` = %d" % selecteditem[
- 0])
- conn.commit()
- cursor.close()
- conn.close()
- def ShowView():
- global viewform
- viewform = Toplevel()
- viewform.title("View Product")
- width = 600
- height = 100
- screen_width = Home.winfo_screenwidth()
- screen_height = Home.winfo_screenheight()
- x = (screen_width / 2) - (width / 2)
- y = (screen_height / 2) - (height / 2)
- viewform.geometry("%dx%d+%d+%d" % (width, height, x, y))
- viewform.resizable(0, 0)
- ViewForm()
- def Logout():
- result = tkMessageBox.askquestion('Inventory System',
- 'Are you sure you want to logout?',
- icon="warning")
- if result == 'yes':
- admin_id = ""
- root.deiconify()
- Home.destroy()
- def Login(event=None):
- global admin_id
- db()
- if USERNAME.get == "" or PASSWORD.get() == "":
- lbl_result.config(text="Please complete the required field!", fg="red")
- else:
- cursor.execute(
- "SELECT * FROM `admin` WHERE `username` = %s AND `password` = %s",
- (USERNAME.get(), PASSWORD.get()))
- if cursor.fetchone() is not None:
- cursor.execute(
- "SELECT * FROM `admin` WHERE `username` = %s AND `password` = "
- "%s",
- (USERNAME.get(), PASSWORD.get()))
- data = cursor.fetchone()
- USERNAME.set("")
- PASSWORD.set("")
- lbl_result.config(text="")
- ShowHome()
- else:
- lbl_result.config(text="Invalid username or password", fg="red")
- USERNAME.set("")
- PASSWORD.set("")
- def ShowHome():
- root.withdraw()
- Home()
- loginform.destroy()
- menubar = Menu(root)
- filemenu = Menu(menubar, tearoff=0)
- filemenu.add_command(label="Exit", command=Exit)
- menubar.add_cascade(label="File", menu=filemenu)
- root.config(menu=menubar)
- Title = Frame(root, bd=1, relief=SOLID)
- Title.pack(pady=5)
- title2 = Frame(root, bd=1, relief=FLAT)
- title2.pack(pady=200)
- button4 = tk.Button(title2, text="Login", command=ShowLoginForm)
- button4.pack(side="left", fill='both', expand=True, padx=10, pady=10, ipadx=60,
- ipady=10)
- button5 = tk.Button(title2, text="Register", command=ShowRegisterForm)
- button5.pack(side="right", fill='both', expand=True, padx=10, pady=10,
- ipadx=50,
- ipady=10)
- lbl_display = Label(Title, text="Inventory System", font=('arial', 45))
- lbl_display.pack()
- if __name__ == '__main__':
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement