Advertisement
Guest User

buscador

a guest
Apr 8th, 2020
10,675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.82 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk
  3. import pandas as pd
  4. import numpy as np
  5. import textwrap
  6.  
  7. #--------------CLASE PARA LA INTERFAZ--------------------
  8.  
  9. class Main(tk.Tk):  
  10.     def __init__(self):  
  11.         super().__init__()        
  12.  
  13.         self.geometry("980x350")        
  14.         self.title("Buscador de Codigos")        
  15.         self.frame1 = ttk.Frame(self)
  16.         self.buscar_var = tk.StringVar()
  17.         self.buscar_entry = tk.Entry(self.frame1, textvariable = self.buscar_var)
  18.         self.buscar_entry.bind("<Return>",
  19.                                 (lambda event: self.insertarDatos()))
  20.         self.buscar_button = ttk.Button(
  21.             self.frame1, text="Buscar", command=self.insertarDatos)
  22.         self.frame2 = ttk.Frame(self)
  23.  
  24.         if tk.TkVersion == 8.6:
  25.  
  26.             self.style = ttk.Style(self)
  27.  
  28.             def fixed_map(option):
  29.             # Returns the style map for 'option' with any styles starting with
  30.             # ("!disabled", "!selected", ...) filtered out
  31.  
  32.             # style.map() returns an empty list for missing options, so this should
  33.             # be future-safe
  34.                 return [elm for elm in self.style.map("Treeview", query_opt=option)
  35.                         if elm[:2] != ("!disabled", "!selected")]
  36.  
  37.             self.style.map("Treeview", foreground=fixed_map("foreground"), background=fixed_map("background"))
  38.  
  39.         self.style69 = ttk.Style(self.frame2)
  40.         self.style69.configure('Treeview', rowheight=40)
  41.  
  42.         self.treeview = ttk.Treeview(self.frame2, columns=("codigo", "desc", "presentacion", "med_pres"), style="Treeview")
  43.  
  44.         self.treeview.heading("#0", text="Renglon")
  45.         self.treeview.column("#0", minwidth=85, width=85, stretch=0)
  46.         self.treeview.heading("codigo", text="Codigo SIGES")
  47.         self.treeview.column("codigo", minwidth=85, width=85, stretch=0)
  48.         self.treeview.heading("desc", text="Descripción")
  49.         self.treeview.column("desc", minwidth=500, width=500)
  50.         self.treeview.heading("presentacion", text="Presentación")
  51.         self.treeview.column("presentacion", minwidth=100, width=100, stretch=0)
  52.         self.treeview.heading("med_pres", text="Cantidad de Presentación")
  53.         self.treeview.column("med_pres", minwidth=200, width=200, stretch=0)
  54.  
  55.         self.barra_lateral = ttk.Scrollbar(self.treeview, orient="vertical", command=self.treeview.yview)
  56.         self.treeview.configure(yscrollcommand=self.barra_lateral.set)
  57.  
  58.         self.frame1.pack(fill="x", padx=10, pady=(10, 0))
  59.         self.buscar_entry.pack(fill="x", expand=True, side="left")  
  60.         self.buscar_button.pack(side="left", padx=(10, 0))
  61.         self.frame2.pack(fill="both", expand=True, padx=10, pady=10)
  62.         self.treeview.pack(fill="both", expand=True, side="bottom")
  63.  
  64.        
  65.  
  66.  
  67.         self.barra_lateral.pack(side='right', fill='y')
  68.  
  69. #----------------FUNCION HACE LA CONSULTA AL EXCEL SEGUN EL INPUT---------------
  70.  
  71.  
  72.     def insertarDatos(self):
  73.  
  74.         #funcion "wrap" hace que las descripciones con muchos
  75.         #caracteres se muestren en 2 o mas lineas en el treeview
  76.  
  77.         def wrap(string, lenght=80):
  78.             return '\n'.join(textwrap.wrap(string, lenght))
  79.  
  80.        
  81.         self.treeview.delete(*self.treeview.get_children()) #esta linea limpia el treeview
  82.         self.catalogo = pd.read_excel("Insumos2.xlsx") #esta linea lee el archivo excel
  83.         self.catalogo2=self.catalogo[self.catalogo.apply(lambda row: row.astype(str).str.contains(
  84.             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())
  85.                                                                                       #en todas las columnas del excel
  86.  
  87.         self.treeview.tag_configure('celeste', background='#D9E1F4') #etiquetas con los colores de fondo para las filas del treeview
  88.         self.treeview.tag_configure('blanco', background='white')
  89.  
  90.        
  91.         for row_id in range(len(self.catalogo2)):  #for loop para popular el treeview
  92.             if not row_id % 2:
  93.                 self.treeview.insert('','end',text=self.catalogo2.loc[row_id][0], values=(          #condicionales if, para intercalar los colores
  94.                     self.catalogo2.loc[row_id][1], wrap(str(self.catalogo2.loc[row_id][2])),
  95.                     self.catalogo2.loc[row_id][3], self.catalogo2.loc[row_id][4]), tag='celeste')
  96.             else:
  97.                 self.treeview.insert('','end',text=self.catalogo2.loc[row_id][0], values=(
  98.                     self.catalogo2.loc[row_id][1], wrap(str(self.catalogo2.loc[row_id][2])),
  99.                     self.catalogo2.loc[row_id][3], self.catalogo2.loc[row_id][4]), tag='blanco')          
  100.  
  101.        
  102.  
  103.  
  104. if __name__ == "__main__":
  105.     root = Main()  
  106.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement