Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import ttk
- import pandas as pd
- import numpy as np
- import textwrap
- #--------------CLASE PARA LA INTERFAZ--------------------
- class Main(tk.Tk):
- def __init__(self):
- super().__init__()
- self.geometry("980x350")
- self.title("Buscador de Codigos")
- self.frame1 = ttk.Frame(self)
- self.buscar_var = tk.StringVar()
- self.buscar_entry = tk.Entry(self.frame1, textvariable = self.buscar_var)
- self.buscar_entry.bind("<Return>",
- (lambda event: self.insertarDatos()))
- self.buscar_button = ttk.Button(
- self.frame1, text="Buscar", command=self.insertarDatos)
- self.frame2 = ttk.Frame(self)
- if tk.TkVersion == 8.6:
- self.style = ttk.Style(self)
- def fixed_map(option):
- # Returns the style map for 'option' with any styles starting with
- # ("!disabled", "!selected", ...) filtered out
- # style.map() returns an empty list for missing options, so this should
- # be future-safe
- return [elm for elm in self.style.map("Treeview", query_opt=option)
- if elm[:2] != ("!disabled", "!selected")]
- self.style.map("Treeview", foreground=fixed_map("foreground"), background=fixed_map("background"))
- self.style69 = ttk.Style(self.frame2)
- self.style69.configure('Treeview', rowheight=40)
- self.treeview = ttk.Treeview(self.frame2, columns=("codigo", "desc", "presentacion", "med_pres"), style="Treeview")
- self.treeview.heading("#0", text="Renglon")
- self.treeview.column("#0", minwidth=85, width=85, stretch=0)
- self.treeview.heading("codigo", text="Codigo SIGES")
- self.treeview.column("codigo", minwidth=85, width=85, stretch=0)
- self.treeview.heading("desc", text="Descripción")
- self.treeview.column("desc", minwidth=500, width=500)
- self.treeview.heading("presentacion", text="Presentación")
- self.treeview.column("presentacion", minwidth=100, width=100, stretch=0)
- self.treeview.heading("med_pres", text="Cantidad de Presentación")
- self.treeview.column("med_pres", minwidth=200, width=200, stretch=0)
- self.barra_lateral = ttk.Scrollbar(self.treeview, orient="vertical", command=self.treeview.yview)
- self.treeview.configure(yscrollcommand=self.barra_lateral.set)
- self.frame1.pack(fill="x", padx=10, pady=(10, 0))
- self.buscar_entry.pack(fill="x", expand=True, side="left")
- self.buscar_button.pack(side="left", padx=(10, 0))
- self.frame2.pack(fill="both", expand=True, padx=10, pady=10)
- self.treeview.pack(fill="both", expand=True, side="bottom")
- self.barra_lateral.pack(side='right', fill='y')
- #----------------FUNCION HACE LA CONSULTA AL EXCEL SEGUN EL INPUT---------------
- def insertarDatos(self):
- #funcion "wrap" hace que las descripciones con muchos
- #caracteres se muestren en 2 o mas lineas en el treeview
- def wrap(string, lenght=80):
- return '\n'.join(textwrap.wrap(string, lenght))
- self.treeview.delete(*self.treeview.get_children()) #esta linea limpia el treeview
- self.catalogo = pd.read_excel("Insumos2.xlsx") #esta linea lee el archivo excel
- self.catalogo2=self.catalogo[self.catalogo.apply(lambda row: row.astype(str).str.contains(
- self.buscar_var.get(), case=False).any(), axis=1)].reset_index(drop=True) #estas dos lineas es como el "query" que busca el input del usuario (buscar_var.get())
- #en todas las columnas del excel
- self.treeview.tag_configure('celeste', background='#D9E1F4') #etiquetas con los colores de fondo para las filas del treeview
- self.treeview.tag_configure('blanco', background='white')
- for row_id in range(len(self.catalogo2)): #for loop para popular el treeview
- if not row_id % 2:
- self.treeview.insert('','end',text=self.catalogo2.loc[row_id][0], values=( #condicionales if, para intercalar los colores
- self.catalogo2.loc[row_id][1], wrap(str(self.catalogo2.loc[row_id][2])),
- self.catalogo2.loc[row_id][3], self.catalogo2.loc[row_id][4]), tag='celeste')
- else:
- self.treeview.insert('','end',text=self.catalogo2.loc[row_id][0], values=(
- self.catalogo2.loc[row_id][1], wrap(str(self.catalogo2.loc[row_id][2])),
- self.catalogo2.loc[row_id][3], self.catalogo2.loc[row_id][4]), tag='blanco')
- if __name__ == "__main__":
- root = Main()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement