Advertisement
judrie13

crud shits

Feb 18th, 2020
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.04 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import ttk
  3. import tkinter.messagebox as MessageBox
  4. import pyodbc
  5.  
  6. #KONEKTA
  7. conn = pyodbc.connect('Driver={SQL Server}; Server=COMPLAB212-PC25; Database=db_clothing_line; Trusted_Connection=yes; ')
  8. cursor = conn.cursor()
  9.  
  10. #BINTANA
  11. crud = Tk() ; crud.title("Inventory") ; crud.resizable(False, False)
  12. window_width = 800 ; window_height = 350
  13. screen_width = crud.winfo_screenwidth() ; screen_height = crud.winfo_screenheight()
  14. x_cordinate = int((screen_width/2 ) - (window_width/2)) ; y_cordinate = int((screen_height) - (window_height/2))
  15. crud.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
  16.  
  17. #PANG-ANDAR
  18. def clear():
  19.     global id_num
  20.     e_name.delete(first = 0, last= 22)
  21.     e_desc.delete(first = 0, last= 22)
  22.     e_size.delete(first = 0, last=22)
  23.     e_price.delete(first = 0, last= 22)
  24.     id_num = ""
  25.     e_name.focus()
  26.  
  27. def insert():
  28.     global id_num
  29.  
  30.     try:
  31.         name = e_name.get().upper()
  32.         desc = e_desc.get().upper()
  33.         size = e_size.get().upper()
  34.         price = e_price.get()
  35.  
  36.         if(name == "" or price == "" or desc== "" or size == ""):
  37.             MessageBox.showerror("inser Starus", "All feilds are required")
  38.         else:
  39.             priceCheck = float(price)
  40.             cursor = conn.cursor().execute("insert into dbo.Products (name, description, size, price) values ('"+name+"','"+desc+"','"+size+"','"+price+"')")
  41.             cursor.execute("commit")
  42.             show() ; clear() ; cursor.close()
  43.             MessageBox.showinfo("insert status", "Succesfully Inserted")
  44.     except ValueError: MessageBox.showerror("error", "price only accept number"); e_price.delete(first=0, last = 22)
  45.     except: MessageBox.showerror("error", "Invalid input")
  46.  
  47. def delete():
  48.     global id_num
  49.  
  50.     try: select_item() ; idnum = id_num ; clear()
  51.     except: print("")
  52.     if(idnum == ""):
  53.         MessageBox.showerror("delete status", "select an item t delete")
  54.     else:
  55.         warning = MessageBox.askquestion('Delete product' , 'are you sure?', icon = 'warning' )
  56.         if (warning == 'yes'):
  57.             cursor = conn.cursor().execute("delete from dbo.Products where id = '"+idnum+"'")
  58.             cursor.execute("comit")
  59.             show() ; cursor.close()
  60.  
  61. def update():
  62.     global id_num
  63.  
  64.     name = e_name.get().upper()
  65.     desc = e_desc.get().upper()
  66.     size = e_size.get().upper()
  67.     price = e_price.get()
  68.  
  69.     try: Item = tv.selection()[0]; check = True
  70.     except: check = False
  71.  
  72.     if(check):
  73.         select_item() ; tv.selection_remove(Item)
  74.         insert.configure(state='disabled'); delete.configure(state='disabled')
  75.     elif(id_num ==""):
  76.         MessageBox.showerror("update status" , "select an item to update")
  77.     else:
  78.         try:
  79.             priceCheck = float(price)
  80.             cursor = conn.cursor().execute("update dbo.Products SET name = '"+name+"', description = '"+desc+"', size = '"+size+"', price = '"+price+"' WHERE id = '"+id_num+"'")
  81.             cursor.execute("commit")
  82.             insert.configure(state='normal') ; delete.configure(state='normal')
  83.             show() ; clear() ; cursor.close()
  84.             MessageBox.showinfo("Update Status", "Sucessfullu Updated")
  85.         except ValueError: MessageBox.showerror("error", "Price only accept numbers") ; e_price.delete(first = 0, last = 22)
  86.         except : MessageBox.showerror("Error", "Invalid Input")
  87.  
  88. def show():
  89.     global id_num
  90.  
  91.     for row in tv.get_children():
  92.         tv.delete(row)
  93.     cursor = conn.cursor().execute("select * from products")
  94.     rows = cursor.fetchall()
  95.     for row in rows:
  96.         tv.insert('', 'end', text=row[0], values=(row[0],row[1], row[2], row[3], row[4]))
  97.    
  98. def select_item():
  99.     global id_num
  100.  
  101.     try:
  102.         Item = tv.selection()[0]
  103.         selection = tv.item(Item)
  104.         get = str(selection)
  105.         get = get[get.find('[')+1 : get.find(']')]
  106.         get = get.split(',') ; clear()
  107.         id_num = get[0].replace("'", "")
  108.         e_name.insert(0, get[1].replace("'", "")[1:])
  109.         e_desc.insert(0, get[2].replace("'", "")[1:])
  110.         e_size.insert(0, get[3].replace("'", "")[1:])
  111.         e_price.insert(0, get[4].replace("'", "")[1:])
  112.     except:
  113.         print("Error")
  114.  
  115.  
  116.  
  117.  
  118. #LABEL
  119. name = Label(crud, text = 'PRODUCT NAME', font = ("sans serif", 11))
  120. name.place(x=20, y=40)
  121. desc = Label(crud, text = 'DESCRIPTION', font = ("sans serif", 11))
  122. desc.place(x=20, y=70)
  123. size = Label(crud, text = 'SIZE', font = ("sans serif", 11))
  124. size.place(x=20, y=100)
  125. price = Label(crud, text = 'PRICE', font = ("sans serif", 11))
  126. price.place(x=20, y=130)
  127.  
  128. #KAHON NG TEKSTO
  129. e_name = Entry(font = ("sans serif", 10))
  130. e_name.place(x = 150, y=40)
  131. e_desc = Entry(font = ("sans serif", 10))
  132. e_desc.place(x=150, y=70)
  133. e_size = Entry(font = ("sans serif", 10))
  134. e_size.place(x=150, y=100)
  135. e_price = Entry(font = ("sans serif", 10))
  136. e_price.place(x=150, y=130)
  137.  
  138. #PINDUTAN
  139. insert = Button(crud, text = "INSERT", font = ("sans serif",11), command = insert)
  140. insert.place(x=85, y=160, width = 210 , height= 40)
  141. update = Button(crud, text = "UPDATE", font = ("sans serif",11), command = update)
  142. update.place(x=85, y=190, width =210, height= 40 )
  143. delete = Button(crud, text = "DELETE", font = ("sans serif",11), command = delete)
  144. delete.place(x=85, y=220, width=210, height=40)
  145.  
  146. #LAMESA
  147. frm = Frame(crud)
  148. frm.place(x=300, y=40)
  149. tv = ttk.Treeview(frm, columns = (1,2,3,4,5), selectmode="extended", height = 12, show = "headings")
  150. tv.pack(expand=YES, fill=BOTH)
  151. tv.heading(1, text = "ID")
  152. tv.column(1 ,minwidth=0,width=30, stretch=NO, anchor = 'center')
  153. tv.heading(2, text = "NAME")
  154. tv.column(2 ,minwidth=0,width=120, stretch=NO, anchor = 'center')
  155. tv.heading(3, text = "DESCRIPTION")
  156. tv.column(3 ,minwidth=0,width=150, stretch=NO, anchor = 'center')
  157. tv.heading(4, text = "SIZE")
  158. tv.column(4 ,minwidth=0,width=50, stretch=NO, anchor = 'center')
  159. tv.heading(5, text = "PRICE")
  160. tv.column(5 ,minwidth=0,width=80, stretch=NO, anchor = 'center')
  161.  
  162.  
  163.  
  164.  
  165. show()
  166. clear()
  167. crud.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement