Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import os
- from tkinter import ttk
- from tkinter import *
- import tkinter as tk
- import psycopg2 as pg
- from psycopg2.extras import *
- import psycopg2.extensions
- import config
- from tabulate import *
- from tkinter.font import *
- import psycopg2.extensions
- psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
- psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
- conn = pg.connect("host=localhost dbname=videotk_3 user=postgres password=1234")
- conn.set_client_encoding('UTF8')
- #print("Base de datos Abierta satisfactoriamente")
- cur = conn.cursor()
- root = Tk
- class Empleado():
- #db_name = 'videotk3.db'
- def __init__(self, window):
- self.wind = window
- #self.wind.geometry(200, 200)
- self.wind.title('NOMINA EMPLEADOS')
- # font config
- self.ff10 = Font(family="Consolas", size=11)
- self.ff10b = Font(family="Consolas", size=11, weight=BOLD)
- s = ttk.Style()
- s.configure("Foo2.Treeview", font=self.ff10, padding=1)
- s.configure("Foo2.Treeview.Heading", font=self.ff10b, padding=1)
- # Creating a Frame Container
- frame = LabelFrame(self.wind, text='REGISTRE EL NUEVO EMPLEADO :')
- #ttk.Button(frame, text='Guardar Empleado').grid(row=0, columnspan=0, sticky=W + E)
- frame.grid(row=0, column=0 )
- # Name Input
- Label(frame, text='nombre: ').grid(row=2, column=0)
- self.nombre = Entry(frame)
- # self.name.focus()
- self.nombre.grid(row=2, column=1)
- # Price Input
- Label(frame, text='salario: ').grid(row=2, column=2 )
- self.salario = Entry(frame)
- self.salario.grid(row=2, column=3)
- # Button Add Product
- ttk.Button(frame, text='Guardar Empleado', command = self.add_empleado).grid(row=14, columnspan=8, ipady=9, sticky="")
- # Output Messages
- self.message = Label(text='', fg='red')
- self.message.grid(row=15, column=0, columnspan=2, sticky=W + E)
- # Table
- # tv=Treeview(root, selectmode=BROWSE, height=8, show="tree headings", columns=("key", "value"), style="Foo2.Treeview")
- #
- # Buttons
- ttk.Button(text='DELETE', command=self.delete_empleado).grid( row=16, columnspan=2, ipady=7, sticky= E)
- ttk.Button(text = 'EDIT', command = self.edit_empleado).grid( row=16, columnspan=2, ipady=7, sticky= "")
- ttk.Button(text='VER TABLA', command=self.Ver).grid(row=16, columnspan=12, ipady=7, sticky="")
- self.tree = ttk.Treeview(selectmode=BROWSE, height=5, columns=(
- "nombre", "salario"), style="Foo2.Treeview")
- #ORGANIZO EL ESPACIO DEL WIDGT CON ...(row=100, column=0, columnspan=100)
- self.tree.grid(row=20, column=0, columnspan=20)
- #self.tree.heading('#0', text='IDEMP', anchor=W)
- self.tree.heading('#0', text='nombre', anchor=W)
- self.tree.heading('#1', text='salario', anchor=W)
- self.tree.grid(padx=28, pady=(28, 15))
- scrollbarV = Scrollbar(orient=VERTICAL, command=self.tree.yview)
- scrollbarV.grid( row=20, columnspan=12, ipady=40, sticky=SE) #row=22, columnspan= 1, sticky='NSEW'
- scrollH = Scrollbar(orient=HORIZONTAL,command=self.tree.xview)
- scrollH.grid(row=24, columnspan= 9, ipady=1, sticky=NSEW) #column=25, row=0, sticky=NSEW)
- self.get_empleados()
- def run_query(self, query, parameters=()):
- with conn as con:
- cur = con.cursor()
- result = cur.execute(query, parameters)
- conn.commit()
- return result
- # Get Products from Database
- def get_empleados(self):
- # cleaning Table
- records = self.tree.get_children()
- for element in records:
- self.tree.delete(element)
- cur = conn.cursor(cursor_factory=DictCursor)
- cur.execute("select * from empleado order by nombre DESC" )
- rows = cur.fetchall()
- for row in rows:
- self.tree.insert('', 'end', text=row[1], values=row[2:])
- # User Input Validation
- def validation(self):
- self.emplea = str(self.nombre.get() and self.salario.get() )
- return len(self.emplea) != 0
- def add_empleado(self):
- if self.validation():
- query = 'INSERT INTO empleado VALUES (default, %s,%s)'
- #,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, \
- # %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
- parameters = ( self.nombre.get(), self.salario.get())
- self.run_query(query, parameters)
- self.message['text'] = 'Empleado {} Adiccionado Satisfactoriamente'.format(self.nombre.get())
- self.nombre.delete(0, END)
- self.salario.delete(0, END)
- else:
- self.message['text'] = 'Nombre and Salario son Requridos'
- self.get_empleados()
- def delete_empleado(self):
- #print(self.tree.item(self.tree.selection()))
- self.message['text'] = ''
- try:
- ( self.tree.item(self.tree.selection())) ['text'][0]
- except IndexError as e:
- self.message['text'] = 'Please select a Record'
- return
- self.message['text'] = ''
- self.num = self.tree.item(self.tree.selection()) ['text'] #[0]
- query=("DELETE FROM empleado WHERE nombre=%s")
- self.run_query(query, (self.num,))
- self.message['text'] = 'Record {} deleted Successfully'.format(self.num)
- self.get_empleados()
- def Ver(self):
- print("Estas en la Opcion Ver EMPLEADOS.... ")
- print("")
- cur = conn.cursor(cursor_factory=RealDictCursor)
- cur.execute("""select * from empleado order by id""")
- rows = [cur.fetchall()]
- for row in rows:
- print(tabulate(row, headers="keys", tablefmt='fancy_grid', stralign='left'))
- def edit_empleado(self):
- #print(self.tree.item(self.tree.selection()))
- self.message['text'] = ''
- try:
- self.tree.item(self.tree.selection()) ['text'] [0]
- except IndexError as e:
- self.message['text'] = 'Por Favor Selecione el Empleado'
- return
- nombre = self.tree.item(self.tree.selection())['text'] #[0]
- salario = self.tree.item(self.tree.selection())['values'][0]
- self.edit_wind = Toplevel()
- self.edit_wind.title('Editar Empleados')
- # Old Tipo Documento
- Label(self.edit_wind, text = 'nombre Actual:').grid(row = 0, column = 1)
- Entry(self.edit_wind, textvariable = StringVar(self.edit_wind, value = nombre), state = 'readonly').grid(row = 0, column = 2)
- # New tipo documento
- Label(self.edit_wind, text = 'Nuevo nombre empleado:').grid(row = 0, column = 3)
- new_nombre = Entry(self.edit_wind)
- new_nombre.grid(row = 0, column = 4)
- # Old numero Documento
- Label(self.edit_wind, text = 'salario Actual:').grid(row = 1, column =1)
- Entry(self.edit_wind, textvariable = StringVar(self.edit_wind, value = salario), state = 'readonly').grid(row = 1, column = 2)
- # New numero documento
- Label(self.edit_wind, text = 'Nuevo salario:').grid(row = 1, column = 3)
- new_salario= Entry(self.edit_wind)
- new_salario.grid(row = 1, column = 4)
- # Nuevo primer Nombre
- Label(self.edit_wind, text='================').grid(row=6, column=2)
- Button(self.edit_wind, text='Actualizar',
- command=lambda: self.edit_records(nombre=new_nombre.get(), salario=new_salario.get())).grid(row=4,
- column=2,
- sticky=W)
- self.edit_wind.mainloop()
- def edit_records(self, nombre, salario):
- query=("UPDATE empleado SET nombre=%s, salario=%s") #[nombre, salario]) #WHERE nombre=%s AND salario=%s")
- conn.commit()
- parameters = (nombre, salario)
- self.run_query(query, parameters)
- self.edit_wind.destroy()
- self.message['text'] = 'Empleado {} Actualizado Satisfactoriamente'.format(nombre)
- self.get_empleados()
- if __name__ == '__main__':
- window = root()
- application = Empleado(window)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement