Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter import ttk
- import tkinter.messagebox as MessageBox
- import pyodbc
- #KONEKTA
- conn = pyodbc.connect('Driver={SQL Server}; Server=COMPLAB212-PC25; Database=db_clothing_line; Trusted_Connection=yes; ')
- cursor = conn.cursor()
- #BINTANA
- crud = Tk() ; crud.title("Inventory") ; crud.resizable(False, False)
- window_width = 800 ; window_height = 350
- screen_width = crud.winfo_screenwidth() ; screen_height = crud.winfo_screenheight()
- x_cordinate = int((screen_width/2 ) - (window_width/2)) ; y_cordinate = int((screen_height) - (window_height/2))
- crud.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
- #PANG-ANDAR
- def clear():
- global id_num
- e_name.delete(first = 0, last= 22)
- e_desc.delete(first = 0, last= 22)
- e_size.delete(first = 0, last=22)
- e_price.delete(first = 0, last= 22)
- id_num = ""
- e_name.focus()
- def insert():
- global id_num
- try:
- name = e_name.get().upper()
- desc = e_desc.get().upper()
- size = e_size.get().upper()
- price = e_price.get()
- if(name == "" or price == "" or desc== "" or size == ""):
- MessageBox.showerror("inser Starus", "All feilds are required")
- else:
- priceCheck = float(price)
- cursor = conn.cursor().execute("insert into dbo.Products (name, description, size, price) values ('"+name+"','"+desc+"','"+size+"','"+price+"')")
- cursor.execute("commit")
- show() ; clear() ; cursor.close()
- MessageBox.showinfo("insert status", "Succesfully Inserted")
- except ValueError: MessageBox.showerror("error", "price only accept number"); e_price.delete(first=0, last = 22)
- except: MessageBox.showerror("error", "Invalid input")
- def delete():
- global id_num
- try: select_item() ; idnum = id_num ; clear()
- except: print("")
- if(idnum == ""):
- MessageBox.showerror("delete status", "select an item t delete")
- else:
- warning = MessageBox.askquestion('Delete product' , 'are you sure?', icon = 'warning' )
- if (warning == 'yes'):
- cursor = conn.cursor().execute("delete from dbo.Products where id = '"+idnum+"'")
- cursor.execute("comit")
- show() ; cursor.close()
- def update():
- global id_num
- name = e_name.get().upper()
- desc = e_desc.get().upper()
- size = e_size.get().upper()
- price = e_price.get()
- try: Item = tv.selection()[0]; check = True
- except: check = False
- if(check):
- select_item() ; tv.selection_remove(Item)
- insert.configure(state='disabled'); delete.configure(state='disabled')
- elif(id_num ==""):
- MessageBox.showerror("update status" , "select an item to update")
- else:
- try:
- priceCheck = float(price)
- cursor = conn.cursor().execute("update dbo.Products SET name = '"+name+"', description = '"+desc+"', size = '"+size+"', price = '"+price+"' WHERE id = '"+id_num+"'")
- cursor.execute("commit")
- insert.configure(state='normal') ; delete.configure(state='normal')
- show() ; clear() ; cursor.close()
- MessageBox.showinfo("Update Status", "Sucessfullu Updated")
- except ValueError: MessageBox.showerror("error", "Price only accept numbers") ; e_price.delete(first = 0, last = 22)
- except : MessageBox.showerror("Error", "Invalid Input")
- def show():
- global id_num
- for row in tv.get_children():
- tv.delete(row)
- cursor = conn.cursor().execute("select * from products")
- rows = cursor.fetchall()
- for row in rows:
- tv.insert('', 'end', text=row[0], values=(row[0],row[1], row[2], row[3], row[4]))
- def select_item():
- global id_num
- try:
- Item = tv.selection()[0]
- selection = tv.item(Item)
- get = str(selection)
- get = get[get.find('[')+1 : get.find(']')]
- get = get.split(',') ; clear()
- id_num = get[0].replace("'", "")
- e_name.insert(0, get[1].replace("'", "")[1:])
- e_desc.insert(0, get[2].replace("'", "")[1:])
- e_size.insert(0, get[3].replace("'", "")[1:])
- e_price.insert(0, get[4].replace("'", "")[1:])
- except:
- print("Error")
- #LABEL
- name = Label(crud, text = 'PRODUCT NAME', font = ("sans serif", 11))
- name.place(x=20, y=40)
- desc = Label(crud, text = 'DESCRIPTION', font = ("sans serif", 11))
- desc.place(x=20, y=70)
- size = Label(crud, text = 'SIZE', font = ("sans serif", 11))
- size.place(x=20, y=100)
- price = Label(crud, text = 'PRICE', font = ("sans serif", 11))
- price.place(x=20, y=130)
- #KAHON NG TEKSTO
- e_name = Entry(font = ("sans serif", 10))
- e_name.place(x = 150, y=40)
- e_desc = Entry(font = ("sans serif", 10))
- e_desc.place(x=150, y=70)
- e_size = Entry(font = ("sans serif", 10))
- e_size.place(x=150, y=100)
- e_price = Entry(font = ("sans serif", 10))
- e_price.place(x=150, y=130)
- #PINDUTAN
- insert = Button(crud, text = "INSERT", font = ("sans serif",11), command = insert)
- insert.place(x=85, y=160, width = 210 , height= 40)
- update = Button(crud, text = "UPDATE", font = ("sans serif",11), command = update)
- update.place(x=85, y=190, width =210, height= 40 )
- delete = Button(crud, text = "DELETE", font = ("sans serif",11), command = delete)
- delete.place(x=85, y=220, width=210, height=40)
- #LAMESA
- frm = Frame(crud)
- frm.place(x=300, y=40)
- tv = ttk.Treeview(frm, columns = (1,2,3,4,5), selectmode="extended", height = 12, show = "headings")
- tv.pack(expand=YES, fill=BOTH)
- tv.heading(1, text = "ID")
- tv.column(1 ,minwidth=0,width=30, stretch=NO, anchor = 'center')
- tv.heading(2, text = "NAME")
- tv.column(2 ,minwidth=0,width=120, stretch=NO, anchor = 'center')
- tv.heading(3, text = "DESCRIPTION")
- tv.column(3 ,minwidth=0,width=150, stretch=NO, anchor = 'center')
- tv.heading(4, text = "SIZE")
- tv.column(4 ,minwidth=0,width=50, stretch=NO, anchor = 'center')
- tv.heading(5, text = "PRICE")
- tv.column(5 ,minwidth=0,width=80, stretch=NO, anchor = 'center')
- show()
- clear()
- crud.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement