class CustomizedSheet(Tk): def __init__(self, records, headers, title, rows=None): Tk.__init__(self) self.title(title) self.state("zoomed") self.grid_columnconfigure(0, weight=1) self.grid_rowconfigure(0, weight=1) self.data = [["Row {r} Column {c}" for c in range(100)] for r in range(5000)] self.sdem = Sheet(self, width=1000, height=700, align="w", header_align="center", row_index_align="center", show=True, column_width=230, row_index_width=50, data_reference=records, headers=headers) self.sdem.enable_bindings( ("single", "drag_select", "column_drag_and_drop", "row_drag_and_drop", "column_select", "row_select", "column_width_resize", "double_click_column_resize", "row_width_resize", "column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize")) self.sdem.edit_bindings(True) self.sdem.grid(row=0, column=0, sticky="nswe") if rows is not None: for row_no in rows: for col in range(0, len(headers)): self.sdem.highlight_cells(row=row_no, column=col, bg="#EC7063", fg="black")
class demo(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.grid_columnconfigure(0, weight = 1) self.grid_rowconfigure(0, weight = 1) self.frame = tk.Frame(self) self.frame.grid_columnconfigure(0, weight = 1) self.frame.grid_rowconfigure(0, weight = 1) self.sheet = Sheet(self.frame, data = [[f"Row {r}, Column {c}\nnewline1\nnewline2" for c in range(50)] for r in range(500)]) self.sheet.enable_bindings() self.frame.grid(row = 0, column = 0, sticky = "nswe") self.sheet.grid(row = 0, column = 0, sticky = "nswe")
class SheetTable(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.state("zoomed") self.grid_columnconfigure(0, weight=1) self.grid_rowconfigure(0, weight=1) self.sheet_demo = Sheet(self, width=1000, height=700, align="w", header_align="center", row_index_align="center", column_width=180, row_index_width=50, total_rows=5000, total_columns=100, headers=[f"Header {c}" for c in range(100)]) self.sheet_demo.enable_bindings( ("single", "drag_select", "column_drag_and_drop", "row_drag_and_drop", "column_select", "row_select", "column_width_resize", "double_click_column_resize", "row_width_resize", "column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "edit_bindings")) self.sheet_demo.grid(row=0, column=0, sticky="nswe") self.sheet_demo.highlight_cells(row=0, column=0, bg="orange", fg="blue") self.sheet_demo.highlight_cells(row=0, bg="orange", fg="blue", canvas="row_index") self.sheet_demo.highlight_cells(column=0, bg="orange", fg="blue", canvas="header") """_________________________ EXAMPLES _________________________ """ """_____________________________________________________________"""
class TableSheet: def __init__(self, records, headers, window): self.records = records self.sdem = Sheet(window, width=1000, height=500, align="w", header_align="center", row_index_align="center", show=True, column_width=180, row_index_width=50, data_reference=records, headers=headers) self.sdem.enable_bindings( ("single", "drag_select", "column_drag_and_drop", "row_drag_and_drop", "column_select", "row_select", "column_width_resize", "double_click_column_resize", "row_width_resize", "column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize")) self.sdem.edit_bindings(True) self.sdem.pack()
class Aplicacion: def __init__(self): #************************************************************************* #Crea un objeto TK #************************************************************************* self.raiz = Tk() self.raiz.title ("Mantenimiento de Personas") self.raiz.geometry('900x600') #************************************************************************* #crea el menu de la pantalla #************************************************************************* menubar = Menu(self.raiz) self.raiz.config(menu=menubar) filemenu = Menu(menubar, tearoff=0) filemenu.add_command(label="Acerca de..") filemenu.add_separator() filemenu.add_command(label="Salir", command=self.raiz.quit) mantmenu = Menu(menubar, tearoff=0) mantmenu.add_command(label="Personas") mantmenu.add_command(label="Teléfonos") mantmenu.add_command(label="Direcciones") menubar.add_cascade(label="Archivo", menu=filemenu) menubar.add_cascade(label="Mantenimiento", menu=mantmenu) #************************************************************************* #crea un objeto tipo fuenta #************************************************************************* self.fuente = font.Font(weight="bold") #************************************************************************* #se crean atributos de la clase #************************************************************************* self.t_gustos = Gustos.Gustos() #se crea el objeto de dominio para guardar la información self.insertando = True #************************************************************************* #se crean los campos de la pantalla #************************************************************************* #Se coloca un label del titulo self.lb_tituloPantalla = Label(self.raiz, text = "MANTENIMIENTO DE GUSTOS", font = self.fuente) self.lb_tituloPantalla.place(x = 320, y = 20) #colocar por medio de espacion en pixeles de la parte superior de la pantalla considerando un eje x y un eje y #coloca en el formulario el campo y el label de idNombre Gustos self.lb_cedula = Label(self.raiz, text = "id Nombre Gustos:") self.lb_cedula.place(x = 240, y = 60) self.txt_cedula = Entry(self.raiz, textvariable=self.t_gustos.idNombreGustos, justify="right") self.txt_cedula.place(x = 370, y = 60) #coloca en el formulario el campo y el label de descripcion gustos self.lb_nombre = Label(self.raiz, text = "Descripcion de los Gustos:") self.lb_nombre.place(x = 240, y = 90) self.txt_nombre = Entry(self.raiz, textvariable=self.t_gustos.descripcionGustos, justify="right", width=30) self.txt_nombre.place(x = 370, y = 90) #coloca en el formulario el campo y el label de idUsuario _TGustos self.lb_apellido1 = Label(self.raiz, text = "id Usuario_TGustos:") self.lb_apellido1.place(x = 240, y = 120) self.txt_apellido1 = Entry(self.raiz, textvariable=self.t_gustos.idUsuario_TGustos, justify="right", width=30) self.txt_apellido1.place(x = 370, y = 120) #coloca en el formulario el campo y el label de nombre gustos self.lb_apellido2 = Label(self.raiz, text = "Nombre gustos:") self.lb_apellido2.place(x = 240, y = 150) self.txt_apellido2 = Entry(self.raiz, textvariable=self.t_gustos.nombreGustos, justify="right", width=30) self.txt_apellido2.place(x = 370, y = 150) #coloca los botones enviar y borrar self.bt_borrar = Button(self.raiz, text="Limpiar", width=15, command = self.limpiarInformacion) self.bt_borrar.place(x = 370, y = 330) self.bt_enviar = Button(self.raiz, text="Enviar", width=15, command = self.enviarInformacion) self.bt_enviar.place(x = 510, y = 330) #Se coloca un label del informacion self.lb_tituloPantalla = Label(self.raiz, text = "INFORMACIÓN INCLUIDA", font = self.fuente) self.lb_tituloPantalla.place(x = 350, y = 360) #colocar por medio de espacion en pixeles de la parte superior de la pantalla considerando un eje x y un eje y #************************************************************************* #tabla con informacion #************************************************************************* self.sheet = Sheet(self.raiz, page_up_down_select_row = True, #empty_vertical = 0, column_width = 120, startup_select = (0,1,"rows"), #row_height = "4", #default_row_index = "numbers", #default_header = "both", #empty_horizontal = 0, #show_vertical_grid = False, #show_horizontal_grid = False, #auto_resize_default_row_index = False, #header_height = "3", #row_index_width = 100, #align = "center", #header_align = "w", #row_index_align = "w", #data = [[f"Row {r}, Column {c}\nnewline1\nnewline2" for c in range(50)] for r in range(1000)], #to set sheet data at startup headers = ['idNombreGustos', 'nombreGstos', 'DescripcionGustos','idUsuario','nombreGustos'], #row_index = [f"Row {r}\nnewline1\nnewline2" for r in range(2000)], #set_all_heights_and_widths = True, #to fit all cell sizes to text at start up #headers = 0, #to set headers as first row at startup #headers = [f"Column {c}\nnewline1\nnewline2" for c in range(30)], #theme = "light green", #row_index = 0, #to set row_index as first column at startup #total_rows = 2000, #if you want to set empty sheet dimensions at startup #total_columns = 30, #if you want to set empty sheet dimensions at startup height = 195, #height and width arguments are optional width = 720 #For full startup arguments see DOCUMENTATION.md ) #self.sheet.hide("row_index") #self.sheet.hide("header") #self.sheet.hide("top_left") self.sheet.enable_bindings(("single_select", #"single_select" or "toggle_select" "column_select", "row_select", "column_width_resize", "double_click_column_resize", #"row_width_resize", #"column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", "rc_insert_column", "rc_delete_column", "rc_insert_row", "rc_delete_row")) #self.sheet.disable_bindings() #uses the same strings #self.sheet.enable_bindings() self.sheet.place(x = 20, y = 390) #coloca los botones cargar y eliminar self.bt_cargar = Button(self.raiz, text="Cargar", width=15, command = self.cargarInformacion) self.bt_cargar.place(x = 750, y = 385) self.bt_eliminar = Button(self.raiz, text="Eliminar", width=15, command = self.eliminarInformacion) self.bt_eliminar.place(x = 750, y = 425) self.cargarTodaInformacion() #************************************************************************* #se inicial el main loop de la pantalla #************************************************************************* self.raiz.mainloop() #************************************************************************* #Metodo para consultar la información de la base de datos para #cargarla en la tabla #************************************************************************* def cargarTodaInformacion(self): try: self.gustosBo = GustosBO.GustosBO() #se crea un objeto de logica de negocio resultado = self.gustosBo.consultar() self.sheet.set_sheet_data(resultado) except Exception as e: msg.showerror("Error", str(e)) #si se genera algun tipo de error muestra un mensache con dicho error #************************************************************************* #Metodo para cargar informacion #************************************************************************* def cargarInformacion(self): try: datoSeleccionado = self.sheet.get_currently_selected() idNombreGustos = (self.sheet.get_cell_data(datoSeleccionado[0],0)) self.t_gustos.idNombreGustos.set(idNombreGustos) self.gustosBo = GustosBO.GustosBO() #se crea un objeto de logica de negocio self.gustosBo.consultarGustos(self.t_gustos) #se envia a consultar self.insertando = False msg.showinfo("Acción: Consultar el gusto", "La información del gusto ha sido consultada correctamente") # Se muestra el mensaje de que todo esta correcto except Exception as e: msg.showerror("Error", str(e)) #si se genera algun tipo de error muestra un mensache con dicho error #************************************************************************* #Metodo para cargar eliminar la informacion #************************************************************************* def eliminarInformacion(self): try: datoSeleccionado = self.sheet.get_currently_selected() idNombreGustos = (self.sheet.get_cell_data(datoSeleccionado[0],0)) gusto = (self.sheet.get_cell_data(datoSeleccionado[0],1)) resultado = msg.askquestion("Eliminar", "¿Desear eliminar a "+gusto+" de la base de datos?") if resultado == "yes": self.t_gustos.idNombreGustos.set(idNombreGustos) self.gustosBo = GustosBO.GustosBO() #se crea un objeto de logica de negocio self.gustosBo.eliminar(self.t_gustos) #se envia a consultar self.cargarTodaInformacion() self.t_gustos.limpiar() except Exception as e: msg.showerror("Error", str(e)) #si se genera algun tipo de error muestra un mensache con dicho error #************************************************************************* #Metodo para enviar la información a la base de datos #************************************************************************* def enviarInformacion(self): try: self.gustosBo = GustosBO.GustosBO() #se crea un objeto de logica de negocio if(self.insertando == True): self.gustosBo.guardar(self.t_gustos) else: self.gustosBo.modificar(self.t_gustos) self.cargarTodaInformacion() self.t_gustos.limpiar() #se limpia el formulario if(self.insertando == True): msg.showinfo("Acción: Agregar el gusto", "La información de los gustos ha sido incluida correctamente") # Se muestra el mensaje de que todo esta correcto else: msg.showinfo("Acción: Agregar modificar", "La información de los gustos ha sido modificada correctamente") # Se muestra el mensaje de que todo esta correcto except Exception as e: msg.showerror("Error", str(e)) #si se genera algun tipo de error muestra un mensache con dicho error #************************************************************************* #Metodo para limpiar el formulario #************************************************************************* def limpiarInformacion(self): self.t_gustos.limpiar() #llama al metodo de la clase gustos para limpiar los atritudos de la clase self.insertando = True msg.showinfo("Acción del sistema", "La información del formulario ha sido eliminada correctamente") # muestra un mensaje indicando que se limpio el formulario #************************************************************************* #Metodo para mostrar un contro tipo datepicker #************************************************************************* def mostrarDatePicker(self): self.top = Toplevel(self.raiz) self.cal = Calendar(self.top, font="Arial 14", selectmode='day', locale='en_US', cursor="hand", year=2019, month=6, day=16) self.cal.pack(fill="both", expand=True) ttk.Button(self.top, text="Seleccionar").pack()
def __init__(self, customer_id): self.customer_id = customer_id ''' DATA ''' akun = DataAkun() self.akun = akun.getAccountsByCustomerId(customer_id) """ Window Configuration """ self.aroot = Tk() self.window_height = 700 #Window Height self.window_width = 1100 #Window Width self.screen_width = self.aroot.winfo_screenwidth() #Screen Width self.screen_height = self.aroot.winfo_screenheight() #Screen Height self.x_cordinate = int((self.screen_width / 2) - (self.window_width / 2)) self.y_cordinate = int((self.screen_height / 2) - (self.window_height / 2)) self.aroot.geometry("{}x{}+{}+{}".format( self.window_width, self.window_height, self.x_cordinate, 5)) #Implement and Center Window based on Device Screen self.aroot.config(bg='#00bd56') #Window Background self.aroot.overrideredirect(True) #Remove Window Status Bar self.aroot.resizable(False, False) #Disable Resizing Window """ Image Declaration """ imgTambah = PhotoImage(file='tampilan/images/tambah-btn.png') imgHapus = PhotoImage(file='tampilan/images/hapus-btn.png') imgExit = PhotoImage(file='tampilan/images/exit-btn.png') #Dashboard Icon Navbar dashimage = Image.open('tampilan/images/dashboard.png') dashImage = dashimage.resize((38, 38), Image.ANTIALIAS) dashboardIMG = ImageTk.PhotoImage(dashImage) #Account Icon Navbar checkImg = Image.open('tampilan/images/checkingicon.png') chImage = checkImg.resize((35, 35), Image.ANTIALIAS) accountImage = ImageTk.PhotoImage(chImage) #Transaction Icon Navbar saveImg = Image.open('tampilan/images/transfer.png') sImage = saveImg.resize((30, 30), Image.ANTIALIAS) transImage = ImageTk.PhotoImage(sImage) #Logout Icon Navbar logoutImg = Image.open('tampilan/images/logout.png') logImage = logoutImg.resize((30, 30), Image.ANTIALIAS) logoutImage = ImageTk.PhotoImage(logImage) #Dashboard Info Background Icon Navbar cusInfoImg = Image.open('tampilan/images/info-bg.png') cInfoImg = cusInfoImg.resize((180, 180), Image.ANTIALIAS) cIImg = ImageTk.PhotoImage(cInfoImg) #Account Info siIMG = Image.open('tampilan/images/savingicon.png') ssImg = siIMG.resize((80, 80), Image.ANTIALIAS) savIMG = ImageTk.PhotoImage(ssImg) #Balance Info chIMG = Image.open('tampilan/images/checkingicon.png') chsImg = chIMG.resize((80, 80), Image.ANTIALIAS) cheIMG = ImageTk.PhotoImage(chsImg) ############################################################################## ############ SIDEBAR CONTENT ########## ############################################################################## navbarLabel = Label(self.aroot, bg='#e5e5e5', width=30, height=self.window_height) navbarLabel.place(x=0, y=0) #Dashboard Icon Navbar dashboardNavIcon = Label(self.aroot, image=dashboardIMG, bg='#e5e5e5', cursor='hand2') dashboardNavIcon.place(x=15, y=25) dashboardNavIcon.bind("<Button>", self.bindingToDashboard) #Dashboard Label Navbar dashboardNavLabel = Label(self.aroot, text="DASHBOARD", font=('Segoe UI', 16, BOLD), bg='#e5e5e5', fg='#23374d', cursor='hand2') dashboardNavLabel.place(x=55, y=25) dashboardNavLabel.bind("<Button>", self.bindingToDashboard) #Account Icon Navbar accNavIcon = Label(self.aroot, image=accountImage, bg='#e5e5e5', cursor='hand2') accNavIcon.place(x=15, y=80) accNavIcon.bind("<Button>", self.bindingToAccount) #Account Label Navbar accNavLabel = Label(self.aroot, text="ACCOUNT", font=('Segoe UI', 16, BOLD), bg='#e5e5e5', fg='#23374d', cursor='hand2') accNavLabel.place(x=55, y=80) accNavLabel.bind("<Button>", self.bindingToAccount) #Transaction Icon Navbar transNavIcon = Label(self.aroot, image=transImage, bg='#e5e5e5', cursor='hand2') transNavIcon.place(x=15, y=140) transNavIcon.bind("<Button>", self.bindingToTranscation) #Transaction Label Navbar transNavLabel = Label(self.aroot, text="TRANSACTION", font=('Segoe UI', 16, BOLD), bg='#e5e5e5', fg='#23374d', cursor='hand2') transNavLabel.place(x=55, y=140) transNavLabel.bind("<Button>", self.bindingToTranscation) #Logout Icon Navbar logoutNavIcon = Label(self.aroot, image=logoutImage, bg='#e5e5e5', cursor='hand2') logoutNavIcon.place(x=10, y=650) logoutNavIcon.bind("<Button>", self.doLogout) #Logout Label Navbar logoutNavLabel = Label(self.aroot, text="LOGOUT", font=('Segoe UI', 16, BOLD), bg='#e5e5e5', fg='#23374d', cursor='hand2') logoutNavLabel.place(x=50, y=650) logoutNavLabel.bind("<Button>", self.doLogout) ############################################################################## ############ DASHBOARD CONTENT ########## ############################################################################## #Customer Page Title cusTitle = Label(self.aroot, text="Customer's Account", font=('Segoe UI', 20, BOLD), bg='#00bd56', fg='#e5e5e5') cusTitle.place(x=500, y=20) #Add Account Button Property addCButton = Button(self.aroot, image=imgTambah, border=0, bg='#00bd56', activebackground='#00bd56', command=self.tambahAkun, cursor='hand2') addCButton.place(x=960, y=550) headerLabel = ["Account Type", "Balance"] data = [] for raw in self.akun: tmpData = (raw.getType(), raw.getBalance()) data.append(tmpData) #Account Table frame = Frame(self.aroot) frame.grid_columnconfigure(0, weight=1) frame.grid_rowconfigure(0, weight=1) sheet = Sheet( frame, page_up_down_select_row=True, column_width=120, startup_select=(0, 1, "rows"), data= data, # [[f"{data_example[1][1]}" for c in range(4)] for r in range(5)], headers=[f"{c}" for c in headerLabel], theme="light green", height=480, width=700) sheet.enable_bindings( ("single_select", "drag_select", "select_all", "column_drag_and_drop", "row_drag_and_drop", "column_select", "row_select", "column_width_resize", "double_click_column_resize", "row_width_resize", "column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", "rc_insert_column", "rc_delete_column", "rc_insert_row", "rc_delete_row", "copy", "cut", "paste", "delete", "undo")) frame.place(x=250, y=100) sheet.grid(row=0, column=0, sticky="nswe") #Hapus Account Button Property hapusButton = Button(self.aroot, image=imgHapus, border=0, bg='#00bd56', activebackground='#00bd56', command=self.hapusAkun, cursor='hand2') hapusButton.place(x=960, y=600) #Exit Button Property exitButton = Button(self.aroot, image=imgExit, border=0, bg='#00bd56', activebackground='#00bd56', command=self.aroot.destroy, cursor='hand2') exitButton.place(x=970, y=650) self.aroot.mainloop()
class Directorio_F: def __init__(self): #Pantalla self.raiz = Tk() self.raiz.title("Mantenimiento de Factura") self.raiz.geometry('600x730') #Barra menu menubar = Menu(self.raiz) self.raiz.config(menu=menubar) filemenu = Menu(menubar, tearoff=0) filemenu.add_command(label="Salir", command=self.raiz.quit) mantmenu = Menu(menubar, tearoff=0) mantmenu.add_command(label="Clientes", command=self.abrir_C) mantmenu.add_command(label="Articulos", command=self.abrir_A) mantmenu.add_command(label="Proveedores", command=self.abrir_p) mantmenu.add_command(label="Conexion", command=self.abrir_R) menubar.add_cascade(label="Archivo", menu=filemenu) menubar.add_cascade(label="Mantenimiento", menu=mantmenu) #Objeto Factura self.fuente = font.Font(weight="bold") self.factura = Factura.Factura() self.insertando = True #Titulo self.lb_tituloPantalla = Label(self.raiz, text="MANTENIMIENTO DE FACTURA", font=self.fuente) self.lb_tituloPantalla.place(x=190, y=20) #Formulario #ID factura self.lb_cedula = Label(self.raiz, text="ID factura:") self.lb_cedula.place(x=150, y=60) self.txt_cedula = Entry(self.raiz, textvariable=self.factura.PK_N_FACTURA, justify="right") self.txt_cedula.place(x=300, y=60) #Cedula self.lb_nombre = Label(self.raiz, text="Cedula:") self.lb_nombre.place(x=150, y=90) self.txt_nombre = Entry(self.raiz, textvariable=self.factura.FK_CEDULA, justify="right") self.txt_nombre.place(x=300, y=90) #Tiempo self.lb_apellido2 = Label(self.raiz, text="Tiempo utilizado") self.lb_apellido2.place(x=150, y=120) self.txt_apellido2 = Entry(self.raiz, textvariable=self.factura.TIEMPO_USO, justify="right", width=30) self.txt_apellido2.place(x=300, y=120) #Monto self.lb_apellido2 = Label(self.raiz, text="Monto Total:") self.lb_apellido2.place(x=150, y=150) self.txt_apellido2 = Entry(self.raiz, textvariable=self.factura.MONTO, justify="right", width=30) self.txt_apellido2.place(x=300, y=150) #Articulos asociados self.lb_apellido2 = Label(self.raiz, text="ID Articulo asociado:") self.lb_apellido2.place(x=150, y=180) self.txt_apellido2 = Entry(self.raiz, textvariable=self.factura.FK_ID_ART, justify="right") self.txt_apellido2.place(x=300, y=180) #Asociado #Se coloca un label del informacion self.lb_tituloPantalla = Label(self.raiz, text="INFORMACIÓN ASOCIADA", font=self.fuente) self.lb_tituloPantalla.place(x=200, y=270) #Nombre-cedula self.lb_apellido2 = Label(self.raiz, text="Nombre asociado:") self.lb_apellido2.place(x=150, y=310) self.txt_apellido2 = Entry(self.raiz, textvariable=self.factura.CLIENTE, justify="right", state="readonly", width=30) self.txt_apellido2.place(x=300, y=310) #Primer Apellido-cedula self.lb_apellido2 = Label(self.raiz, text="1.ª Apellido asociado:") self.lb_apellido2.place(x=150, y=340) self.txt_apellido2 = Entry(self.raiz, textvariable=self.factura.APELLIDO_1, justify="right", state="readonly", width=30) self.txt_apellido2.place(x=300, y=340) #Segundo Apellido-cedula self.lb_apellido2 = Label(self.raiz, text="2.ª Apellido asociado:") self.lb_apellido2.place(x=150, y=370) self.txt_apellido2 = Entry(self.raiz, textvariable=self.factura.APELLIDO_2, justify="right", state="readonly", width=30) self.txt_apellido2.place(x=300, y=370) #Nombre-articulo self.lb_apellido2 = Label(self.raiz, text="Nombre Articulo:") self.lb_apellido2.place(x=150, y=400) self.txt_apellido2 = Entry(self.raiz, textvariable=self.factura.ARTICULO, justify="right", state="readonly", width=30) self.txt_apellido2.place(x=300, y=400) #Botones #Boton Limpiar self.bt_borrar = Button(self.raiz, text="Limpiar", width=15, command=self.limpiarInformacion) self.bt_borrar.place(x=190, y=220) #Boton Enviar self.bt_enviar = Button(self.raiz, text="Enviar", width=15, command=self.enviarInformacion) self.bt_enviar.place(x=310, y=220) #Boton Cargar self.bt_borrar = Button(self.raiz, text="Cargar", width=15, command=self.cargarInformacion) self.bt_borrar.place(x=430, y=220) #Boton Eliminar self.bt_enviar = Button(self.raiz, text="Eliminar", width=15, command=self.eliminarInformacion) self.bt_enviar.place(x=550, y=220) self.bt_reporte = Button(self.raiz, text="Reporte", width=15, command=self.generarPDFListado) self.bt_reporte.place(x=670, y=220) #Se coloca un label del informacion self.lb_tituloPantalla = Label(self.raiz, text="INFORMACIÓN INCLUIDA", font=self.fuente) self.lb_tituloPantalla.place(x=200, y=440) self.sheet = Sheet(self.raiz, page_up_down_select_row=True, column_width=120, startup_select=(0, 1, "rows"), headers=[ 'Factura', 'Cedula', 'Tiempo uso', 'Monto', 'ID articulo' ], height=170, width=560) self.sheet.enable_bindings( ("single_select", "column_select", "row_select", "column_width_resize", "double_click_column_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", "rc_insert_column", "rc_delete_column", "rc_insert_row", "rc_delete_row")) self.sheet.place(x=20, y=480) self.cargarTodaInformacion() self.raiz.mainloop() def generarPDFListado(self): try: #Crea un objeto para la creación del PDF nombreArchivo = "ListadoPersonas.pdf" rep = reportPDF.Canvas(nombreArchivo) #Agrega el tipo de fuente Arial registerFont(TTFont('Arial', 'ARIAL.ttf')) #Crea el texto en donde se incluye la información textobject = rep.beginText() # Coloca el titulo textobject.setFont('Arial', 16) textobject.setTextOrigin(10, 800) textobject.setFillColor(colors.darkorange) textobject.textLine(text='LISTA DE FACTURA') #Escribe el titulo en el reportes rep.drawText(textobject) #consultar la informacion de la base de datos self.facturaBo = FacturaBO.FacturaBO( ) #se crea un objeto de logica de negocio informacion = self.facturaBo.consultar() #agrega los titulos de la tabla en la información consultada titulos = [ "Factura", "Cedula", "Tiempo uso", "Monto", "ID articulo" ] informacion.insert(0, titulos) #crea el objeto tabla para mostrar la información t = Table(informacion) #Le coloca el color tanto al borde de la tabla como de las celdas t.setStyle( TableStyle([("BOX", (0, 0), (-1, -1), 0.25, colors.black), ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black) ])) #para cambiar el color de las fichas de hace un ciclo según la cantidad de datos #que devuelve la base de datos data_len = len(informacion) for each in range(data_len): if each % 2 == 0: bg_color = colors.whitesmoke else: bg_color = colors.lightgrey if each == 0: #Le aplica un estilo diferente a la tabla t.setStyle( TableStyle([('BACKGROUND', (0, each), (-1, each), colors.orange)])) else: t.setStyle( TableStyle([('BACKGROUND', (0, each), (-1, each), bg_color)])) #acomoda la tabla según el espacio requerido aW = 840 aH = 780 w, h = t.wrap(aW, aH) t.drawOn(rep, 10, aH - h) #Guarda el archivo rep.save() #Abre el archivo desde comandos, puede variar en MacOs es open #subprocess.Popen("open '%s'" % nombreArchivo, shell=True) subprocess.Popen(nombreArchivo, shell=True) #Windows except IOError: msg.showerror("Error", "El archivo ya se encuentra abierto") #Limpiar def limpiarInformacion(self): self.factura.limpiar() msg.showinfo( "Acción del sistema", "La información del formulario ha sido eliminada correctamente") #envia la info def enviarInformacion(self): try: self.facturaBo = FacturaBO.FacturaBO( ) #se crea un objeto de logica de negocio if (self.insertando == True): self.facturaBo.guardar(self.factura) else: self.facturaBo.modificar(self.factura) self.cargarTodaInformacion() self.insertando = True self.factura.limpiar() #se limpia el formulario if (self.insertando == True): msg.showinfo( "Acción: Agregar factura", "La información de la factura ha sido incluida correctamente" ) # Se muestra el mensaje de que todo esta correcto else: msg.showinfo( "Acción: Modificar factura", "La información de la factura ha sido modificada correctamente" ) # Se muestra el mensaje de que todo esta correcto except Exception as e: msg.showerror( "Error", str(e) ) #si se genera algun tipo de error muestra un mensache con dicho error #eliminar la info def eliminarInformacion(self): try: datoSeleccionado = self.sheet.get_currently_selected() factura = (self.sheet.get_cell_data(datoSeleccionado[0], 0)) resultado = msg.askquestion( "Eliminar", "¿Desear eliminar a " + factura + " de la base de datos?") if resultado == "yes": self.factura.PK_N_FACTURA.set(factura) self.facturaBo = FacturaBO.FacturaBO() self.facturaBo.eliminar(self.factura) self.cargarTodaInformacion() self.factura.limpiar() except Exception as e: msg.showerror("Error", str(e)) #cargar toda la info def cargarTodaInformacion(self): try: self.facturaBo = FacturaBO.FacturaBO() resultado = self.facturaBo.consultar() self.sheet.set_sheet_data(resultado) except Exception as e: msg.showerror("Error", str(e)) #selecionado def cargarInformacion(self): try: datoSeleccionado = self.sheet.get_currently_selected() numero = (self.sheet.get_cell_data(datoSeleccionado[0], 0)) self.factura.PK_N_FACTURA.set(numero) self.facturaBo = FacturaBO.FacturaBO() self.facturaBo.consultarFactura(self.factura) self.insertando = False msg.showinfo( "Acción: Consultar factura", "La información de la factura ha sido consultada correctamente" ) except Exception as e: msg.showerror("Error", str(e)) #abrir def abrir_C(self): from mant_Cliente import Directorio_C self.raiz.destroy() Directorio_C() def abrir_A(self): from mant_Articulos import Directorio_A self.raiz.destroy() Directorio_A() def abrir_p(self): from mant_Proveedor import Directorio_P self.raiz.destroy() Directorio_P() def abrir_R(self): from mant_RelacionAP import Conexion_AP self.raiz.destroy() Conexion_AP()
class EmailTab(tk.Frame): def __init__(self, master): self.emailHandler = EmailSheetHandler(FILENAME) tk.Frame.__init__(self, master) self.grid_columnconfigure(0, weight = 1) self.grid_rowconfigure(0, weight = 1) self.frame = ttk.Frame(self, height=450, width=400) self.frame.grid_columnconfigure(0, weight = 1) self.frame.grid_rowconfigure(0, weight = 1) self.sheet = Sheet(self.frame, page_up_down_select_row = True, #empty_vertical = 0, column_width = 300, startup_select = (0,1,"rows"), data=self.populate_sheet(), total_columns = 3, #if you want to set empty sheet dimensions at startup height = 450, #height and width arguments are optional width = 600 #For full startup arguments see DOCUMENTATION.md ) self.sheet.enable_bindings(("single_select", #"single_select" or "toggle_select" "drag_select", #enables shift click selection as well "column_drag_and_drop", "row_drag_and_drop", "column_select", "row_select", "column_width_resize", "double_click_column_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", "edit_cell")) self.sheet.extra_bindings("end_edit_cell", self.end_edit_cell) self.frame.grid(row = 0, column = 0, sticky = "nsw") self.sheet.grid(row = 0, column = 0, sticky = "nw") self.email_button_frame = ttk.Frame(self.frame, padding=(3,3,12,12), borderwidth=5, width=100, heigh=200) self.email_add_button = tk.Button(self.email_button_frame, text="Add email", command=self.add_email) self.email_merge_button = tk.Button(self.email_button_frame, text="Merge email (with file)", command=self.merge_mails) self.email_update_button = tk.Button(self.email_button_frame, text="Update file with table", command=self.email_file_update ) self.email_button_frame.grid(row=0, column=2, sticky="nswe") self.email_add_button.grid(row=1, column=1) self.email_merge_button.grid(row=2, column=1) self.email_update_button.grid(row=3, column=1) # print(self.sheet.get_sheet_data(get_index=0, get_header=0)) def end_edit_cell(self, event): print(event) print(self.sheet.get_cell_data(event[0], event[1 ])) def add_email(self): package = [] AddDialogue(package, "Add Email", "Add Email") print("Rsult: ", package) self.emailHandler.append_to_file(package) self.update_table() def email_file_update(self): self.emailHandler.update_email_file(self.sheet.get_sheet_data()) def update_table(self): data = self.populate_sheet() self.sheet.set_sheet_data(data) def merge_mails(self): file = filedialog.askopenfile().name print(file) # TODO: handle merge print(self.emailHandler.validate(file)) def populate_sheet(self): self.data = [] try: with open(FILENAME, 'r') as f: L = f.readlines() self.data = [i.strip("\n").split(",") for i in L] # for i in self.data: i.append("50") return self.data except FileNotFoundError: return self.data
class AnalyzeStockScreen(Frame): def __init__(self, master=None, session=None): super().__init__(master) self.master = master self.session = session self.analyze_controller = FAnalyzeStockController(session) self.pack(padx=20, pady=20) self.create_widgets() def create_widgets(self): # Khởi tạo tab phân tích chứng khoán ## Phần nhập ngày bắt đầu self.start_date_frame = Frame(self) self.start_date_frame.pack(fill=X) self.start_date_label = Label(self.start_date_frame, text="Start date", width=10) self.start_date_label.pack(side=LEFT, padx=5, pady=5) self.start_date_input = Entry(self.start_date_frame) self.start_date_input.pack(fill=X, padx=5, expand=True) ## Phần nhập ngày kết thúc self.end_date_frame = Frame(self) self.end_date_frame.pack(fill=X) self.end_date_label = Label(self.end_date_frame, text="End date", width=10) self.end_date_label.pack(side=LEFT, padx=5, pady=5) self.end_date_input = Entry(self.end_date_frame) self.end_date_input.pack(fill=X, padx=5, expand=True) ## Phần nút bấm self.stock_submit_frame = Frame(self) self.stock_submit_frame.pack(fill=X, padx=10) ## Phần button phân tích self.stock_submit = Button(self.stock_submit_frame) self.stock_submit["text"] = "Tính" self.stock_submit["command"] = lambda: self.start_calc() self.stock_submit.pack(side=RIGHT) # Phầns button export excel self.export_excel_button = Button(self.stock_submit_frame, state=DISABLED) self.export_excel_button["text"] = "Export file" self.export_excel_button["command"] = lambda: self.export_file_excel() self.export_excel_button.pack(side=RIGHT, padx=20) ## Phần bản tính self.tabel_frame = Frame(self) self.tabel_frame.pack(fill=X, pady=10) self.sheet = Sheet(self.tabel_frame) self.sheet.pack(fill=X) self.header = ("Mã", "Giá đóng nhỏ nhất", "Giá đóng lớn nhất") self.sheet.headers(self.header) ### Cấu hình cho tkinter sheet self.sheet.enable_bindings( ("single_select", "row_select", "column_width_resize", "arrowkeys", "right_click_popup_menu", "rc_select", "rc_insert_row", "rc_delete_row", "copy", "cut", "paste", "delete", "undo", "edit_cell")) def start_calc(self): ## Disable trang export file button self.export_excel_button["state"] = DISABLED datetime_format = "%d/%m/%Y" startDate = datetime.strptime(self.start_date_input.get().strip(), datetime_format) endDate = datetime.strptime(self.end_date_input.get(), datetime_format) self.analyzeStocks = self.analyze_controller.get_analyze_stocks_data( startDate, endDate) ## Tạo data cho tkinter sheet self.data_sheet = [] for anaStock in self.analyzeStocks: self.data_sheet.append([ anaStock.code, anaStock.closedPriceMin, anaStock.closedPriceMax ]) ## Thêm vào tkinter sheet self.sheet.set_sheet_data(self.data_sheet) self.export_excel_button["state"] = NORMAL def export_file_excel(self): ## Tạo bảnh tính excel với tên file là file_excel_<Thời gian tính theo giây> workbook = xlsxwriter.Workbook('file_excel_{}.xlsx'.format( time.time())) worksheet = workbook.add_worksheet() ## Tạo kiểu kiểu chữ bold = workbook.add_format({'bold': True}) ## Phần header cho trang tính excel worksheet.write('A1', 'Mã', bold) worksheet.write('B1', 'Giá đóng nhỏ nhất', bold) worksheet.write('C1', 'Giá đóng lớn nhất', bold) ## Thêm dòng vào bảng tính row = 1 for record in self.analyzeStocks: worksheet.write(row, 0, record.code) worksheet.write(row, 1, record.closedPriceMin) worksheet.write(row, 2, record.closedPriceMax) row += 1 ## Lưu file và đóng bảng tính workbook.close()
class Directorio_C: def __init__(self): #Pantalla self.raiz = Tk() self.raiz.title("Mantenimiento de Clientes") self.raiz.geometry('600x630') #Barra menu menubar = Menu(self.raiz) self.raiz.config(menu=menubar) filemenu = Menu(menubar, tearoff=0) filemenu.add_command(label="Salir", command=self.raiz.quit) mantmenu = Menu(menubar, tearoff=0) mantmenu.add_command(label="Facturas", command=self.abrir_F) mantmenu.add_command(label="Articulos", command=self.abrir_A) mantmenu.add_command(label="Proveedores", command=self.abrir_p) mantmenu.add_command(label="Conexion", command=self.abrir_R) menubar.add_cascade(label="Archivo", menu=filemenu) menubar.add_cascade(label="Mantenimiento", menu=mantmenu) #Objecto cliente self.fuente = font.Font(weight="bold") self.cliente = Cliente.Cliente() self.insertando = True #Titulo self.lb_tituloPantalla = Label(self.raiz, text="MANTENIMIENTO DE CLIENTES", font=self.fuente) self.lb_tituloPantalla.place(x=180, y=20) #Formulario #Cedula self.lb_cedula = Label(self.raiz, text="Cedula:") self.lb_cedula.place(x=100, y=60) self.txt_cedula = Entry(self.raiz, textvariable=self.cliente.PK_CEDULA, justify="right") self.txt_cedula.place(x=230, y=60) #Nombre self.lb_nombre = Label(self.raiz, text="Nombre:") self.lb_nombre.place(x=100, y=90) self.txt_nombre = Entry(self.raiz, textvariable=self.cliente.NOMBRE_C, justify="right", width=30) self.txt_nombre.place(x=230, y=90) #Apellido 1 self.lb_apellido1 = Label(self.raiz, text="Primer apellido:") self.lb_apellido1.place(x=100, y=120) self.txt_apellido1 = Entry(self.raiz, textvariable=self.cliente.APELLIDO_1, justify="right", width=30) self.txt_apellido1.place(x=230, y=120) #Apellido 2 self.lb_apellido2 = Label(self.raiz, text="Segundo apellido:") self.lb_apellido2.place(x=100, y=150) self.txt_apellido2 = Entry(self.raiz, textvariable=self.cliente.APELLIDO_2, justify="right", width=30) self.txt_apellido2.place(x=230, y=150) #Fecha nacimiento self.lb_fec_nacimiento = Label(self.raiz, text="Fecha nacimiento:") self.lb_fec_nacimiento.place(x=100, y=180) self.txt_fechaNacimiento = Entry( self.raiz, textvariable=self.cliente.FECHA_NACIMIENTO, justify="right", width=30, state="readonly") self.txt_fechaNacimiento.place(x=230, y=180) self.bt_mostrarCalendario = Button(self.raiz, text="...", width=3, command=self.mostrarDatePicker) self.bt_mostrarCalendario.place(x=510, y=180) #Direccion self.lb_direccion = Label(self.raiz, text="Direccion:") self.lb_direccion.place(x=100, y=210) self.txt_direccion = Entry(self.raiz, textvariable=self.cliente.DIRECCION, justify="right", width=30) self.txt_direccion.place(x=230, y=210) #Observaciones self.lb_observaciones = Label(self.raiz, text="Observaciones:") self.lb_observaciones.place(x=100, y=240) self.txt_observaciones = Entry(self.raiz, textvariable=self.cliente.OBSERVACIONES, justify="right", width=30) self.txt_observaciones.place(x=230, y=240) #Telefono 1 self.lb_telefono_1 = Label(self.raiz, text="Telefono Principal:") self.lb_telefono_1.place(x=100, y=270) self.txt_telefono_1 = Entry(self.raiz, textvariable=self.cliente.TELEFONO_1, justify="right", width=30) self.txt_telefono_1.place(x=230, y=270) #Telefono 2 self.lb_telefono_2 = Label(self.raiz, text="Telefono segundario:") self.lb_telefono_2.place(x=100, y=300) self.txt_telefono_2 = Entry(self.raiz, textvariable=self.cliente.TELEFONO_2, justify="right", width=30) self.txt_telefono_2.place(x=230, y=300) #Boton Limpiar self.bt_borrar = Button(self.raiz, text="Limpiar", width=15, command=self.limpiarInformacion) self.bt_borrar.place(x=70, y=340) #Boton Enviar self.bt_enviar = Button(self.raiz, text="Enviar", width=15, command=self.enviarInformacion) self.bt_enviar.place(x=190, y=340) #Boton Cargar self.bt_borrar = Button(self.raiz, text="Cargar", width=15, command=self.cargarInformacion) self.bt_borrar.place(x=310, y=340) #Boton Eliminar self.bt_enviar = Button(self.raiz, text="Eliminar", width=15, command=self.eliminarInformacion) self.bt_enviar.place(x=430, y=340) self.bt_reporte = Button(self.raiz, text="Reporte", width=15, command=self.generarPDFListado) self.bt_reporte.place(x=550, y=340) #label del informacion self.lb_tituloPantalla = Label(self.raiz, text="INFORMACIÓN INCLUIDA", font=self.fuente) self.lb_tituloPantalla.place(x=190, y=400) #cuadro excel self.sheet = Sheet(self.raiz, page_up_down_select_row=True, column_width=120, startup_select=(0, 1, "rows"), headers=[ 'Cédula', 'Nombre', 'Primer Ape.', 'Segundo Ape.', 'Fec. Nacimiento', 'Direccion', 'Observaciones', 'Telefono 1', 'Telefono 2' ], height=170, width=560) self.sheet.enable_bindings( ("single_select", "column_select", "row_select", "column_width_resize", "double_click_column_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", "rc_insert_column", "rc_delete_column", "rc_insert_row", "rc_delete_row")) self.sheet.place(x=20, y=440) #toda informacion self.cargarTodaInformacion() #cierre de raiz self.raiz.mainloop() def generarPDFListado(self): try: #Crea un objeto para la creación del PDF nombreArchivo = "ListadoPersonas.pdf" rep = reportPDF.Canvas(nombreArchivo) #Agrega el tipo de fuente Arial registerFont(TTFont('Arial', 'ARIAL.ttf')) #Crea el texto en donde se incluye la información textobject = rep.beginText() # Coloca el titulo textobject.setFont('Arial', 16) textobject.setTextOrigin(10, 800) textobject.setFillColor(colors.darkorange) textobject.textLine(text='LISTA DE CLIENTES') #Escribe el titulo en el reportes rep.drawText(textobject) #consultar la informacion de la base de datos self.clienteBo = ClienteBO.ClienteBO( ) #se crea un objeto de logica de negocio informacion = self.clienteBo.consultar() #agrega los titulos de la tabla en la información consultada titulos = [ "Cédula", "Nombre", "Primer Ape.", "Segundo Ape.", "Fec. Nacimiento", "Direccion", "Observaciones", "Telefono 1", "Telefono 2" ] informacion.insert(0, titulos) #crea el objeto tabla para mostrar la información t = Table(informacion) #Le coloca el color tanto al borde de la tabla como de las celdas t.setStyle( TableStyle([("BOX", (0, 0), (-1, -1), 0.25, colors.black), ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black) ])) #para cambiar el color de las fichas de hace un ciclo según la cantidad de datos #que devuelve la base de datos data_len = len(informacion) for each in range(data_len): if each % 2 == 0: bg_color = colors.whitesmoke else: bg_color = colors.lightgrey if each == 0: #Le aplica un estilo diferente a la tabla t.setStyle( TableStyle([('BACKGROUND', (0, each), (-1, each), colors.orange)])) else: t.setStyle( TableStyle([('BACKGROUND', (0, each), (-1, each), bg_color)])) #acomoda la tabla según el espacio requerido aW = 840 aH = 780 w, h = t.wrap(aW, aH) t.drawOn(rep, 10, aH - h) #Guarda el archivo rep.save() #Abre el archivo desde comandos, puede variar en MacOs es open #subprocess.Popen("open '%s'" % nombreArchivo, shell=True) subprocess.Popen(nombreArchivo, shell=True) #Windows except IOError: msg.showerror("Error", "El archivo ya se encuentra abierto") #calendario def mostrarDatePicker(self): #ventana segundaria self.top = Toplevel(self.raiz) self.cal = Calendar(self.top, font="Arial 14", selectmode='day', locale='en_US', cursor="hand1", year=2019, month=6, day=16) self.cal.pack(fill="both", expand=True) ttk.Button(self.top, text="Seleccionar", command=self.seleccionarFecha).pack() #Selecciona la fecha def seleccionarFecha(self): self.cliente.FECHA_NACIMIENTO.set(self.cal.selection_get()) self.top.destroy() #Limpiar def limpiarInformacion(self): self.cliente.limpiar() msg.showinfo( "Acción del sistema", "La información del formulario ha sido eliminada correctamente") #envia la info def enviarInformacion(self): try: self.clienteBo = ClienteBO.ClienteBO() if (self.insertando == True): self.clienteBo.guardar(self.cliente) else: self.clienteBo.modificar(self.cliente) self.cargarTodaInformacion() self.insertando = True self.cliente.limpiar() if (self.insertando == True): msg.showinfo( "Acción: Agregar cliente", "La información del cliente ha sido incluida correctamente" ) else: msg.showinfo( "Acción: modificar cliente", "La información del cliente ha sido modificada correctamente" ) except Exception as e: msg.showerror("Error", str(e)) #eliminar la info def eliminarInformacion(self): try: datoSeleccionado = self.sheet.get_currently_selected() cedula = (self.sheet.get_cell_data(datoSeleccionado[0], 0)) nombre = (self.sheet.get_cell_data(datoSeleccionado[0], 1)) resultado = msg.askquestion( "Eliminar", "¿Desear eliminar a " + nombre + " de la base de datos?") if resultado == "yes": self.cliente.PK_CEDULA.set(cedula) self.clienteBo = ClienteBO.ClienteBO() self.clienteBo.eliminar(self.cliente) self.cliente.limpiar() except Exception as e: msg.showerror("Error", str(e)) self.cargarTodaInformacion( ) #refrescar la pagina especialmente para llaves foraneas relacionales #cargar toda la info def cargarTodaInformacion(self): try: self.clienteBo = ClienteBO.ClienteBO() resultado = self.clienteBo.consultar() self.sheet.set_sheet_data(resultado) except Exception as e: msg.showerror("Error", str(e)) #selecionado def cargarInformacion(self): try: datoSeleccionado = self.sheet.get_currently_selected() cedula = (self.sheet.get_cell_data(datoSeleccionado[0], 0)) self.cliente.PK_CEDULA.set(cedula) self.clienteBo = ClienteBO.ClienteBO() self.clienteBo.consultarCliente(self.cliente) self.insertando = False msg.showinfo( "Acción: Consultar cliente", "La información del cliente ha sido consultada correctamente") except Exception as e: msg.showerror("Error", str(e)) #abrir def abrir_F(self): from mant_Factura import Directorio_F self.raiz.destroy() Directorio_F() def abrir_A(self): from mant_Articulos import Directorio_A self.raiz.destroy() Directorio_A() def abrir_p(self): from mant_Proveedor import Directorio_P self.raiz.destroy() Directorio_P() def abrir_R(self): from mant_RelacionAP import Conexion_AP self.raiz.destroy() Conexion_AP()
def __init__(self, customer_id, account_id=False): self.customer_id = customer_id """ Window Configuration """ self.transroot = Tk() self.window_height = 700 #Window Height self.window_width = 1100 #Window Width self.screen_width = self.transroot.winfo_screenwidth() #Screen Width self.screen_height = self.transroot.winfo_screenheight() #Screen Height self.x_cordinate = int((self.screen_width/2) - (self.window_width/2)) self.y_cordinate = int((self.screen_height/2) - (self.window_height/2)) self.transroot.geometry("{}x{}+{}+{}".format(self.window_width, self.window_height, self.x_cordinate, 5)) #Implement and Center Window based on Device Screen self.transroot.config(bg='#00bd56') #Window Background self.transroot.overrideredirect(True) #Remove Window Status Bar self.transroot.resizable(False, False) #Disable Resizing Window """ Image Declaration """ imgTambah = PhotoImage(file='tampilan/images/tambah-btn.png') imgTampil = PhotoImage(file='tampilan/images/tampil-btn.png') imgExit = PhotoImage(file='tampilan/images/exit-btn.png') #Dashboard Icon Navbar dashimage = Image.open('tampilan/images/dashboard.png') dashImage = dashimage.resize((38, 38), Image.ANTIALIAS) dashboardIMG = ImageTk.PhotoImage(dashImage) #Account Icon Navbar checkImg = Image.open('tampilan/images/checkingicon.png') chImage = checkImg.resize((35, 35), Image.ANTIALIAS) accountImage = ImageTk.PhotoImage(chImage) #Transaction Icon Navbar saveImg = Image.open('tampilan/images/transfer.png') sImage = saveImg.resize((30, 30), Image.ANTIALIAS) transImage = ImageTk.PhotoImage(sImage) #Logout Icon Navbar logoutImg = Image.open('tampilan/images/logout.png') logImage = logoutImg.resize((30, 30), Image.ANTIALIAS) logoutImage = ImageTk.PhotoImage(logImage) #Dashboard Info Background Icon Navbar cusInfoImg = Image.open('tampilan/images/info-bg.png') cInfoImg = cusInfoImg.resize((180, 180), Image.ANTIALIAS) cIImg = ImageTk.PhotoImage(cInfoImg) #Account Info siIMG = Image.open('tampilan/images/savingicon.png') ssImg = siIMG.resize((80, 80), Image.ANTIALIAS) savIMG = ImageTk.PhotoImage(ssImg) #Balance Info chIMG = Image.open('tampilan/images/checkingicon.png') chsImg = chIMG.resize((80, 80), Image.ANTIALIAS) cheIMG = ImageTk.PhotoImage(chsImg) ############################################################################## ############ SIDEBAR CONTENT ########## ############################################################################## navbarLabel = Label( self.transroot, bg='#e5e5e5', width=30, height=self.window_height ) navbarLabel.place(x=0,y=0) #Dashboard Icon Navbar dashboardNavIcon = Label( self.transroot, image=dashboardIMG, bg='#e5e5e5', cursor='hand2' ) dashboardNavIcon.place(x=15,y=25) dashboardNavIcon.bind("<Button>",self.bindingToDashboard) #Dashboard Label Navbar dashboardNavLabel = Label( self.transroot, text="DASHBOARD", font=( 'Segoe UI', 16, BOLD ), bg='#e5e5e5', fg='#23374d', cursor='hand2' ) dashboardNavLabel.place(x=55, y=25) dashboardNavLabel.bind("<Button>",self.bindingToDashboard) #Account Icon Navbar accNavIcon = Label( self.transroot, image=accountImage, bg='#e5e5e5', cursor='hand2' ) accNavIcon.place(x=15,y=80) accNavIcon.bind("<Button>",self.bindingToAccount) #Account Label Navbar accNavLabel = Label( self.transroot, text="ACCOUNT", font=( 'Segoe UI', 16, BOLD ), bg='#e5e5e5', fg='#23374d', cursor='hand2' ) accNavLabel.place(x=55, y=80) accNavLabel.bind("<Button>",self.bindingToAccount) #Transaction Icon Navbar transNavIcon = Label( self.transroot, image=transImage, bg='#e5e5e5', cursor='hand2' ) transNavIcon.place(x=15,y=140) transNavIcon.bind("<Button>",self.bindingToTranscation) #Transaction Label Navbar transNavLabel = Label( self.transroot, text="TRANSACTION", font=( 'Segoe UI', 16, BOLD ), bg='#e5e5e5', fg='#23374d', cursor='hand2' ) transNavLabel.place(x=55, y=140) transNavLabel.bind("<Button>",self.bindingToTranscation) #Logout Icon Navbar logoutNavIcon = Label( self.transroot, image=logoutImage, bg='#e5e5e5', cursor='hand2' ) logoutNavIcon.place(x=10,y=650) logoutNavIcon.bind("<Button>",self.doLogout) #Logout Label Navbar logoutNavLabel = Label( self.transroot, text="LOGOUT", font=( 'Segoe UI', 16, BOLD ), bg='#e5e5e5', fg='#23374d', cursor='hand2' ) logoutNavLabel.place(x=50, y=650) logoutNavLabel.bind("<Button>",self.doLogout) ############################################################################## ############ DASHBOARD CONTENT ########## ############################################################################## #Customer Page Title cusTitle = Label( self.transroot, text="Customer's Transaction History", font=( 'Segoe UI', 20, BOLD ), bg='#00bd56', fg='#e5e5e5' ) cusTitle.place(x =450, y=20) self.clickedAccount = StringVar() ac = Account() self.accounts = ac.getAccountsByCustomerId(self.customer_id) self.opt = [] self.opt_id = [] account_on_opt = 0 for ac in self.accounts: self.opt.append(str(ac.getType())+str(ac.getId())) self.opt_id.append(ac.getId()) accountOption = self.opt if account_id!=False: optidx = self.opt_id.index(account_id) else: optidx = 0 self.clickedAccount.set(self.opt[optidx]) # Create Dropdown menu accountType = OptionMenu( self.transroot, self.clickedAccount , *accountOption) accountType.config( width=53, height=2, font=( 'normal', 10, BOLD ) ) accountType.place(x=280,y=85) #Show Account Button Property showButton = Button( self.transroot, image=imgTampil, border=0, bg='#00bd56', activebackground='#00bd56', command=self.showTransaction, cursor='hand2' ) showButton.place(x=710, y=80) #Add Account Button Property addCButton = Button( self.transroot, image=imgTambah, border=0, bg='#00bd56', activebackground='#00bd56', command=self.addTransaction, cursor='hand2' ) addCButton.place(x=850, y=80) if account_id != False: t = DataTransaction() transactions = t.getTransactionsByAccountId(account_id) headerLabel = ["Account Id","Date/Time","Transaction Type","Amount", "Note"] if transactions != []: datas = [(str(dt.getAccountId()), str(dt.getTime()), str(dt.getType()), str(dt.getAmount()), str(dt.getNote())) for dt in transactions] else: datas = [] #Account Table frame = Frame(self.transroot) frame.grid_columnconfigure(0, weight = 1) frame.grid_rowconfigure(0, weight = 1) sheet = Sheet(frame, page_up_down_select_row = True, column_width = 120, startup_select = (0,1,"rows"), data = datas, headers = [f"{c}" for c in headerLabel], theme = "light green", height = 480, width = 700 ) sheet.enable_bindings(( "single_select", "drag_select", "select_all", "column_drag_and_drop", "row_drag_and_drop", "column_select", "row_select", "column_width_resize", "double_click_column_resize", "row_width_resize", "column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", "rc_insert_column", "rc_delete_column", "rc_insert_row", "rc_delete_row", "copy", "cut", "paste", "delete", "undo" )) frame.place(x=280,y=150) sheet.grid(row = 0, column = 0, sticky = "nswe") #Exit Button Property exitButton = Button( self.transroot, image=imgExit, border=0, bg='#00bd56', activebackground='#00bd56', command=self.transroot.destroy, cursor='hand2' ) exitButton.place(x=980, y=650) self.transroot.mainloop()
class PageDiary(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) self.controller = controller topFrame = tk.Frame(self) self.btnMainPage = tk.Button(topFrame, text="主页面", command=self.on_btn_main_page).pack( side=tk.LEFT, padx=4) # 日期选择 # Calendar((x, y), 'ur').selection() 获取日期,x,y为点坐标 date_start_gain = lambda: [ self.str_date.set(date) for date in [CalendarCustom(None, 'ur').selection()] if date ] tk.Button(topFrame, text='日期:', command=date_start_gain).pack(side=tk.LEFT, padx=4) self.str_date = tk.StringVar() ttk.Entry(topFrame, textvariable=self.str_date, width=12).pack(side=tk.LEFT) today = datetime.date.today() self.str_date.set(today) tk.Label(topFrame, text="代码:").pack(side=tk.LEFT) self.strCode = tk.StringVar(value='') ttk.Entry(topFrame, width=10, textvariable=self.strCode).pack(side=tk.LEFT, padx=4) tk.Label(topFrame, text="名称:").pack(side=tk.LEFT) self.strName = tk.StringVar(value='') ttk.Entry(topFrame, width=10, textvariable=self.strName).pack(side=tk.LEFT, padx=4) tk.Label(topFrame, text="操作时间:").pack(side=tk.LEFT) self.strTime = tk.StringVar(value='') ttk.Entry(topFrame, width=8, textvariable=self.strTime).pack(side=tk.LEFT, padx=4) # 操作类型 self.operateTypeStr = tk.StringVar() cmbOperateType = ttk.Combobox(topFrame, width=8, textvariable=self.operateTypeStr, state='readonly') # Adding combobox drop down list cmbOperateType['values'] = ('买入', '卖出') cmbOperateType.current(0) cmbOperateType.pack(side=tk.LEFT, padx=4) cmbOperateType.bind("<<ComboboxSelected>>", self.on_cmb_operate_type_select) topFrame2 = tk.Frame(self) tk.Label(topFrame2, text="价格:").pack(side=tk.LEFT, padx=4, pady=2) self.strPrice = tk.StringVar(value='') ttk.Entry(topFrame2, width=8, textvariable=self.strPrice).pack(side=tk.LEFT, padx=4) tk.Label(topFrame2, text="数量:").pack(side=tk.LEFT) self.strAmount = tk.StringVar(value='') ttk.Entry(topFrame2, width=8, textvariable=self.strAmount).pack(side=tk.LEFT, padx=4) tk.Label(topFrame2, text="目标价位:").pack(side=tk.LEFT) self.strTargetPrice = tk.StringVar(value='') ttk.Entry(topFrame2, width=8, textvariable=self.strTargetPrice).pack(side=tk.LEFT, padx=4) tk.Label(topFrame2, text="止损价位:").pack(side=tk.LEFT) self.strStopPrice = tk.StringVar(value='') ttk.Entry(topFrame2, width=8, textvariable=self.strStopPrice).pack(side=tk.LEFT, padx=4) tk.Label(topFrame2, text="收益比:").pack(side=tk.LEFT) self.strProfitRate = tk.StringVar(value='') ttk.Entry(topFrame2, width=8, textvariable=self.strProfitRate).pack(side=tk.LEFT, padx=4) tk.Label(topFrame, text="盈利:").pack(side=tk.LEFT) self.strProfit = tk.StringVar(value='') ttk.Entry(topFrame, width=8, textvariable=self.strProfit).pack(side=tk.LEFT, padx=4) tk.Label(topFrame, text="备注:").pack(side=tk.LEFT) self.text = tk.Text(topFrame, width=50, height=4) self.text.pack(side=tk.LEFT, padx=4) tk.Button(topFrame2, text="添加", width=10, command=self.on_btn_add).pack(side=tk.LEFT, padx=4) topFrame.pack(side=tk.TOP, fill=tk.BOTH) topFrame2.pack(side=tk.TOP, fill=tk.BOTH) # 列表框 self.frameReport = tk.Frame(self) self.sheet = Sheet(self.frameReport) self.sheet.enable_bindings(( "single_select", # "single_select" or "toggle_select" "drag_select", # enables shift click selection as well "column_drag_and_drop", "row_drag_and_drop", "column_select", "row_select", "column_width_resize", "double_click_column_resize", "row_width_resize", "column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", "rc_insert_column", "rc_delete_column", "rc_insert_row", "rc_delete_row", "hide_columns", "copy", "cut", "paste", "delete", "undo", "edit_cell")) self.sheet.pack(side=tk.TOP, fill=tk.BOTH, expand=1) self.frameReport.pack(side=tk.TOP, fill=tk.BOTH, expand=1, pady=4) self.sheet.headers([ '日期', '编码', '名称', '操作时间', '操作方向', '价格', '数量', '目标价位', '止损', '收益比', '盈利', '备注' ]) self.sheet.refresh() def on_btn_main_page(self): self.controller.show_frame('PageMain') pass def on_btn_add(self): self.sheet.insert_row(values=(self.str_date.get(), self.strCode.get(), self.strName.get(), self.strTime.get(), self.operateTypeStr.get(), self.strPrice.get(), self.strAmount.get(), self.strTargetPrice.get(), self.strStopPrice.get(), self.strProfitRate.get(), self.strProfit.get(), self.text.get('0.0', tk.END))) self.sheet.refresh() pass def on_cmb_operate_type_select(self, event): if not self.operateTypeStr.get(): pass # self.nameEntered.delete(0, tk.END) # self.nameEntered.insert(0, self.measureTypeStr.get()) pass def tree_solution_selected(self, selected): print('tree_solution_selected items:', selected) pass
class Directorio_A: def __init__(self): #Pantalla self.raiz = Tk() self.raiz.title("Mantenimiento de Articulos") self.raiz.geometry('680x520') #Barra menu menubar = Menu(self.raiz) self.raiz.config(menu=menubar) filemenu = Menu(menubar, tearoff=0) filemenu.add_command(label="Salir", command=self.raiz.quit) mantmenu = Menu(menubar, tearoff=0) mantmenu.add_command(label="Proveedor", command=self.abrir_P) mantmenu.add_command(label="Clientes", command=self.abrir_C) mantmenu.add_command(label="Facturas", command=self.abrir_F) mantmenu.add_command(label="Conexion", command=self.abrir_R) menubar.add_cascade(label="Archivo", menu=filemenu) menubar.add_cascade(label="Mantenimiento", menu=mantmenu) #Objecto Articulo self.fuente = font.Font(weight="bold") self.articulo = Articulos.Articulo() self.insertando = True #Titulo self.lb_tituloPantalla = Label(self.raiz, text="MANTENIMIENTO DE ARTICULOS", font=self.fuente) self.lb_tituloPantalla.place(x=180, y=20) #Formulario #ID articulo self.lb_cedula = Label(self.raiz, text="ID articulo:") self.lb_cedula.place(x=100, y=60) self.txt_cedula = Entry(self.raiz, textvariable=self.articulo.PK_ID_ART, justify="right") self.txt_cedula.place(x=230, y=60) #Nombre self.lb_nombre = Label(self.raiz, text="nombre del articulo:") self.lb_nombre.place(x=100, y=90) self.txt_nombre = Entry(self.raiz, textvariable=self.articulo.NOMBRE, justify="right", width=30) self.txt_nombre.place(x=230, y=90) #Cantidad existente self.lb_apellido1 = Label(self.raiz, text="Cantidad existente:") self.lb_apellido1.place(x=100, y=120) self.txt_apellido1 = Entry(self.raiz, textvariable=self.articulo.CANT_EXI, justify="right") self.txt_apellido1.place(x=230, y=120) #descripcion self.lb_apellido2 = Label(self.raiz, text="Descripcion:") self.lb_apellido2.place(x=100, y=150) self.txt_apellido2 = Entry(self.raiz, textvariable=self.articulo.DESCRIPCION, justify="right", width=30) self.txt_apellido2.place(x=230, y=150) #Precio unitario self.lb_apellido1 = Label(self.raiz, text="Precio unitario:") self.lb_apellido1.place(x=100, y=180) self.txt_apellido1 = Entry(self.raiz, textvariable=self.articulo.PRECIO_UN, justify="right") self.txt_apellido1.place(x=230, y=180) #Boton Limpiar self.bt_borrar = Button(self.raiz, text="Limpiar", width=15) self.bt_borrar.place(x=70, y=220) #Boton Enviar self.bt_enviar = Button(self.raiz, text="Enviar", width=15, command=self.enviarInformacion) self.bt_enviar.place(x=190, y=220) #Boton Cargar self.bt_borrar = Button(self.raiz, text="Cargar", width=15, command=self.cargarInformacion) self.bt_borrar.place(x=310, y=220) #Boton Eliminar self.bt_enviar = Button(self.raiz, text="Eliminar", width=15, command=self.eliminarInformacion) self.bt_enviar.place(x=430, y=220) self.bt_reporte = Button(self.raiz, text="Reporte", width=15, command=self.generarPDFListado) self.bt_reporte.place(x=550, y=340) #Se coloca un label del informacion self.lb_tituloPantalla = Label(self.raiz, text="INFORMACIÓN INCLUIDA", font=self.fuente) self.lb_tituloPantalla.place(x=190, y=275) self.sheet = Sheet(self.raiz, page_up_down_select_row=True, column_width=120, startup_select=(0, 1, "rows"), headers=[ 'ID articulo', 'Nombre', 'Cantidad', 'Descripcion', 'Preio Unitario' ], height=170, width=560) #hoja excel self.sheet.enable_bindings( ("single_select", "column_select", "row_select", "column_width_resize", "double_click_column_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", "rc_insert_column", "rc_delete_column", "rc_insert_row", "rc_delete_row")) self.sheet.place(x=20, y=310) #toda informacion self.cargarTodaInformacion() self.raiz.mainloop() def generarPDFListado(self): try: #Crea un objeto para la creación del PDF nombreArchivo = "ListadoArticulos.pdf" rep = reportPDF.Canvas(nombreArchivo) #Agrega el tipo de fuente Arial registerFont(TTFont('Arial', 'ARIAL.ttf')) #Crea el texto en donde se incluye la información textobject = rep.beginText() # Coloca el titulo textobject.setFont('Arial', 16) textobject.setTextOrigin(10, 800) textobject.setFillColor(colors.darkorange) textobject.textLine(text='LISTA DE ARTICULOS') #Escribe el titulo en el reportes rep.drawText(textobject) #consultar la informacion de la base de datos self.articuloBo = ArticuloBO.ArticuloBO( ) #se crea un objeto de logica de negocio informacion = self.articuloBo.consultar() #agrega los titulos de la tabla en la información consultada titulos = [ "ID articulo", "Nombre del articulo", "Descripcion", "Cant.Existente", "Precio Unitario" ] informacion.insert(0, titulos) #crea el objeto tabla para mostrar la información t = Table(informacion) #Le coloca el color tanto al borde de la tabla como de las celdas t.setStyle( TableStyle([("BOX", (0, 0), (-1, -1), 0.25, colors.black), ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black) ])) #para cambiar el color de las fichas de hace un ciclo según la cantidad de datos #que devuelve la base de datos data_len = len(informacion) for each in range(data_len): if each % 2 == 0: bg_color = colors.whitesmoke else: bg_color = colors.lightgrey if each == 0: #Le aplica un estilo diferente a la tabla t.setStyle( TableStyle([('BACKGROUND', (0, each), (-1, each), colors.orange)])) else: t.setStyle( TableStyle([('BACKGROUND', (0, each), (-1, each), bg_color)])) #acomoda la tabla según el espacio requerido aW = 840 aH = 780 w, h = t.wrap(aW, aH) t.drawOn(rep, 10, aH - h) #Guarda el archivo rep.save() #Abre el archivo desde comandos, puede variar en MacOs es open #subprocess.Popen("open '%s'" % nombreArchivo, shell=True) subprocess.Popen(nombreArchivo, shell=True) #Windows except IOError: msg.showerror("Error", "El archivo ya se encuentra abierto") #Limpiar def limpiarInformacion(self): self.articulo.limpiar() msg.showinfo( "Acción del sistema", "La información del formulario ha sido eliminada correctamente") #envia la info def enviarInformacion(self): try: self.articuloBo = ArticuloBO.ArticuloBO() if (self.insertando == True): self.articuloBo.guardar(self.articulo) else: self.articuloBo.modificar(self.articulo) self.cargarTodaInformacion() self.insertando = True self.articulo.limpiar() if (self.insertando == True): msg.showinfo( "Acción: Agregar articulo", "La información del articulo ha sido incluida correctamente" ) else: msg.showinfo( "Acción: modificar articulo", "La información del articulo ha sido modificada correctamente" ) except Exception as e: msg.showerror("Error", str(e)) #eliminar la info def eliminarInformacion(self): try: datoSeleccionado = self.sheet.get_currently_selected() idarticulo = (self.sheet.get_cell_data(datoSeleccionado[0], 0)) nombre = (self.sheet.get_cell_data(datoSeleccionado[0], 1)) resultado = msg.askquestion( "Eliminar", "¿Desear eliminar a " + nombre + " de la base de datos?") if resultado == "yes": self.articulo.PK_ID_ART.set(idarticulo) self.articuloBo = ArticuloBO.ArticuloBO() self.articuloBo.eliminar(self.articulo) self.cargarTodaInformacion() self.articulo.limpiar() except Exception as e: msg.showerror("Error", str(e)) #cargar toda la info def cargarTodaInformacion(self): try: self.articuloBo = ArticuloBO.ArticuloBO() resultado = self.articuloBo.consultar() self.sheet.set_sheet_data(resultado) except Exception as e: msg.showerror("Error", str(e)) #selecionado def cargarInformacion(self): try: datoSeleccionado = self.sheet.get_currently_selected() idarticulo = (self.sheet.get_cell_data(datoSeleccionado[0], 0)) self.articulo.PK_ID_ART.set(idarticulo) self.articuloBo = ArticuloBO.ArticuloBO() self.articuloBo.consultarArticulo(self.articulo) self.insertando = False msg.showinfo( "Acción: Consultar proveedor", "La información del proveedor ha sido consultada correctamente" ) except Exception as e: msg.showerror("Error", str(e)) #abrir def abrir_P(self): from mant_Proveedor import Directorio_P self.raiz.destroy() Directorio_P() def abrir_C(self): from mant_Clientes import Directorio_C self.raiz.destroy() Directorio_C() def abrir_F(self): from mant_Factura import Directorio_F self.raiz.destroy() Directorio_F() def abrir_R(self): from mant_RelacionAP import Conexion_AP self.raiz.destroy() Conexion_AP() self.raiz.mainloop()
class demo(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.data = [["Average Motor Stator Current", 0], ["Average Motor Phase Voltage", 0], ["Target Torque", 0], ["Motor Actual Torque ", 0], ["Steering Angle", 0], ["Proportional Speed Limit ", 0], ["Motor RPM ", 0], ["Calculated Battery Current ", 0], ["Controller Capacitor Voltage", 0], ["Throttle input ", 0], ["Motor Temp ", 0], ["Controller Temperature ", 0], ["Distance travelled ", 0], ["Forward switch ", 0], ["Reverse Switch ", 0], ["Seat Switch ", 0]] self.grid_columnconfigure(0, weight=1) self.grid_rowconfigure(0, weight=1) self.frame = tk.Frame(self) self.frame.grid_columnconfigure(0, weight=1) self.frame.grid_rowconfigure(0, weight=1) self.sheet = Sheet( self.frame, page_up_down_select_row=True, #empty_vertical = 0, column_width=150, startup_select=(0, 1, "rows"), data=self.data, theme="black", height=400, #height and width arguments are optional width=360, #For full startup arguments see DOCUMENTATION.md ) self.sheet.enable_bindings(( "single_select", #"single_select" or "toggle_select" "drag_select", #enables shift click selection as well "column_drag_and_drop", "row_drag_and_drop", "column_select", "row_select", "column_width_resize", "double_click_column_resize", # "row_width_resize", # "column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", "rc_insert_column", "rc_delete_column", "rc_insert_row", "rc_delete_row", "hide_columns", "copy", "cut", "paste", "delete", "undo", "edit_cell")) self.frame.grid(row=0, column=0, sticky="nswe") self.sheet.grid(row=0, column=0, sticky="nswe") self.sheet.headers( (f"Header {c}" for c in range(2))) #any iterable works self.sheet.headers("CAN Data", 0) self.sheet.headers("CAN Values", 1) def all_extra_bindings(self, event): print(event) def begin_edit_cell(self, event): print(event) # event[2] is keystroke return event[ 2] # return value is the text to be put into cell edit window def end_edit_cell(self, event): print(event) def window_resized(self, event): pass #print (event) def mouse_motion(self, event): region = self.sheet.identify_region(event) row = self.sheet.identify_row(event, allow_end=False) column = self.sheet.identify_column(event, allow_end=False) print(region, row, column) def deselect(self, event): print(event, self.sheet.get_selected_cells()) def rc(self, event): print(event) def cell_select(self, response): #print (response) pass def shift_select_cells(self, response): print(response) def drag_select_cells(self, response): pass #print (response) def ctrl_a(self, response): print(response) def row_select(self, response): print(response) def shift_select_rows(self, response): print(response) def drag_select_rows(self, response): pass #print (response) def column_select(self, response): print(response) #for i in range(50): # self.sheet.create_dropdown(i, response[1], values=[f"{i}" for i in range(200)], set_value="100", # destroy_on_select = False, destroy_on_leave = False, see = False) #print (self.sheet.get_cell_data(0, 0)) #self.sheet.refresh() def shift_select_columns(self, response): print(response) def drag_select_columns(self, response): pass # print (response) def null_print(self): print("NULL Printing\n")
class Content: def __init__(self, app: tk.Tk, login: LoginForm, toolbar: Toolbar, menubar: Menubar, impartus: Impartus): self.app = app self.login = login self.toolbar = toolbar self.menubar = menubar self.impartus = impartus conf = Config.load(ConfigType.IMPARTUS) self.content_font = conf.get('content_font').get(platform.system()) self.content_font_size = conf.get('content_font_size') self.header_font = conf.get('header_font').get(platform.system()) self.header_font_size = conf.get('header_font_size') self.frame_content = None self.sheet = None self.videos = None self.video_slide_mapping = None self.expected_real_paths_differ = False self.all_captions_found = True self.offline_video_ttid_mapping = None # sort options self.sort_by = None self.sort_order = None # threads for downloading videos / slides. self.threads = None self.logger = logging.getLogger(self.__class__.__name__) def _init_content(self): if self.frame_content: self.frame_content.destroy() self.frame_content = self.add_content_frame(self.app) self.sheet = Sheet( self.frame_content, header_font=(self.header_font, self.header_font_size, "bold"), font=(self.content_font, self.content_font_size, "normal"), align='w', row_height="1", # str value for row height in number of lines. row_index_align="w", auto_resize_default_row_index=False, row_index_width=40, header_align='center', empty_horizontal=0, empty_vertical=0, ) self.sheet.enable_bindings(( "single_select", "column_select", "column_width_resize", "double_click_column_resize", "edit_cell", "copy", )) self.sheet.grid(row=0, column=0, sticky='nsew') self.set_headers() self.set_display_columns() self.frame_content.columnconfigure(0, weight=1) self.frame_content.rowconfigure(0, weight=1) self.sheet.extra_bindings('column_select', self.sort_table) self.sheet.extra_bindings('cell_select', self.on_click_button_handler) # sort options self.sort_by = None self.sort_order = None # threads for downloading videos / slides. self.threads = dict() def add_content_frame(self, anchor) -> tk.Frame: # noqa frame_content = tk.Frame(anchor, padx=0, pady=0) frame_content.grid(row=2, column=0, sticky='nsew') return frame_content def set_headers(self, sort_by=None, sort_order=None): """ Set the table headers. """ # set column title to reflect sort status headers = list() for name, value in Columns.display_columns.items(): if value.get('sortable'): if name == sort_by: sort_icon = Icons.SORT_DESC if sort_order == 'desc' else Icons.SORT_ASC else: sort_icon = Icons.UNSORTED text = '{} {}'.format(value['display_name'], sort_icon) else: text = value['display_name'] if value.get('editable'): text = '{} {}'.format(Icons.EDITABLE, text) headers.append(text) self.sheet.headers(headers) def set_display_widgets(self): """ Create the table/sheet. Fill in the data for table content, Set the buttons and their states. """ self.fetch_content() self.fill_content() def on_click_button_handler(self, args): """ On click handler for all the buttons, calls the corresponding function as defined by self.button_columns """ (event, row, col) = args real_col = self.get_real_col(col) self.sheet.highlight_rows(rows=[row], redraw=False) self.sheet.highlight_columns(columns=[col], redraw=False) # is subject field col_name = Columns.column_names[real_col] if Columns.all_columns[col_name].get('editable'): old_value = self.sheet.get_cell_data(row, real_col) self.sheet.create_text_editor(row=row, column=real_col, text=old_value, set_data_ref_on_destroy=False, binding=partial( self.end_edit_cell, old_value)) # not a button. if Columns.all_columns[col_name].get('type') != 'button': self.sheet.refresh() return state_button_col_name, state_button_col_num = self.get_state_button( col_name) state = self.sheet.get_cell_data(row, state_button_col_num) if state == 'False': # data read from sheet is all string. self.sheet.refresh() return # disable the button if it is one of the Download buttons, to prevent a re-download. if col_name == 'download_video': self.sheet.set_cell_data(row, real_col, Icons.PAUSE_DOWNLOAD, redraw=False) elif col_name == 'download_slides': cs = Config.load( ConfigType.COLORSCHEMES)[Variables().colorscheme_var().get()] self.disable_button(row, real_col, cs) func_name = Columns.all_columns[col_name]['function'] getattr(self, func_name)(row, real_col) def get_state_button(self, button_name): # noqa if Columns.all_columns[button_name].get('state'): state_col_name = Columns.all_columns[button_name].get('state') state_col_number = Columns.column_names.index(state_col_name) return state_col_name, state_col_number else: return None, None def end_edit_cell(self, old_value, event=None): row, col = (event[0], event[1]) new_value = self.sheet.get_text_editor_value( event, r=row, c=col, set_data_ref_on_destroy=True, move_down=False, redraw=True, recreate=True) # empty value or escape pressed. if not new_value or new_value == '': return # no changes made. if old_value == new_value: return col_name = Columns.column_names[self.get_real_col(col)] columns_item = Columns.data_columns[col_name] orig_values_col_name = columns_item.get('original_values_col') original_value = self.sheet.get_cell_data( row, Columns.column_names.index(orig_values_col_name)) for i, data in enumerate( self.sheet.get_column_data( Columns.column_names.index(orig_values_col_name))): if data == original_value: self.sheet.set_cell_data(i, col, new_value) self.expected_real_paths_differ = True Mappings.update_mappings(orig_values_col_name, original_value, new_value) self.reset_column_sizes() self.sheet.deselect(row=row, column=col, redraw=False) self.sheet.refresh() if self.expected_real_paths_differ or not self.all_captions_found: self.toolbar.auto_organize_button.config(state='normal') self.menubar.actions_menu.entryconfig(Labels.AUTO_ORGANIZE, state='normal') def reset_column_sizes(self): """ Adjust column sizes after data has been filled. """ # resize cells self.sheet.set_all_column_widths() # reset column widths to fill the screen pad = 5 column_widths = self.sheet.get_column_widths() table_width = self.sheet.RI.current_width + sum(column_widths) + len( column_widths) + pad diff_width = self.frame_content.winfo_width() - table_width # adjust extra width only to top N data columns n = 3 column_states = [ v.get() for v in Variables().display_columns_vars().values() ] count = 0 for k, v in enumerate(column_states): if Columns.column_names[k] == 'downloaded': break count += v # range(0..count) is all data columns. data_col_widths = {k: v for k, v in enumerate(column_widths[:count])} top_n_cols = sorted(data_col_widths, key=data_col_widths.get, reverse=True)[:n] for i in top_n_cols: self.sheet.column_width(i, column_widths[i] + diff_width // n) def get_index(self, row): """ Find the values stored in the hidden column named 'Index', given a row record. In case the row value has been updated due to sorting the table, Index field helps identify the new location of the associated record. """ # find where is the Index column index_col = Columns.column_names.index('index') # original row value as per the index column return int(self.sheet.get_cell_data(row, index_col)) def get_row_after_sort(self, index_value): # find the new correct location of the row_index col_index = Columns.column_names.index('index') col_data = self.sheet.get_column_data(col_index) return col_data.index(str(index_value)) def progress_bar_text(self, value, processed=False): """ return progress bar text, calls the unicode/ascii implementation. """ conf = Config.load(ConfigType.IMPARTUS) if conf.get('progress_bar') == 'unicode': text = self.progress_bar_text_unicode(value) else: text = self.progress_bar_text_ascii(value) pad = ' ' * 2 if 0 < value < 100: percent_text = '{:2d}%'.format(value) status = percent_text elif value == 0: status = '{}{}{}'.format(pad, Icons.VIDEO_NOT_DOWNLOADED, pad) else: # 100 % if processed: status = '{}{}{}'.format(pad, Icons.VIDEO_DOWNLOADED, pad) else: status = '{}{}{}'.format(pad, Icons.VIDEO_PROCESSING, pad) return '{} {}{}'.format(text, status, pad) def progress_bar_text_ascii(self, value): # noqa """ progress bar implementation with ascii characters. """ bars = 50 ascii_space = " " if value > 0: progress_text = '{}'.format('❘' * (value * bars // 100)) empty_text = '{}'.format(ascii_space * (bars - len(progress_text))) full_text = '{}{} '.format(progress_text, empty_text) else: full_text = '{}'.format(ascii_space * bars) return full_text def progress_bar_text_unicode(self, value): # noqa """ progress bar implementation with unicode blocks. """ chars = ['▏', '▎', '▍', '▌', '▋', '▊', '▉', '█'] # 1 full unicode block = 8 percent values # => 13 unicode blocks needed to represent counter 100. unicode_space = ' ' if value > 0: # progress_text: n characters, empty_text: 13-n characters progress_text = '{}{}'.format(chars[-1] * (value // 8), chars[value % 8]) empty_text = '{}'.format(unicode_space * (13 - len(progress_text))) full_text = '{}{}'.format(progress_text, empty_text) else: # all 13 unicode whitespace. full_text = '{} '.format(unicode_space * 13) return full_text def progress_bar_callback(self, count, row, col, processed=False): """ Callback function passed to the backend, where it computes the download progress. Every time the function is called, it will update the progress bar value. """ updated_row = self.get_row_after_sort(row) new_text = self.progress_bar_text(count, processed) if new_text != self.sheet.get_cell_data(updated_row, col): self.sheet.set_cell_data(updated_row, col, new_text, redraw=True) def get_real_col(self, col): # noqa """ with configurable column list, the col number returned by tksheet may not be the same as column no from self.all_columns/self.display_columns. Use self.display_column_vars to identify and return the correct column. """ # find n-th visible column, where n=col i = 0 for c, state in enumerate(Variables().display_columns_vars().values()): if state.get() == 1: if i == col: return c i += 1 def fetch_content(self): self.videos = dict() self.video_slide_mapping = dict() self.expected_real_paths_differ = False self.offline_video_ttid_mapping = None root_url = self.login.url_box.get() subject_dicts = self.impartus.get_subjects(root_url) has_flipped_lectures = False for subject_dict in subject_dicts: videos_by_subject = self.impartus.get_lectures( root_url, subject_dict) flipped_videos_by_subject = self.impartus.get_flipped_lectures( root_url, subject_dict) if len(flipped_videos_by_subject): has_flipped_lectures = True all_videos_by_subject = [ *videos_by_subject, *flipped_videos_by_subject ] slides = self.impartus.get_slides(root_url, subject_dict) mapping_dict = self.impartus.map_slides_to_videos( all_videos_by_subject, slides) for key, val in mapping_dict.items(): self.video_slide_mapping[key] = val self.videos[subject_dict.get('subjectId')] = { x['ttid']: x for x in all_videos_by_subject } state = 'normal' if has_flipped_lectures else 'disabled' self.toolbar.flipped_video_quality_dropdown.configure(state=state) self.menubar.main_menu.entryconfig(Labels.VIDEO, state=state) def fill_content(self): # A mapping dict containing previously downloaded, and possibly moved around / renamed videos. # extract their ttid and map those to the correct records, to avoid forcing the user to re-download. self.offline_video_ttid_mapping = self.impartus.get_mkv_ttid_map() row = 0 sheet_rows = list() for subject_id, videos in self.videos.items(): for ttid, video_metadata in videos.items(): video_metadata = Utils.add_new_fields(video_metadata, self.video_slide_mapping) video_path = self.impartus.get_mkv_path(video_metadata) if not os.path.exists(video_path): # or search from the downloaded videos, using video_ttid_map video_path_moved = self.offline_video_ttid_mapping.get( str(ttid)) if video_path_moved: # For now, use the offline path if a video found. Also set the flag to enable move/rename button video_path = video_path_moved self.expected_real_paths_differ = True captions_path = self.impartus.get_captions_path(video_metadata) if not os.path.exists(captions_path): self.all_captions_found = False slides_path = self.impartus.get_slides_path(video_metadata) video_exists_on_disk = video_path and os.path.exists( video_path) slides_exist_on_server = self.video_slide_mapping.get(ttid) slides_exist_on_disk, slides_path = self.impartus.slides_exist_on_disk( slides_path) metadata = { 'video_metadata': video_metadata, 'video_path': video_path, 'video_exists_on_disk': video_exists_on_disk, 'slides_exist_on_server': slides_exist_on_server, 'slides_exist_on_disk': slides_exist_on_disk, 'slides_url': self.video_slide_mapping.get(ttid), 'slides_path': slides_path, 'captions_path': captions_path, } row_items = list() button_states = list() # data items for col, (key, item) in enumerate(Columns.all_columns.items()): text = '' if item['type'] == 'data': text = video_metadata[key] # title case if item.get('title_case'): text = " ".join(text.splitlines()).strip().title() elif item['type'] == 'auto': text = row elif item['type'] == 'progressbar': if video_exists_on_disk: text = self.progress_bar_text(100, processed=True) else: text = self.progress_bar_text(0) elif item['type'] == 'button': button_states.append( self.get_button_state(key, video_exists_on_disk, slides_exist_on_server, slides_exist_on_disk)) text = item.get('text') elif item['type'] == 'button_state': text = button_states.pop(0) elif item['type'] == 'metadata': text = metadata elif item['type'] == 'original_value': text = video_metadata[key] row_items.append(text) row += 1 sheet_rows.append(row_items) self._init_content() self.sheet.insert_rows(sheet_rows, idx='end') self.reset_column_sizes() self.decorate() # update button status self.set_button_status(redraw=True) self.sheet.grid(row=0, column=0, sticky='nsew') def sort_table(self, args): """ Sorts the table content. """ col = args[1] real_col = self.get_real_col(col) self.sheet.deselect("all") col_name = Columns.column_names[real_col] if not Columns.all_columns[col_name].get('sortable'): return sort_by = col_name if sort_by == self.sort_by: sort_order = 'asc' if self.sort_order == 'desc' else 'desc' else: sort_order = 'desc' self.sort_by = sort_by self.sort_order = sort_order reverse = True if sort_order == 'desc' else False table_data = self.sheet.get_sheet_data() table_data.sort(key=lambda x: x[real_col], reverse=reverse) self.set_headers(sort_by, sort_order) self.set_button_status() self.sheet.refresh() def save_captions_if_needed(self, video_metadata, root_url, captions_path): chat_msgs = self.impartus.get_chats(video_metadata, root_url) date_format = "%Y-%m-%d %H:%M:%S" start_epoch = int( datetime.strptime(video_metadata['startTime'], date_format).timestamp()) try: vtt_content = Captions.get_vtt(chat_msgs, start_epoch) Captions.save_vtt(vtt_content, captions_path) except CaptionsNotFound as ex: self.logger.info( "no lecture chat found for {}".format(captions_path)) return return True def _download_video(self, video_metadata, filepath, captions_path, root_url, row, col, pause_ev, resume_ev): # noqa """ Download a video in a thread. Update the UI upon completion. """ # create a new Impartus session reusing existing token. imp = Impartus(self.impartus.token) pb_col = Columns.column_names.index('downloaded') # # voodoo alert: # It is possible for user to sort the table while download is in progress. # In such a case, the row index supplied to the function call won't match the row index # required to update the correct progressbar/open/play buttons, which now exists at a new # location. # The hidden column index keeps the initial row index, and remains unchanged. # Use row_index to identify the new correct location of the progress bar. row_index = self.get_index(row) imp.process_video(video_metadata, filepath, root_url, pause_ev, resume_ev, partial(self.progress_bar_callback, row=row_index, col=pb_col), video_quality=Variables().lecture_quality_var()) # also download lecture chats, and create a webvtt subtitles file. self.save_captions_if_needed(video_metadata, root_url, captions_path) # download complete, enable open / play buttons updated_row = self.get_row_after_sort(row_index) # update progress bar status to complete. self.progress_bar_callback(row=row_index, col=pb_col, count=100, processed=True) self.sheet.set_cell_data(updated_row, Columns.column_names.index('download_video'), Icons.DOWNLOAD_VIDEO) self.disable_button(updated_row, Columns.column_names.index('download_video')) # enable buttons. self.enable_button(updated_row, Columns.column_names.index('open_folder')) self.enable_button(updated_row, Columns.column_names.index('play_video')) def add_slides(self, row, col): # noqa conf = Config.load(ConfigType.IMPARTUS) file_types = [(str(ext).upper(), '*.{}'.format(ext)) for ext in conf.get('allowed_ext')] filepaths = tkinter.filedialog.askopenfilenames(filetypes=file_types) data = self.read_metadata(row) slides_folder_path = os.path.dirname(data.get('video_path')) for filepath in filepaths: shutil.copy(filepath, slides_folder_path) def pause_resume_button_click(self, row, col, pause_event, resume_event): row_index = self.get_index(row) updated_row = self.get_row_after_sort(row_index) if pause_event.is_set(): self.sheet.set_cell_data(updated_row, col, Icons.PAUSE_DOWNLOAD, redraw=True) resume_event.set() pause_event.clear() else: self.sheet.set_cell_data(updated_row, col, Icons.RESUME_DOWNLOAD, redraw=True) pause_event.set() resume_event.clear() def download_video(self, row, col): """ callback function for Download button. Creates a thread to download the request video. """ data = self.read_metadata(row) video_metadata = data.get('video_metadata') filepath = data.get('video_path') captions_path = data.get('captions_path') root_url = self.login.url_box.get() real_row = self.get_index(row) if self.threads.get(real_row): pause_ev = self.threads.get(real_row)['pause_event'] resume_ev = self.threads.get(real_row)['resume_event'] self.pause_resume_button_click(row, col, pause_ev, resume_ev) return from threading import Event pause_event = Event() resume_event = Event() # note: args is a tuple. thread = threading.Thread(target=self._download_video, args=( video_metadata, filepath, captions_path, root_url, row, col, pause_event, resume_event, )) self.threads[real_row] = { 'thread': thread, 'pause_event': pause_event, 'resume_event': resume_event, } thread.start() def _download_slides(self, ttid, file_url, filepath, root_url, row): """ Download a slide doc in a thread. Update the UI upon completion. """ # create a new Impartus session reusing existing token. imp = Impartus(self.impartus.token) if imp.download_slides(ttid, file_url, filepath, root_url): # download complete, enable show slides buttons self.enable_button(row, Columns.column_names.index('show_slides')) else: tkinter.messagebox.showerror( 'Error', 'Error downloading slides, see console logs for details.') self.enable_button(row, Columns.column_names.index('download_slides')) def download_slides(self, row, col): # noqa """ callback function for Download button. Creates a thread to download the request video. """ data = self.read_metadata(row) video_metadata = data.get('video_metadata') ttid = video_metadata['ttid'] file_url = data.get('slides_url') filepath = data.get('slides_path') root_url = self.login.url_box.get() # note: args is a tuple. thread = threading.Thread(target=self._download_slides, args=( ttid, file_url, filepath, root_url, row, )) # self.threads.append(thread) thread.start() def read_metadata(self, row): """ We saved a hidden column 'metadata' containing metadata for each record. Extract it, and eval it as python dict. """ metadata_col = Columns.column_names.index('metadata') data = self.sheet.get_cell_data(row, metadata_col) return ast.literal_eval(data) def open_folder(self, row, col): # noqa """ fetch video_path's folder from metadata column's cell and open system launcher with it. """ data = self.read_metadata(row) video_folder_path = os.path.dirname(data.get('video_path')) Utils.open_file(video_folder_path) def play_video(self, row, col): # noqa """ fetch video_path from metadata column's cell and open system launcher with it. """ data = self.read_metadata(row) Utils.open_file(data.get('video_path')) def show_slides(self, row, col): # noqa """ fetch slides_path from metadata column's cell and open system launcher with it. """ data = self.read_metadata(row) Utils.open_file(data.get('slides_path')) def auto_organize(self): self.toolbar.auto_organize_button.config(state='disabled') self.menubar.actions_menu.entryconfig(Labels.AUTO_ORGANIZE, state='disabled') moved_files = dict() conf = Config.load(ConfigType.IMPARTUS) for subject_id, videos in self.videos.items(): for ttid, video_metadata in videos.items(): video_metadata = Utils.add_new_fields(video_metadata, self.video_slide_mapping) # for videos expected_video_path = self.impartus.get_mkv_path( video_metadata) real_video_path = self.offline_video_ttid_mapping.get( str(ttid)) if real_video_path and \ pathlib.PurePath(expected_video_path) != pathlib.PurePath(real_video_path) \ and os.path.exists(real_video_path): Utils.move_and_rename_file(real_video_path, expected_video_path) self.logger.info('moved {} -> {}'.format( real_video_path, expected_video_path)) moved_files[real_video_path] = expected_video_path # also update the offline_video_ttid_mapping self.offline_video_ttid_mapping[str( ttid)] = expected_video_path # also check any slides. for ext in conf.get('allowed_ext'): slides_path = '{}.{}'.format( real_video_path[:-len(".mkv")], ext) if os.path.exists(slides_path): expected_slides_path = '{}.{}'.format( expected_video_path[:-len(".mkv")], ext) Utils.move_and_rename_file(slides_path, expected_slides_path) self.logger.info('moved {} -> {}'.format( slides_path, expected_slides_path)) moved_files[slides_path] = expected_slides_path # is the folder empty, remove it.? [also any empty parent folders] old_video_dir = os.path.dirname(real_video_path) sys_name = platform.system() if conf.get('ignore_files').get(sys_name): ignore_files = conf.get('ignore_files')[sys_name] else: ignore_files = [] while True: dir_files = [ x for x in os.listdir(old_video_dir) if x not in ignore_files ] if len(dir_files) > 0: break for file in ignore_files: filepath = os.path.join(old_video_dir, file) if os.path.exists(filepath): os.unlink(filepath) os.rmdir(old_video_dir) self.logger.info('removed empty directory: {}'.format( old_video_dir)) # parent path. old_video_dir = Path(old_video_dir).parent.absolute() # captions expected_captions_path = self.impartus.get_captions_path( video_metadata) if not os.path.exists(expected_captions_path): if self.save_captions_if_needed(video_metadata, self.login.url_box.get(), expected_captions_path): self.logger.info('downloaded captions: {}'.format( expected_captions_path)) self.all_captions_found = True if len(moved_files) > 0: self.auto_organize_dialog(moved_files) self.expected_real_paths_differ = False def set_display_columns(self): column_states = [ i for i, v in enumerate(Variables().display_columns_vars().values()) if v.get() == 1 ] self.sheet.display_columns(indexes=column_states, enable=True, redraw=False) self.reset_column_sizes() self.sheet.refresh() def odd_even_color(self, cs: Dict, redraw=False): """ Apply odd/even colors for table for better looking UI. """ num_rows = self.sheet.total_rows() self.sheet.highlight_rows(list(range(0, num_rows, 2)), bg=cs['even_row']['bg'], fg=cs['even_row']['fg'], redraw=redraw) self.sheet.highlight_rows(list(range(1, num_rows, 2)), bg=cs['odd_row']['bg'], fg=cs['odd_row']['fg'], redraw=redraw) def progress_bar_color(self, cs: Dict, redraw=True): """ Set progress bar color. """ col = Columns.column_names.index('downloaded') num_rows = self.sheet.total_rows() for row in range(num_rows): odd_even_bg = cs['odd_row']['bg'] if row % 2 else cs['even_row'][ 'bg'] self.sheet.highlight_cells(row, col, fg=cs['progressbar']['fg'], bg=odd_even_bg, redraw=redraw) def set_button_status(self, redraw=False): """ reads the states of the buttons from the hidden state columns, and sets the button states appropriately. """ col_indexes = [ x for x, v in enumerate(Columns.all_columns.values()) if v['type'] == 'button_state' ] num_buttons = len(col_indexes) for row, row_item in enumerate(self.sheet.get_sheet_data()): for col in col_indexes: # data set via sheet.insert_row retains tuple/list's element data type, # data set via sheet.set_cell_data makes everything a string. # Consider everything coming out of a sheet as string to avoid any issues. state = str(row_item[col]) if state == 'True': self.enable_button(row, col - num_buttons, redraw=redraw) elif state == 'False': self.disable_button(row, col - num_buttons, redraw=redraw) return def get_button_state(self, key, video_exists_on_disk, slides_exist_on_server, slides_exist_on_disk): # noqa """ Checks to identify when certain buttons should be enabled/disabled. """ state = True if key == 'download_video' and video_exists_on_disk: state = False elif key == 'open_folder' and not video_exists_on_disk: state = False elif key == 'play_video' and not video_exists_on_disk: state = False elif key == 'download_slides' and (slides_exist_on_disk or not slides_exist_on_server): state = False elif key == 'show_slides' and not slides_exist_on_disk: state = False return state def disable_button(self, row, col, redraw=False): """ Disable a button given it's row/col position. """ cs = Config.load( ConfigType.COLORSCHEMES)[Variables().colorscheme_var().get()] self.sheet.highlight_cells(row, col, bg=cs['disabled']['bg'], fg=cs['disabled']['fg'], redraw=redraw) # update state field. state_button_col_name, state_button_col_num = self.get_state_button( Columns.column_names[col]) self.sheet.set_cell_data(row, state_button_col_num, False, redraw=redraw) def enable_button(self, row, col, redraw=False): """ Enable a button given it's row/col position. """ cs = Config.load( ConfigType.COLORSCHEMES)[Variables().colorscheme_var().get()] odd_even_bg = cs['odd_row']['bg'] if row % 2 else cs['even_row']['bg'] odd_even_fg = cs['odd_row']['fg'] if row % 2 else cs['even_row']['fg'] self.sheet.highlight_cells(row, col, bg=odd_even_bg, fg=odd_even_fg, redraw=redraw) # update state field. state_button_col_name, state_button_col_num = self.get_state_button( Columns.column_names[col]) self.sheet.set_cell_data(row, state_button_col_num, True, redraw=redraw) def set_readonly_columns(self, redraw=False): readonly_cols = [ i for i, (k, v) in enumerate(Columns.all_columns.items()) if not v.get('editable') ] self.sheet.readonly_columns(columns=readonly_cols, readonly=True, redraw=redraw) def set_colorscheme(self, cs): if self.frame_content: self.frame_content.configure(bg=cs['root']['bg']) if self.sheet: self.sheet.set_options(frame_bg=cs['table']['bg'], table_bg=cs['table']['bg'], table_fg=cs['table']['fg'], header_bg=cs['header']['bg'], header_fg=cs['header']['fg'], header_grid_fg=cs['table']['grid'], index_grid_fg=cs['table']['grid'], header_border_fg=cs['table']['grid'], index_border_fg=cs['table']['grid'], table_grid_fg=cs['table']['grid'], top_left_bg=cs['header']['bg'], top_left_fg=cs['header']['bg']) self.odd_even_color(cs, redraw=False) self.progress_bar_color(cs, redraw=False) self.set_button_status(redraw=False) self.set_readonly_columns(redraw=False) self.sheet.refresh() def decorate(self): """ calls multiple ui related tweaks. """ self.align_columns() cs = Config.load( ConfigType.COLORSCHEMES)[Variables().colorscheme_var().get()] self.set_colorscheme(cs) self.odd_even_color(cs) self.progress_bar_color(cs) def align_columns(self): # data and progressbar west/left aligned, button center aligned. self.sheet.align_columns([ Columns.column_names.index(k) for k in Columns.data_columns.keys() ], align='w') self.sheet.align_columns([ Columns.column_names.index(k) for k in Columns.progressbar_column.keys() ], align='w') self.sheet.align_columns([ Columns.column_names.index(k) for k in Columns.button_columns.keys() ], align='center') def show_video_callback(self, impartus: Impartus, event=None): # noqa if threading.activeCount( ) > 1: # 1. main thread, 2,3... download threads. response = tk.messagebox.askquestion( 'Download(s) in progress!', "Reloading the content will lose the downloads in progress.\n" + "Do you want to continue?", icon='warning') if response != 'yes': return self.toolbar.reload_button.config(state='disabled') self.menubar.actions_menu.entryconfig(Labels.RELOAD, state='disabled') self.toolbar.auto_organize_button.config(state='disabled') self.menubar.actions_menu.entryconfig(Labels.AUTO_ORGANIZE, state='disabled') self.toolbar.frame_toolbar.grid(row=1, column=0, sticky='ew') self.login.authenticate(impartus) self.set_display_widgets() self.toolbar.reload_button.config(state='normal') self.menubar.actions_menu.entryconfig(Labels.RELOAD, state='normal') if self.expected_real_paths_differ or not self.all_captions_found: auto_organize_button_state = 'normal' else: auto_organize_button_state = 'disabled' self.toolbar.auto_organize_button.config( state=auto_organize_button_state) self.menubar.actions_menu.entryconfig(Labels.AUTO_ORGANIZE, state=auto_organize_button_state) def on_auto_organize_dialog_close(self): self.login.authenticate(self.impartus) Dialogs.on_dialog_close() self.show_video_callback(self.impartus) def auto_organize_dialog(self, moved_files): # noqa dialog = Dialogs.create_dialog( on_close_callback=self.on_auto_organize_dialog_close, title='Auto Organize') label = tk.Label( dialog, text='Following files were moved / renamed -', ) label.grid(row=0, column=0, sticky='w', ipadx=10, ipady=10) dialog.columnconfigure(0, weight=1) # show a dialog with the output. sheet = Sheet( Dialogs.dialog, header_font=(self.header_font, self.header_font_size, "bold"), font=(self.content_font, self.content_font_size, "normal"), align='w', row_height="1", # str value for row height in number of lines. row_index_align="w", auto_resize_default_row_index=False, row_index_width=40, header_align='center', empty_horizontal=0, empty_vertical=0, ) sheet.headers(['Source', '', 'Destination']) target_parent = os.path.dirname(self.impartus.download_dir) for row, (source, destination) in enumerate(moved_files.items()): source = source[len(target_parent) + 1:] destination = destination[len(target_parent) + 1:] sheet.insert_row([source, Icons.MOVED_TO, destination]) sheet.set_all_column_widths() sheet.grid(row=1, column=0, sticky='nsew') ok_button = tk.Button(dialog, text='OK', command=self.on_auto_organize_dialog_close) ok_button.grid(row=2, column=0, padx=10, pady=10)
class product_demo(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.grid_columnconfigure(0, weight=1) self.grid_rowconfigure(0, weight=1) self.frame = tk.Frame(self) self.fill_data_from_db() self.frame.grid_columnconfigure(0, weight=1) self.frame.grid_rowconfigure(0, weight=1) self.sheet = Sheet( self.frame, page_up_down_select_row=True, # empty_vertical = 0, headers=headers, column_width=120, startup_select=(0, 1, "rows"), data=data, height=500, # height and width arguments are optional width=500 # For full startup arguments see DOCUMENTATION.md ) self.sheet.enable_bindings(( "single_select", # "single_select" or "toggle_select" "drag_select", # enables shift click selection as well "column_drag_and_drop", "row_drag_and_drop", "column_select", "row_select", "column_width_resize", "double_click_column_resize", # "row_width_resize", # "column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", # "rc_insert_column", # "rc_delete_column", # "rc_insert_row", "rc_delete_row", # "hide_columns", "copy", # "cut", # "paste", # "delete", "undo", "edit_cell")) self.frame.grid(row=0, column=0, sticky="nswe") self.sheet.grid(row=0, column=0, sticky="nswe") # __________ DISPLAY SUBSET OF COLUMNS __________ self.sheet.display_subset_of_columns(indexes=[0, 1, 2], enable=True) # __________ BINDING A FUNCTIONS TO USER ACTIONS __________ self.sheet.extra_bindings([("end_edit_cell", self.end_edit_cell), ("begin_rc_delete_row", self.row_delete)]) # __________ GETTING FULL SHEET DATA __________ # self.all_data = self.sheet.get_sheet_data() # __________ GETTING CELL DATA __________ # print (self.sheet.get_cell_data(0, 0)) # __________ GETTING ROW DATA __________ # print (self.sheet.get_row_data(0)) # only accessible by index # __________ GETTING COLUMN DATA __________ # print (self.sheet.get_column_data(0)) # only accessible by index def end_edit_cell(self, event): print("cell edited") print(event) PRODUCT.query.filter_by(**{ "pid": self.sheet.get_cell_data(event[0], 0) }).update( {headers[event[1]]: self.sheet.get_cell_data(event[0], event[1])}) db_session.commit() def row_delete(self, event): print("row deleted") print(event) print({"pid": self.sheet.get_cell_data(event[1][0], 0)}) PRODUCT.query.filter_by(**{ "pid": self.sheet.get_cell_data(event[1][0], 0) }).delete() db_session.commit() def fill_data_from_db(self): data.clear() ps = PRODUCT.query.all() k = [[i.to_dict(rules=('-ORDERITEM', )).get(z) for z in headers] for i in ps] data.extend(k)
class TechnicianScreen(design.Page): def __init__(self, app, root, id, *args, **kwargs): design.Page.__init__(self, app, root, *args, **kwargs) self.height = 600 self.width = 1050 self.id = id self.status = [] self.title = "Technician schedule" self.grid_columnconfigure(0, weight=1) self.grid_rowconfigure(0, weight=1) #self.configure(bg="white") self.cols = pd.read_csv("techissue.csv") self.cols = self.cols.loc[self.cols.ID == self.id, :] self.Cols = self.cols[[ "ID", "customerName", "product", "issue", "location", "crit", "issueID" ]] self.row_count = len(self.cols) #print(self.row_coun t) self.data = [self.Cols.iloc[i] for i in range(self.row_count)] labelTop = tk.Label(self, text="Issue status", font=('Times New Roman', 14)) labelTop.grid(column=0, row=0) labelTop.place(relx=0.830, rely=0.075) for i in range(self.row_count): self.var = tk.StringVar() if self.cols.iloc[i, 5] == "**": self.comboExample = ttk.Combobox( self, textvariable=self.var, values=["treated", "not treated", "becomes normal"]) if self.cols.iloc[i, 5] == "*": self.comboExample = ttk.Combobox( self, textvariable=self.var, values=["treated", "not treated"]) self.status.append(self.var) self.comboExample.grid(column=0, row=i + 1) self.comboExample.place(relx=0.830, rely=0.120 + (i * 0.04), relheight=0.04, relwidth=0.140) self.comboExample.current(1) self.sdem = Sheet( self, align="w", header_align="center", row_index_align="center", show=True, column_width=140, row_index_width=50, data_reference=self.data, headers=[ "ID", "customerName", "product", "issue", "location", "crit", "issueID" ], ) self.sdem.enable_bindings( ("single", "drag_select", "column_drag_and_drop", "row_drag_and_drop", "column_select", "row_select", "column_width_resize", "double_click_column_resize", "row_width_resize", "column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize")) self.sdem.edit_bindings(True) self.sdem.grid(row=0, column=0, sticky="nswe") self.sdem.place(relx=0.05, rely=0.067, relheight=0.751, relwidth=0.780) self.sdem.highlight_cells(row=0, column=0, bg="orange", fg="blue") self.sdem.highlight_cells(row=0, bg="orange", fg="blue", canvas="row_index") self.sdem.highlight_cells(column=0, bg="orange", fg="blue", canvas="header") self.send = Button(self, text="send", font=('Times New Roman', 15), bg='orange', fg='blue', padx=5, command=self.save).place(relx=0.880, rely=0.85) def save(self): self.dff = pd.read_csv("customerissue.csv") count1 = len(self.dff) for i in range(self.row_count): if self.status[i].get() == "treated": for j in range(count1): if self.dff.iloc[j]["ID"] == self.cols.iloc[i]["issueID"]: self.dff.iat[j, 5] = "2" if self.status[i].get() == "not treated": for j in range(count1): if self.dff.iloc[j]["ID"] == self.cols.iloc[i]["issueID"]: self.dff.iat[j, 5] = "0" self.dff.to_csv('customerissue.csv', mode='w', index=False, header=[ "name", "product", "issue", "place", "time", "taken", "coordinate", "ID" ]) self.df = pd.read_csv("techissue.csv") count = len(self.df) self.df = self.df.loc[self.df.ID != self.id, :] self.df.to_csv('techissue.csv', mode='w', index=False, header=[ "ID", "customerName", "product", "issue", "location", "crit", "issueID" ]) print(self.dff) print(self.df) print("")
def start_gui(hostname, total_mem, patterns): global combo_pattern, root_window, sheet_proc, sheet_proc_last_row, proc_mem_plot global root_window ############################################################################### # root window ############################################################################### root_window = tk.Tk() root_window.geometry('1200x400+20+20') root_window.title('Process Memory Monitor ' + VERSION + ' - ' + hostname) root_window_icon = tk.PhotoImage(file=str(__icon__)) root_window.iconphoto(True, root_window_icon) root_window.grid_columnconfigure(0, weight=0) root_window.grid_columnconfigure(1, weight=0) root_window.grid_columnconfigure(2, weight=0) root_window.grid_columnconfigure(3, weight=1) cur_grid_row = 0 label_pattern = ttk.Label(root_window, text="Command Pattern", width=LABEL_WIDTH, anchor='w') label_pattern.grid(row=cur_grid_row, column=0, sticky='w', padx=10, pady=10) combo_pattern = ttk.Combobox(root_window, width=COMBO_WIDTH) combo_pattern['values'] = [] combo_pattern.grid(row=cur_grid_row, column=1, sticky='w', pady=10) cur_grid_row = cur_grid_row + 1 # sheet for pattern sheet_proc = Sheet(root_window, default_row_index="numbers", total_rows=200, total_columns=5) sheet_proc.enable_bindings(( "single_select", # "single_select" or "toggle_select" "drag_select", # enables shift click selection as well "column_drag_and_drop", "row_drag_and_drop", #"column_select", "row_select", "column_width_resize", "double_click_column_resize", "arrowkeys", #"row_height_resize", #"double_click_row_resize", "right_click_popup_menu", "rc_select", #"rc_insert_column", #"rc_delete_column", #"rc_insert_row", #"rc_delete_row", "copy", "cut", "paste", "delete", "undo", "edit_cell")) sheet_proc.grid(row=cur_grid_row, columnspan=4, sticky='nswe') root_window.grid_rowconfigure(cur_grid_row, weight=1) sheet_proc.set_cell_data(0, SHEET_PID_COL, 'PID') sheet_proc.set_cell_data(0, SHEET_CPU_COL, '%CPU') sheet_proc.set_cell_data(0, SHEET_RSS_COL, 'RSS(GB)') sheet_proc.set_cell_data(0, SHEET_CMD_COL, 'CMD') sheet_proc.set_cell_data(0, SHEET_LAST_UPDATED_COL, 'Last Updated') sheet_proc.column_width(column=SHEET_PID_COL, width=150) sheet_proc.column_width(column=SHEET_CPU_COL, width=100) sheet_proc.column_width(column=SHEET_RSS_COL, width=100) sheet_proc.column_width(column=SHEET_CMD_COL, width=450) sheet_proc.column_width(column=SHEET_LAST_UPDATED_COL, width=200) cur_grid_row = cur_grid_row + 1 sheet_proc_last_row = 0 # command buttons button_plot = ttk.Button(root_window, text="Plot", command=show_plot_window) button_plot.grid(row=cur_grid_row, column=1, pady=10) cur_grid_row = cur_grid_row + 1 proc_mem_plot = ProcMemPlot(hostname) proc_mem_plot.total_mem = total_mem combo_pattern['values'] = patterns if len(patterns) > 0: combo_pattern.current(0) root_window.after(DEFAULT_REFRESH_INTERVAL * 1000, refresh_database) root_window.mainloop()
class PageScreener(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) self.controller = controller self.flag = 0 self.FLAG_THRD_SCREEN_DATA = 1 # 处理数据队列 self.queData = Queue() # 数据缓存,用于重复使用, self.listData = [] topFrame = tk.Frame(self) self.btnMainPage = tk.Button(topFrame, text="主页面", command=self.on_btn_main_page).pack( side=tk.LEFT, padx=4) # 日期选择 # Calendar((x, y), 'ur').selection() 获取日期,x,y为点坐标 date_start_gain = lambda: [ self.date_start.set(date) for date in [CalendarCustom(None, 'ul').selection()] if date ] self.btnStart = tk.Button(topFrame, text='选股日期:', command=date_start_gain) self.btnStart.pack(side=tk.LEFT, padx=4) # ret = self.btnStart.winfo_geometry() self.date_start = tk.StringVar() ttk.Entry(topFrame, textvariable=self.date_start).pack(side=tk.LEFT) today = get_last_valid_trade_date() dt = datetime.datetime.strptime(today, '%Y%m%d') today = dt.strftime('%Y-%m-%d') self.date_start.set(today) tk.Label(topFrame, text="选股类型:").pack(side=tk.LEFT) self.screenTypeStr = tk.StringVar() cmbScreenType = ttk.Combobox(topFrame, width=15, textvariable=self.screenTypeStr, state='readonly') # Adding combobox drop down list cmbScreenType['values'] = ('平台突破', '均线附近') cmbScreenType.current(1) cmbScreenType.pack(side=tk.LEFT, padx=4) # cmbScreenType.bind("<<ComboboxSelected>>", self.on_cmb_screen_select) # 选股周期数 self.screenIntervalCountStr = tk.StringVar(value='25') # state=tk.DISABLED 默认禁止输入 self.screenIntervalCount = ttk.Entry( topFrame, width=10, textvariable=self.screenIntervalCountStr) self.screenIntervalCount.pack(side=tk.LEFT, padx=4) # 选股周期类型 self.screenIntervalTypeStr = tk.StringVar() cmbScreenInterval = ttk.Combobox( topFrame, width=8, textvariable=self.screenIntervalTypeStr, state='readonly') # Adding combobox drop down list cmbScreenInterval['values'] = ('日', '周', '月') cmbScreenInterval.current(0) cmbScreenInterval.pack(side=tk.LEFT, padx=4) # cmbScreenInterval.bind("<<ComboboxSelected>>", self.on_cmb_screen_interval_select) self.chkST = tk.IntVar() tk.Checkbutton(topFrame, text="包括ST", variable=self.chkST).pack(side=tk.LEFT, padx=4) self.chkTech = tk.IntVar() tk.Checkbutton(topFrame, text="包括科创板", variable=self.chkTech).pack(side=tk.LEFT, padx=4) self.btnStart = tk.Button(topFrame, text="选股", command=self.on_btn_start) self.btnStart.pack(side=tk.LEFT, padx=4) topFrame.pack(side=tk.TOP, fill=tk.BOTH) self.tipsStr = tk.StringVar() tk.Label(topFrame, textvariable=self.tipsStr, font=("simsun", 12)).pack(side=tk.LEFT) self.tipsStr.set('状态:准备...') # Progress bar widget self.progress = Progressbar(topFrame, orient=tk.HORIZONTAL, length=100, mode='determinate') # self.progress.pack(side=tk.LEFT, padx=4) # self.progress.pack_forget() # 列表框 self.frameReport = tk.Frame(self) self.sheet = Sheet(self.frameReport) self.sheet.enable_bindings(( # "single_select", # "single_select" or "toggle_select" # "drag_select", # enables shift click selection as well # "column_drag_and_drop", # "row_drag_and_drop", # "column_select", "row_select", "column_width_resize", "double_click_column_resize", # "row_width_resize", # "column_height_resize", # "arrowkeys", # "row_height_resize", # "double_click_row_resize", # "right_click_popup_menu", # "rc_select", # "rc_insert_column", # "rc_delete_column", # "rc_insert_row", # "rc_delete_row", # "hide_columns", # "copy", # "cut", # "paste", # "delete", # "undo", # "edit_cell" )) self.sheet.pack(side=tk.TOP, fill=tk.BOTH, expand=1) self.frameReport.pack(side=tk.TOP, fill=tk.BOTH, expand=1, pady=4) self.sheet.headers(['编码', '名称', '板块', '收盘价', '涨幅']) self.sheet.refresh() def on_btn_main_page(self): self.controller.show_frame('PageMain') pass def on_btn_start(self): if self.FLAG_THRD_SCREEN_DATA == self.flag & self.FLAG_THRD_SCREEN_DATA: return self.flag |= self.FLAG_THRD_SCREEN_DATA self.progress.pack(side=tk.LEFT, padx=4) # 获取数据线程,取一个放入队列,由另一个线程处理 # 周期参数 iInterval = int(self.screenIntervalCountStr.get()) # TODO 测试代码, iInterval = 43 type = 'd' if self.screenIntervalTypeStr.get() == '周': type = 'w' elif self.screenIntervalTypeStr.get() == '月': type = 'm' start_date = self.date_start.get().replace('-', '') # 多获取 10 组数据 thrd = threading.Thread(target=dbService.get_data_thread, args=( iInterval + 30, type, self.chkST.get(), self.chkTech.get(), self.queData, start_date, )) thrd.setDaemon(True) # 守护线程 thrd.start() cnt = self.sheet.get_total_rows() for i in range(cnt): self.sheet.delete_row(i) self.sheet.refresh() if self.screenTypeStr.get() == '平台突破': thrd = threading.Thread(target=self.screen_platform, args=(self.queData, )) thrd.setDaemon(True) # 守护线程 thrd.start() elif self.screenTypeStr.get() == '均线附近': thrd = threading.Thread(target=self.screen_ma_around, args=(self.queData, )) thrd.setDaemon(True) # 守护线程 thrd.start() pass def screen_platform(self, in_q): """ 平台突破 找出N天内创新高的股票, :return: """ self.btnStart['state'] = 'disabled' self.tipsStr.set('状态:正在读取数据,请耐心等待...') # 准备数据 iInterval = int(self.screenIntervalCountStr.get()) type = 'd' if self.screenIntervalTypeStr.get() == '周': type = 'w' elif self.screenIntervalTypeStr.get() == '月': type = 'm' self.progress['value'] = 5 # root.update_idletasks() # 避免中间缺数据 * 2 count = iInterval + 5 datas = dbService.get_data(count, type, self.chkST.get(), self.chkTech.get()) screenCount = 0 self.tipsStr.set('状态:正在选股,请耐心等待...') per_interval = len(datas) / 95 step_count = 0 progress_step = 5 pickup_list = [] for it in datas: step_count += 1 if step_count >= per_interval: step_count = 0 progress_step += 1 self.progress['value'] = progress_step # root.update_idletasks() result = platform_break_through(it, iInterval) if result: screenCount += 1 print(result) pickup_list.append(result) pickup_sorted = sorted(pickup_list, key=itemgetter(3), reverse=True) for it in pickup_sorted: self.sheet.insert_row(values=it) self.sheet.refresh() self.progress['value'] = 100 # root.update_idletasks() self.tipsStr.set('状态:共选出 {:d} 只股票'.format(screenCount)) self.flag &= (~self.FLAG_THRD_SCREEN_DATA) self.btnStart['state'] = 'normal' self.progress.pack_forget() print("screen_platform exit ==========") pass def screen_ma_around(self, in_q): """ 参数 120,2 表示 120 日均线,2%附近 ma120*0.98 < 收盘价 < ma120*1.2 :return: """ self.btnStart['state'] = 'disabled' screenCount = 0 self.tipsStr.set('状态:正在处理,请耐心等待...') # 准备数据 param = [(self.screenIntervalCountStr.get())] per_interval = get_code_count() / 95 step_count = 0 progress_step = 5 pickup_list = [] while True: try: it = in_q.get_nowait() except queue.Empty as e1: continue if it is None: break step_count += 1 if step_count >= per_interval: step_count = 0 progress_step += 1 self.progress['value'] = progress_step result = ma25_around(it, param) if result: screenCount += 1 print(result) self.sheet.insert_row(values=result) # self.sheet.refresh() # pickup_list.append(result) # pickup_sorted = sorted(pickup_list, key=itemgetter(3), reverse=True) # # for it in pickup_sorted: # self.sheet.insert_row(values=it) self.progress['value'] = 100 # root.update_idletasks() self.tipsStr.set('状态:共选出 {:d} 只股票'.format(screenCount)) self.flag &= (~self.FLAG_THRD_SCREEN_DATA) self.btnStart['state'] = 'normal' self.progress.pack_forget() self.progress['value'] = 0 print("screen_ma_around exit ==========") pass def tree_solution_selected(self, selected): print('tree_solution_selected items:', selected) pass
class Application: def __init__(self, master): self.master = master self.now = datetime.now() # self.master.wm_iconbitmap("logo.ico") self.master.eval('tk::PlaceWindow . center') self.master.minsize(400, 300) self.master.grid_columnconfigure(0, weight=1) self.master.grid_rowconfigure(0, weight=1) style = ttk.Style(self.master) style.configure("Placeholder.TEntry", foreground="#d5d5d5") self.master.title("Query thông tin sản phẩm") self.savedKw = str(Path.home()) + os.path.sep + "savedkw.txt" self.master_data = {} self.selected_sheets_data = [] self.data_to_fill = [] self.menubar_items = ["File", "Xem", "Công cụ", "Giúp đỡ"] self.sheet = None self.gspread_sheet = None self.url_input = None self.sheet_list = [] self.sheet_titles = [] self.highlighted_index = [] # self.url_gsheet = "https://docs.google.com/spreadsheets/d/10oJamLk0Bj4ffcDbnu9-96-dn7Tf7TM0EnJ2-emSp9c/edit#gid=1900586655" self.url_gsheet = "Reason Code CB" self.error_load_sheet = False self.current_words = [] self.toggle_theme = IntVar() self.toggle_theme.set(0) self.toggle_compact = IntVar() self.toggle_compact.set(0) self.logging = [] self.current_log = [] self.area_filter_value = IntVar() self.sheet_filter_value = IntVar() self.create_widgets() def get_data_to_fill(self, selected_sheets): for selected_sheet in selected_sheets: self.selected_sheets_data.append(self.master_data[selected_sheet]) self.sheet_titles.append(self.master_data[selected_sheet][0]) for each_sheet in self.selected_sheets_data: self.data_to_fill = self.data_to_fill + each_sheet def get_gsheet_value(self, gsheet_url): scope = [ "https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive" ] creds = ServiceAccountCredentials.from_json_keyfile_name( "creds.json", scope) # creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope) self.data_to_fill = [] self.error_load_sheet = False try: client = gspread.authorize(creds) if "http" in gsheet_url: self.gspread_sheet = client.open_by_url(gsheet_url) else: self.gspread_sheet = client.open(gsheet_url) worksheets = self.gspread_sheet.worksheets() for worksheet in worksheets: worksheet_name = worksheet.title worksheet_values = self.gspread_sheet.worksheet( worksheet_name).get_all_values() self.sheet_list.append(worksheet_name) self.master_data[worksheet_name] = worksheet_values self.get_data_to_fill(self.sheet_list) except (ServerNotFoundError, Exception) as e: self.error_load_sheet = True self.cell_value_label.configure(text="Có lỗi xảy ra, hãy thử lại", anchor="w") self.data_to_fill = [[]] except SpreadsheetNotFound as notfound: self.error_load_sheet = True self.cell_value_label.configure( text="File spreadsheet không tồn tại", anchor="w") self.data_to_fill = [[]] class AutocompleteEntry(ttk.Entry): def __init__(self, autocompleteList, placeholder, *args, **kwargs): # Custom matches function if 'matchesFunction' in kwargs: self.matchesFunction = kwargs['matchesFunction'] del kwargs['matchesFunction'] else: def matches(fieldValue, acListEntry): pattern = re.compile('.*' + re.escape(fieldValue) + '.*', re.IGNORECASE) return re.match(pattern, acListEntry) self.matchesFunction = matches ttk.Entry.__init__(self, *args, style="Placeholder.TEntry", **kwargs) self.autocompleteList = autocompleteList self.placeholder = placeholder self.focus() self.insert("0", self.placeholder) self.bind("<FocusIn>", self.clear_placeholder) self.bind("<FocusOut>", self.add_placeholder) self.var = self["textvariable"] if self.var == '': self.var = self["textvariable"] = StringVar() self.var.trace('w', self.changed) self.bind("<Right>", self.selection) self.bind("<Up>", self.moveUp) self.bind("<Down>", self.moveDown) self.bind("<Escape>", self.hide) self.listboxUp = False def clear_placeholder(self, e): if self["style"] == "Placeholder.TEntry": self.delete("0", "end") self["style"] = "TEntry" def add_placeholder(self, e): if not self.get(): self.insert("0", self.placeholder) self["style"] = "Placeholder.TEntry" def changed(self, name, index, mode): if self.var.get() == '': if self.listboxUp: self.listbox.destroy() self.listboxUp = False else: words = self.comparison() self.listboxLength = len(words) if words: if not self.listboxUp: self.listbox = Listbox(width=self["width"], height=self.listboxLength) self.listbox.bind("<<ListboxSelect>>", self.clickItem) self.listbox.bind("<Right>", self.selection) self.listbox.place(x=self.winfo_x(), y=self.winfo_y() + self.winfo_height()) self.listboxUp = True self.listbox.delete(0, END) for w in words: self.listbox.insert(END, w) else: if self.listboxUp: self.listbox.destroy() self.listboxUp = False def clickItem(self, event): if self.listboxUp: index = int(self.listbox.curselection()[0]) self.var.set(self.listbox.get(index)) self.listbox.destroy() self.listboxUp = False self.icursor(END) def selection(self, event): if self.listboxUp and self.listbox.curselection() != (): self.var.set(self.listbox.get(ACTIVE)) self.listbox.destroy() self.listboxUp = False self.icursor(END) else: if self.listboxUp: self.listbox.destroy() self.listboxUp = False self.icursor(END) def hide(self, event): if self.listboxUp: self.listbox.destroy() self.listboxUp = False def moveUp(self, event): if self.listboxUp: if self.listbox.curselection() == (): index = '-2' else: index = self.listbox.curselection()[-1] if index != END: self.listbox.selection_clear(first=index) index = str(int(index) - 1) self.listbox.see(index) # Scroll! self.listbox.selection_set(first=index) self.listbox.activate(index) def moveDown(self, event): if self.listboxUp: if self.listbox.curselection() == (): index = '-1' else: index = self.listbox.curselection()[-1] if index != END: self.listbox.selection_clear(first=index) index = str(int(index) + 1) self.listbox.see(index) # Scroll! self.listbox.selection_set(first=index) self.listbox.activate(index) def comparison(self): return [ w for w in self.autocompleteList if self.matchesFunction(self.var.get(), w) ] def get_saved_keywords(self): try: with open(self.savedKw, "r", encoding="utf-8") as f: lines = f.readlines() for line in lines: self.current_words.append(line.replace("\n", "")) except FileNotFoundError as fe: pass def save_keywords(self, keyword): if keyword not in self.current_words and keyword != "": with open(self.savedKw, "a", encoding="utf-8") as f: f.write(f"{keyword}\n") def search_keyword(self, event): self.search_bar.selection(event) kw = self.search_bar.get() result = [] titles = [] for each_sheet in self.selected_sheets_data: search_result = [ row for row in each_sheet if kw.lower() in str(row).lower() ] if len(search_result) > 0: search_result.insert(0, each_sheet[0]) titles.append(each_sheet[0]) # search_result = list(set(search_result)) result = result + search_result self.sheet.set_sheet_data(result, reset_col_positions=False) self.dehighlight_current_titles() self.highlight_sheet_title(result, titles) if kw != "": self.save_keywords(kw) log_output = "Đã tìm từ khóa: {}\n".format(kw) self.logging.append(log_output) if len(result) > 0 and kw not in self.current_words: self.current_words.append(kw) def highlight_sheet_title(self, current_data, titles): for index, value in enumerate(current_data): for i in titles: if i == value: for i in range(len(i)): self.sheet.highlight_cells(row=index, column=i, bg="#ed4337", fg="white") self.highlighted_index.append(index) self.sheet.refresh() def dehighlight_current_titles(self, indexes): for r in self.highlighted_index: for c in range(50): self.sheet.dehighlight_cells(row=r, column=c) self.sheet.refresh() def load_data(self): self.sheet.set_sheet_data([[]], reset_col_positions=True) for i in self.menubar_items: self.menubar.entryconfig(i, state="disabled") self.cell_value_label.configure(text="Đang tải dữ liệu file ...", anchor="w") self.get_gsheet_value(self.url_gsheet) self.toggle_compact.set(0) self.sheet.set_sheet_data(self.data_to_fill, reset_col_positions=True) if not self.error_load_sheet: self.cell_value_label.configure(text="---", anchor="w") for i in self.menubar_items: self.menubar.entryconfig(i, state="normal") self.highlight_sheet_title(self.data_to_fill, self.sheet_titles) def load_data_in_thread(self): t = Thread(target=self.load_data) t.start() def cell_select(self, response): self.cell_value_label.config( text=self.sheet.get_cell_data(response[1], response[2])) def load_another_gsheet(self): self.load_new_wd = Toplevel() self.load_new_wd.title("Nhập tên hoặc url của spreadsheet") self.load_new_wd.grab_set() self.load_new_wd.resizable(False, False) self.load_new_wd.grid_columnconfigure(0, weight=1) self.load_new_wd.grid_rowconfigure(0, weight=1) sub_frame = Frame(self.load_new_wd) sub_frame.grid(row=0, column=0, sticky="nsew", padx=1, pady=1) self.url_input = ttk.Entry(sub_frame, width=100) self.url_input.focus() self.url_input.grid(row=0, column=0, ipady=5, sticky="we", padx=5, pady=5) self.url_input.bind("<Return>", self.get_new_gsheet_data) sub_frame.grid_columnconfigure(0, weight=1) sub_frame.grid_rowconfigure(1, weight=1) def get_new_gsheet_data(self, event): if self.url_gsheet.strip() != "": self.url_gsheet = self.url_input.get() self.load_new_wd.destroy() self.load_data_in_thread() def switch_theme(self): mode = self.toggle_theme.get() if mode == 0: self.sheet.change_theme("light") else: self.sheet.change_theme("dark") def switch_compact(self): mode = self.toggle_compact.get() len_shd = len(self.data_to_fill) if mode == 0: self.sheet.set_column_widths([120 for c in range(len_shd)]) else: self.sheet.set_column_widths([30 for c in range(len_shd)]) self.sheet.refresh() def filter_sheet(self): self.filter_wd = Toplevel() self.filter_wd.title("Filter dữ liệu") self.filter_wd.maxsize(400, 200) self.filter_wd.grab_set() self.filter_wd.grid_columnconfigure(0, weight=1) self.filter_wd.grid_rowconfigure(0, weight=1) nb = ttk.Notebook(self.filter_wd) nb.grid(row=0, column=0, sticky="nswe") area_filter = Frame(self.filter_wd, padx=1, pady=1) sheet_filter = Frame(self.filter_wd, padx=1, pady=1) area_filter.grid(row=0, column=0, sticky="nsew", padx=1, pady=1) sheet_filter.grid(row=0, column=0, sticky="nsew", padx=1, pady=1) nb.add(area_filter, text="Filter theo vùng") nb.add(sheet_filter, text="Filter theo sheet") radiobtn1 = Radiobutton(area_filter, text="Shopee C2C", variable=self.area_filter_value, value=1) radiobtn1.grid(row=0, column=0) radiobtn2 = Radiobutton(area_filter, text="Shopee Mall", variable=self.area_filter_value, value=2) radiobtn2.grid(row=1, column=0) radiobtn3 = Radiobutton(area_filter, text="Note", variable=self.area_filter_value, value=3) radiobtn3.grid(row=1, column=0) for sheet_name in self.sheet_list: ttk.Checkbutton(sheet_filter, text=sheet_name).grid(sticky="w") nb.grid_columnconfigure(0, weight=1) nb.grid_rowconfigure(0, weight=1) sheet_filter.grid_columnconfigure(0, weight=1) sheet_filter.grid_rowconfigure(0, weight=1) # def get_logging(self): # if len(self.current_log) < len(self.logging): # self.log_entry.config(state=NORMAL) # self.log_entry.insert(INSERT, self.logging[len(self.logging) - 1]) # self.log_entry.see(END) # self.log_entry.config(state=DISABLED) # self.current_log.append(self.logging[len(self.logging) - 1]) # self.log_entry.after(1000, self.get_logging) def open_console(self): self.console_wd = Toplevel() self.console_wd.title("Console log") # self.console_wd.grab_set() self.console_wd.minsize(500, 200) self.console_wd.resizable(False, False) self.console_wd.grid_columnconfigure(0, weight=1) self.console_wd.grid_rowconfigure(0, weight=1) sub_frame = Frame(self.console_wd) sub_frame.grid(row=0, column=0, sticky="nsew", padx=1, pady=1) self.log_entry = ScrolledText(sub_frame) self.log_entry.grid(row=0, column=0, sticky="nswe", padx=5, pady=5) self.log_entry.insert( INSERT, "Console log của app được mở vào lúc {}\n\n".format(self.now)) # self.get_logging() sub_frame.grid_columnconfigure(0, weight=1) sub_frame.grid_rowconfigure(0, weight=1) def create_menu_bar(self): self.menubar = Menu(self.master) filemenu = Menu(self.menubar, tearoff=0) self.menubar.add_cascade(label=self.menubar_items[0], menu=filemenu) filemenu.add_command(label="Reload dữ liệu", command=self.load_data_in_thread) filemenu.add_command(label="Thoát", command=self.master.quit) viewmenu = Menu(self.menubar, tearoff=0) self.menubar.add_cascade(label=self.menubar_items[1], menu=viewmenu) viewmenu.add_command(label="Filter dữ liệu", command=self.filter_sheet) viewmenu.add_checkbutton(label="Đổi theme trắng/đen", variable=self.toggle_theme, command=self.switch_theme) viewmenu.add_checkbutton(label="Chế độ compact", variable=self.toggle_compact, command=self.switch_compact) toolmenu = Menu(self.menubar, tearoff=0) self.menubar.add_cascade(label=self.menubar_items[2], menu=toolmenu) toolmenu.add_command(label="Load dữ liệu khác", command=self.load_another_gsheet) helpmenu = Menu(self.menubar, tearoff=0) self.menubar.add_cascade(label=self.menubar_items[3], menu=helpmenu) helpmenu.add_command(label="Console log", command=self.open_console) self.master.config(menu=self.menubar) def create_widgets(self): self.get_saved_keywords() #--------CREATE MENU BAR---------- self.create_menu_bar() #--------CREATE NAVIGATION FRAME---------- self.main_frame = Frame(self.master) self.main_frame.grid(row=0, column=0, sticky="nsew", padx=1, pady=1) #--------CREATE SEARCH BAR AND SEARCH BUTTON WIDGET---------- self.search_bar = self.AutocompleteEntry(self.current_words, "Nhập từ khóa cần tìm", self.main_frame) self.search_bar.grid(row=0, column=0, ipady=5, sticky="we", padx=5, pady=5) self.search_bar.bind("<Return>", self.search_keyword) #--------CREATE CELL DETAIL VALUE LABEL---------- self.cell_value_label = Label(self.main_frame, text="---", anchor="w") self.cell_value_label.grid(row=1, column=0, padx=5, sticky="nw") self.main_frame.grid_columnconfigure(0, weight=1) self.main_frame.grid_rowconfigure(2, weight=1) #--------CREATE SHEET WIDGET---------- self.sheet = Sheet(self.main_frame, align="w") self.sheet.enable_bindings( ("single_select", "drag_select", "column_select", "row_select", "column_width_resize", "double_click_column_resize", "row_width_resize", "column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "rc_select", "copy", "paste", "undo")) self.sheet.extra_bindings([("cell_select", self.cell_select)]) self.sheet.grid(row=2, column=0, sticky="nswe", padx=5, pady=5) self.load_data_in_thread()
class TableLastOut(): def __init__(self): text_customer_latest = Label( root, text="List Pelanggan Urut Terakhir Keluar", font="ROBOTO 13") text_customer_latest.grid(row=8, column=0, columnspan=2, padx=5, pady=(80, 10)) self.frame = tableFrameLatest self.frame.grid_columnconfigure(0, weight=1) self.frame.grid_rowconfigure(0, weight=1) self.sheet = Sheet( self.frame, page_up_down_select_row=True, # empty_vertical = 0, column_width=120, startup_select=(0, 1, "rows"), # row_height = "4", # default_row_index = "numbers", # default_header = "both", # empty_horizontal = 0, # show_vertical_grid = False, # show_horizontal_grid = False, # auto_resize_default_row_index = False, # header_height = "3", # row_index_width = 100, # align = "e", # header_align = "w", # row_index_align = "w", data=sorted(list_last_out, key=lambda x: x[2], reverse=TRUE), # to set sheet data at startup headers=["No Plat Polisi", "Masuk", "Keluar", "Biaya"], # set_all_heights_and_widths = True, #to fit all cell sizes to text at start up # headers = 0, #to set headers as first row at startup # headers = [f"Column {c}\nnewline1\nnewline2" for c in range(30)], # theme = "light green", # row_index = 0, #to set row_index as first column at startup # total_rows = 2000, #if you want to set empty sheet dimensions at startup # total_columns = 30, #if you want to set empty sheet dimensions at startup height=200, # height and width arguments are optional width=500 # For full startup arguments see DOCUMENTATION.md ) # self.sheet.hide("row_index") # self.sheet.hide("header") # self.sheet.hide("top_left") self.sheet.enable_bindings(( "single_select", # "single_select" or "toggle_select" "drag_select", # enables shift click selection as well "column_drag_and_drop", "row_drag_and_drop", "column_select", "row_select", "column_width_resize", "double_click_column_resize", # "row_width_resize", # "column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", "rc_insert_column", "rc_delete_column", "rc_insert_row", "rc_delete_row", "hide_columns", "copy", "cut", "paste", "delete", "undo", "edit_cell")) # self.sheet.disable_bindings() #uses the same strings # self.sheet.enable_bindings() self.frame.grid(row=9, column=0, columnspan=2, sticky="nswe", padx=5) self.sheet.grid(row=9, column=0, columnspan=2, sticky="nswe", padx=5) def set_data(self): self.sheet.set_sheet_data( sorted(list_last_out, key=lambda x: x[2], reverse=TRUE))
class MantTelefono(): def __init__(self, parent): self.parent = parent self.raiz = Toplevel(self.parent) self.raiz.title("Mantenimiento de Telefonos") self.raiz.geometry('900x510') menubar = Menu(self.raiz) self.raiz.config(menu=menubar) filemenu = Menu(menubar, tearoff=0) filemenu.add_command(label="Acerca de..") filemenu.add_separator() filemenu.add_command(label="Salir", command=self.raiz.quit) mantmenu = Menu(menubar, tearoff=0) mantmenu.add_command(label="Personas", command=self.mostrar_mant_personas) mantmenu.add_command(label="Direcciones") menubar.add_cascade(label="Archivo", menu=filemenu) menubar.add_cascade(label="Mantenimiento", menu=mantmenu) self.fuente = font.Font(weight="bold") self.telefono = Telefono.Telefono() self.insertando = True self.nombreCliente = StringVar() self.Cliente = Cliente.Cliente() self.lb_tituloPantalla = Label(self.raiz, text="MANTENIMIENTO DE TELEFONOS", font=self.fuente) self.lb_tituloPantalla.place(x=320, y=20) self.lb_cedula = Label(self.raiz, text="Cedula:") self.lb_cedula.place(x=240, y=60) self.txt_cedula = Entry(self.raiz, textvariable=self.telefono.cedula, justify="right", width=12) self.txt_cedula.place(x=370, y=60) self.bt_consultar = Button(self.raiz, text="Consultar", width=15, command=self.consultarNombre) self.bt_consultar.place(x=512, y=60) self.lb_nombre = Label(self.raiz, text="Nombre:") self.lb_nombre.place(x=240, y=90) self.txt_nombre = Entry(self.raiz, textvariable=self.nombrePersona, justify="right", width=30) self.txt_nombre.place(x=370, y=90) self.lb_telefono = Label(self.raiz, text="Telefono:") self.lb_telefono.place(x=240, y=120) self.txt_telefono = Entry(self.raiz, textvariable=self.telefono.telefono, justify="right", width=30) self.txt_telefono.place(x=370, y=120) self.lb_descripcion = Label(self.raiz, text="Descripción:") self.lb_descripcion.place(x=240, y=150) self.txt_descripcion = Entry(self.raiz, textvariable=self.telefono.descripcion, justify="right", width=30) self.txt_descripcion.place(x=370, y=150) self.bt_borrar = Button(self.raiz, text="Limpiar", width=15, command=self.limpiarInformacion) self.bt_borrar.place(x=370, y=180) self.bt_enviar = Button(self.raiz, text="Enviar", width=15, command=self.enviarInformacion) self.bt_enviar.place(x=510, y=180) self.lb_tituloPantalla = Label(self.raiz, text="INFORMACIÓN INCLUIDA", font=self.fuente) self.lb_tituloPantalla.place(x=350, y=230) self.sheet = Sheet( self.raiz, page_up_down_select_row=True, column_width=120, startup_select=(0, 1, "rows"), headers=['Cédula', 'Nombre', 'Telefono', 'Descripción'], height=195, width=720) self.sheet.enable_bindings( ("single_select", "column_select", "row_select", "column_width_resize", "double_click_column_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", "rc_insert_column", "rc_delete_column", "rc_insert_row", "rc_delete_row")) self.sheet.place(x=20, y=260) self.bt_cargar = Button(self.raiz, text="Cargar", width=15, command=self.cargarInformacion) self.bt_cargar.place(x=750, y=255) self.bt_eliminar = Button(self.raiz, text="Eliminar", width=15, command=self.eliminarInformacion) self.bt_eliminar.place(x=750, y=295) self.cargarTodaInformacion() self.parent.withdraw() self.raiz.protocol("WM_DELETE_WINDOW", self.on_closing) def enviarInformacion(self): try: self.telefonoBo = TelefonoBO.TelefonoBO() if (self.insertando == True): self.telefonoBo.guardar(self.telefono) else: self.telefonoBo.modificar(self.telefono) self.cargarTodaInformacion() self.telefono.limpiar() self.nombreCliente.set("") if (self.insertando == True): msg.showinfo( "Acción: Agregar teléfono", "La información del teléfono ha sido incluida correctamente" ) else: msg.showinfo( "Acción: Modificar teléfono", "La información del teléfono ha sido modificada correctamente" ) self.insertando = True except Exception as e: msg.showerror("Error", str(e)) def limpiarInformacion(self): self.telefono.limpiar() self.nombreCliente.set("") self.insertando = True msg.showinfo( "Acción del sistema", "La información del formulario ha sido eliminada correctamente") def consultarNombre(self): try: self.ClienteBo = PersonoBO.ClienteBO() self.Cliente.cedula.set(self.telefono.cedula.get()) self.ClienteBo.consultarCliente(self.Cliente) if self.Cliente.nombre.get() == "": self.nombreCliente = "No existe el Cliente " else: self.nombreCliente.set(self.Cliente.nombre.get() + " " + self.Cliente.apellido1.get() + " " + self.Cliente.apellido2.get()) except Exception as e: msg.showerror("Error", str(e)) def cargarTodaInformacion(self): try: self.telefonoBo = TelefonoBO.TelefonoBO() resultado = self.telefonoBo.consultar() self.sheet.set_sheet_data(resultado) except Exception as e: msg.showerror("Error", str(e)) def cargarInformacion(self): try: datoSeleccionado = self.sheet.get_currently_selected() telefono = (self.sheet.get_cell_data(datoSeleccionado[0], 2)) self.telefono.telefono.set(telefono) self.telefonoBo = TelefonoBO.TelefonoBO() self.telefonoBo.consultarTelefono(self.telefono) self.consultarNombre() self.insertando = False msg.showinfo( "Acción: Consultar teléfono", "La información del teléfono ha sido consultada correctamente") except Exception as e: msg.showerror("Error", str(e)) def eliminarInformacion(self): try: datoSeleccionado = self.sheet.get_currently_selected() telefono = (self.sheet.get_cell_data(datoSeleccionado[0], 2)) nombre = (self.sheet.get_cell_data(datoSeleccionado[0], 1)) resultado = msg.askquestion( "Eliminar", "¿Desear eliminar el telefono " + telefono + " de " + nombre + " de la base de datos?") if resultado == "yes": self.telefono.telefono.set(telefono) self.telefonoBo = TelefonoBO.TelefonoBO() self.telefonoBo.eliminar(self.telefono) self.cargarTodaInformacion() self.telefono.limpiar() self.nombreCliente.set("") except Exception as e: msg.showerror("Error", str(e)) def mostrar_mant_cliente(self): self.parent.deiconify() self.raiz.destroy() def on_closing(self): self.parent.destroy()
text='Quit', command=master.quit).grid(row=4, column=0, sticky='nswe', pady=4) search_button = tk.Button(master, text='Start Search', command=start_search) search_button.grid(row=4, column=1, sticky='nswe', pady=4) tk.Label(master, text="Click any cell to open URL").grid(row=6, column=0) ignored_label_text = tk.StringVar() ignored_label = tk.Label(master, textvariable=ignored_label_text) ignored_label.grid(row=6, column=1) tk.Label(master, text="").grid(row=7, column=0) sheet = Sheet(master, headers=['Name', 'Address', 'URL', 'Vaccine Types', 'Distance (miles)', 'Staleness (mins)']) sheet.enable_bindings() sheet.extra_bindings('cell_select', func=cell_selected) sheet.grid(row=8, column=0, columnspan=2, sticky='nswe') for x in range(2): tk.Grid.columnconfigure(master, x, weight=1) for y in range(6): tk.Grid.rowconfigure(master, y, weight=1) master.mainloop()
class Aplicacion: def __init__(self): self.raiz = Tk() self.raiz.title ("Mantenimiento de Administración") self.raiz.geometry('900x600') menubar = Menu(self.raiz) self.raiz.config(menu=menubar) filemenu = Menu(menubar, tearoff=0) filemenu.add_command(label="Acerca de..") filemenu.add_separator() filemenu.add_command(label="Salir", command=self.raiz.quit) mantmenu = Menu(menubar, tearoff=0) mantmenu.add_command(label="Clientes", command=self.mostrar_mant_telefonos) mantmenu.add_command(label="Servidor") menubar.add_cascade(label="Archivo", menu=filemenu) menubar.add_cascade(label="Mantenimiento", menu=mantmenu) self.fuente = font.Font(weight="bold") self.persona = Persona.Persona() self.insertando = True self.lb_tituloPantalla = Label(self.raiz, text = "MANTENIMIENTO DE ADMINISTRACIÓN", font = self.fuente) self.lb_tituloPantalla.place(x = 320, y = 20) self.lb_cedula = Label(self.raiz, text = "Cedula:") self.lb_cedula.place(x = 240, y = 60) self.txt_cedula = Entry(self.raiz, textvariable=self.persona.cedula, justify="right") self.txt_cedula.place(x = 370, y = 60) self.lb_nombre = Label(self.raiz, text = "Nombre:") self.lb_nombre.place(x = 240, y = 90) self.txt_nombre = Entry(self.raiz, textvariable=self.persona.nombre, justify="right", width=30) self.txt_nombre.place(x = 370, y = 90) self.lb_apellido1 = Label(self.raiz, text = "Primer apellido:") self.lb_apellido1.place(x = 240, y = 120) self.txt_apellido1 = Entry(self.raiz, textvariable=self.persona.apellido1, justify="right", width=30) self.txt_apellido1.place(x = 370, y = 120) self.lb_apellido2 = Label(self.raiz, text = "Segundo apellido:") self.lb_apellido2.place(x = 240, y = 150) self.txt_apellido2 = Entry(self.raiz, textvariable=self.persona.apellido2, justify="right", width=30) self.txt_apellido2.place(x = 370, y = 150) self.lb_fec_nacimiento = Label(self.raiz, text = "Fecha nacimiento:") self.lb_fec_nacimiento.place(x = 240, y = 180) self.txt_fechaNacimiento = Entry(self.raiz, textvariable=self.persona.fecNacimiento, justify="right", width=30, state="readonly") self.txt_fechaNacimiento.place(x = 370, y = 180) self.bt_mostrarCalendario = Button(self.raiz, text="...", width=3, command = self.mostrarDatePicker) self.bt_mostrarCalendario.place(x = 650, y = 180) self.lb_sexo = Label(self.raiz, text = "Sexo:") self.lb_sexo.place(x = 240, y = 210) self.radio_sexoM = Radiobutton(self.raiz, text="Masculino", variable=self.persona.sexo, value=1) self.radio_sexoF = Radiobutton(self.raiz, text="Femenino", variable=self.persona.sexo, value=2) self.radio_sexoM.place(x = 370, y = 210) self.radio_sexoF.place(x = 490, y = 210) self.persona.sexo.set(1) self.lb_observaciones = Label(self.raiz, text = "Observaciones:") self.lb_observaciones.place(x = 240, y = 250) self.txt_observaciones = Entry(self.raiz, textvariable=self.persona.observaciones, justify="right", width=30) self.txt_observaciones.place(x = 370, y = 250) self.bt_borrar = Button(self.raiz, text="Limpiar", width=15, command = self.limpiarInformacion) self.bt_borrar.place(x = 370, y = 310) self.bt_enviar = Button(self.raiz, text="Enviar", width=15, command = self.enviarInformacion) self.bt_enviar.place(x = 510, y = 310) self.bt_modificar = Button(self.raiz, text="Modificar", width=15, command = self.enviarInformacion) self.bt_modificar.place(x = 650, y = 310) self.lb_tituloPantalla = Label(self.raiz, text = "INFORMACIÓN INCLUIDA", font = self.fuente) self.lb_tituloPantalla.place(x = 350, y = 355) self.lb_bloquear=Button(self.raiz,text="Bloquear",width=15,command=self.bloquear) self.lb_bloquear.place(x=650,y=330) self.desbloquear=Button(self.raiz,text="Desbloquear",width=15,command=self.desbloquear) self.lb_desbloquear.place(x=650,y=340) self.lb_iniciar_tiempo=Button(self.raiz,text="Iniciar tiempo",width=15,command=self.iniciar_tiempo) self.lb_iniciar_tiempo.place(x=650,y=350) self.lb_detener_tiempo=Button(self.raiz,text="Detener tiempo",width=15,command=self.detener_tiempo) self.lb_bloquear.place(x=650,y=360) self.lb_enviar_mensaje=Button(self.raiz,text="Enviar",width=15,command=self.enviarInformacion) self.lb_enviar_mensaje.place(x=650,y=370) self.sheet = Sheet(self.raiz, page_up_down_select_row = True, column_width = 120, startup_select = (0,1,"rows"), headers = ['Cédula', 'Nombre', 'Primer Ape.', 'Segundo Ape.', 'Fec. Nacimiento', 'Sexo'], height = 195, width = 720 ) self.sheet.enable_bindings(("single_select", "column_select", "row_select", "column_width_resize", "double_click_column_resize", #"row_width_resize", #"column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", "rc_insert_column", "rc_delete_column", "rc_insert_row", "rc_delete_row")) self.sheet.place(x = 20, y = 390) self.bt_cargar = Button(self.raiz, text="Cargar", width=15, command = self.cargarInformacion) self.bt_cargar.place(x = 750, y = 385) self.bt_eliminar = Button(self.raiz, text="Eliminar", width=15, command = self.eliminarInformacion) self.bt_eliminar.place(x = 750, y = 425) self.cargarTodaInformacion() self.raiz.mainloop() def mostrar_mant_telefonos(self): mant_telefonos.MantTelefonos(self.raiz) def cargarTodaInformacion(self): try: self.personaBo = PersonoBO.PersonaBO() resultado = self.personaBo.consultar() self.sheet.set_sheet_data(resultado) except Exception as e: msg.showerror("Error", str(e)) def cargarInformacion(self): try: datoSeleccionado = self.sheet.get_currently_selected() cedula = (self.sheet.get_cell_data(datoSeleccionado[0],0)) self.persona.cedula.set(cedula) self.personaBo = PersonoBO.PersonaBO() self.personaBo.consultarPersona(self.persona) self.insertando = False msg.showinfo("Acción: Consultar persona", "La información de la persona ha sido consultada correctamente") except Exception as e: msg.showerror("Error", str(e)) def eliminarInformacion(self): try: datoSeleccionado = self.sheet.get_currently_selected() cedula = (self.sheet.get_cell_data(datoSeleccionado[0],0)) nombre = (self.sheet.get_cell_data(datoSeleccionado[0],1)) resultado = msg.askquestion("Eliminar", "¿Desear eliminar a "+nombre+" de la base de datos?") if resultado == "yes": self.persona.cedula.set(cedula) self.personaBo = PersonoBO.PersonaBO() self.personaBo.eliminar(self.persona) self.cargarTodaInformacion() self.persona.limpiar() except Exception as e: msg.showerror("Error", str(e)) def printTxt(self, texto): print(texto) def enviarInformacion(self): try: self.personaBo = PersonoBO.PersonaBO() if(self.insertando == True): self.personaBo.guardar(self.persona) else: self.personaBo.modificar(self.persona) self.cargarTodaInformacion() self.persona.limpiar() if(self.insertando == True): msg.showinfo("Acción: Agregar persona", "La información de la persona ha sido incluida correctamente") else: msg.showinfo("Acción: Agregar modificar", "La información de la persona ha sido modificada correctamente") self.insertando = True except Exception as e: msg.showerror("Error", str(e)) def limpiarInformacion(self): self.persona.limpiar() self.insertando = True msg.showinfo("Acción del sistema", "La información del formulario ha sido eliminada correctamente") def mostrarDatePicker(self): self.top = Toplevel(self.raiz) self.cal = Calendar(self.top, font="Arial 14", selectmode='day', locale='en_US', cursor="hand", year=2019, month=6, day=16) self.cal.pack(fill="both", expand=True) ttk.Button(self.top, text="Seleccionar", command = self.seleccionarFecha).pack() def seleccionarFecha(self): self.persona.fecNacimiento.set(self.cal.selection_get())
def __init__(self): """ Window Configuration """ self.loan = Tk() self.window_height = 700 #Window Height self.window_width = 1100 #Window Width self.screen_width = self.loan.winfo_screenwidth() #Screen Width self.screen_height = self.loan.winfo_screenheight() #Screen Height self.x_cordinate = int((self.screen_width / 2) - (self.window_width / 2)) self.y_cordinate = int((self.screen_height / 2) - (self.window_height / 2)) self.loan.geometry("{}x{}+{}+{}".format( self.window_width, self.window_height, self.x_cordinate, 5)) #Implement and Center Window based on Device Screen self.loan.config(bg='#23374d') #Window Background self.loan.overrideredirect(True) #Remove Window Status Bar self.loan.resizable(False, False) #Disable Resizing Window """ Image Declaration """ imgExit = PhotoImage(file='tampilan/images/exit-btn.png') #Dashboard Icon Navbar dashimage = Image.open('tampilan/images/dashboard.png') dashImage = dashimage.resize((38, 38), Image.ANTIALIAS) dashboardIMG = ImageTk.PhotoImage(dashImage) #Customer Icon Navbar cImage = Image.open('tampilan/images/customersicon.png') cuImage = cImage.resize((50, 50), Image.ANTIALIAS) cusImage = ImageTk.PhotoImage(cuImage) #Saving Icon Navbar saveImg = Image.open('tampilan/images/savingicon.png') sImage = saveImg.resize((30, 30), Image.ANTIALIAS) savingImage = ImageTk.PhotoImage(sImage) #Checking Icon Navbar checkImg = Image.open('tampilan/images/checkingicon.png') chImage = checkImg.resize((30, 30), Image.ANTIALIAS) checkImage = ImageTk.PhotoImage(chImage) #Loan Icon Navbar loanImg = Image.open('tampilan/images/loanicon.png') lImage = loanImg.resize((30, 30), Image.ANTIALIAS) loanImage = ImageTk.PhotoImage(lImage) #Logout Icon Navbar logoutImg = Image.open('tampilan/images/logout.png') logImage = logoutImg.resize((30, 30), Image.ANTIALIAS) logoutImage = ImageTk.PhotoImage(logImage) ############################################################################## ############ SIDEBAR ########## ############################################################################## navbarLabel = Label(self.loan, bg='#e5e5e5', width=30, height=self.window_height) navbarLabel.place(x=0, y=0) #Dashboard Icon Navbar dashboardNavIcon = Label(self.loan, image=dashboardIMG, bg='#e5e5e5', cursor='hand2') dashboardNavIcon.place(x=15, y=23) dashboardNavIcon.bind("<Button>", self.bindingToDashboard) #Dashboard Label Navbar dashboardNavLabel = Label(self.loan, text="DASHBOARD", font=('Segoe UI', 16, BOLD), bg='#e5e5e5', fg='#23374d', cursor='hand2') dashboardNavLabel.place(x=65, y=25) dashboardNavLabel.bind("<Button>", self.bindingToDashboard) #Customer Icon Navbar allcusicon = Label(self.loan, image=cusImage, bg='#e5e5e5', cursor='hand2') allcusicon.place(x=10, y=73) allcusicon.bind("<Button>", self.bindingToCustomer) #Customer Label Navbar cusNavLabel = Label(self.loan, text="CUSTOMER", font=('Segoe UI', 16, BOLD), bg='#e5e5e5', fg='#23374d', cursor='hand2') cusNavLabel.place(x=65, y=80) cusNavLabel.bind("<Button>", self.bindingToCustomer) #Saving Account Icon Navbar savingNavIcon = Label(self.loan, image=savingImage, bg='#e5e5e5', cursor='hand2') savingNavIcon.place(x=15, y=155) savingNavIcon.bind("<Button>", self.bindingToSavingAccount) #Saving Account Label Navbar savingNavLabel = Label(self.loan, text="SAVING \n ACCOUNT", font=('Segoe UI', 16, BOLD), bg='#e5e5e5', fg='#23374d', cursor='hand2') savingNavLabel.place(x=55, y=140) savingNavLabel.bind("<Button>", self.bindingToSavingAccount) #Checking Account Icon Navbar checkNavIcon = Label(self.loan, image=checkImage, bg='#e5e5e5', cursor='hand2') checkNavIcon.place(x=15, y=245) checkNavIcon.bind("<Button>", self.bindingToCheckingAccount) #Checking Account Label Navbar checkNavLabel = Label(self.loan, text="CHECKING \n ACCOUNT", font=('Segoe UI', 16, BOLD), bg='#e5e5e5', fg='#23374d', cursor='hand2') checkNavLabel.place(x=55, y=230) checkNavLabel.bind("<Button>", self.bindingToCheckingAccount) #Loan Account Icon Navbar loanNavIcon = Label(self.loan, image=loanImage, bg='#e5e5e5', cursor='hand2') loanNavIcon.place(x=15, y=335) loanNavIcon.bind("<Button>", self.bindingToLoanAccount) #Loan Account Label Navbar loanNavLabel = Label(self.loan, text="LOAN \n ACCOUNT", font=('Segoe UI', 16, BOLD), bg='#e5e5e5', fg='#23374d', cursor='hand2') loanNavLabel.place(x=55, y=320) loanNavLabel.bind("<Button>", self.bindingToLoanAccount) #Logout Icon Navbar logoutNavIcon = Label(self.loan, image=logoutImage, bg='#e5e5e5', cursor='hand2') logoutNavIcon.place(x=10, y=650) logoutNavIcon.bind("<Button>", self.doLogout) #Logout Label Navbar logoutNavLabel = Label(self.loan, text="LOGOUT", font=('Segoe UI', 16, BOLD), bg='#e5e5e5', fg='#23374d', cursor='hand2') logoutNavLabel.place(x=50, y=650) logoutNavLabel.bind("<Button>", self.doLogout) #Customer Page Title cusTitle = Label(self.loan, text="Loan Account Bank", font=('Segoe UI', 20, BOLD), bg='#23374d', fg='#e5e5e5') cusTitle.place(x=550, y=20) #Exit Button Property exitButton = Button(self.loan, image=imgExit, border=0, bg='#23374d', activebackground='#23374d', command=self.loan.destroy, cursor='hand2') exitButton.place(x=980, y=650) ############################################################################## ############ LOAN ACCOUNT CONTENT ########## ############################################################################## loanAccount = DataLoanAccount() tmpData = loanAccount.getAllLoans() customer = DataCustomer() datas = [] for dt in tmpData: tmpCustomer = customer.getCustomerById(dt.getId()) tmpDt = (tmpCustomer.getName(), dt.getBalance()) datas.append(tmpDt) headerLabel = ["Customer Name", "Account Balance"] #Account Table frame = Frame(self.loan) frame.grid_columnconfigure(0, weight=1) frame.grid_rowconfigure(0, weight=1) sheet = Sheet( frame, page_up_down_select_row=True, column_width=120, startup_select=(0, 1, "rows"), data= datas, #[[f"{data_example[1][1]}" for c in range(2)] for r in range(5)], headers=[f"{c}" for c in headerLabel], theme="light green", height=480, width=700) sheet.enable_bindings( ("single_select", "drag_select", "select_all", "column_drag_and_drop", "row_drag_and_drop", "column_select", "row_select", "column_width_resize", "double_click_column_resize", "row_width_resize", "column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", "rc_insert_column", "rc_delete_column", "rc_insert_row", "rc_delete_row", "copy", "cut", "paste", "delete", "undo")) frame.place(x=280, y=150) sheet.grid(row=0, column=0, sticky="nswe") self.loan.mainloop()
def set_display_widgets(self, subjects, root_url, anchor): """ Create the table/sheet. Fill in the data for table content, Set the buttons and their states. """ cs = self.colorscheme sheet = Sheet( anchor, frame_bg=cs['table']['bg'], table_bg=cs['table']['bg'], table_fg=cs['table']['fg'], table_grid_fg=cs['table']['grid'], top_left_bg=cs['header']['bg'], top_left_fg=cs['header']['bg'], header_bg=cs['header']['bg'], header_fg=cs['header']['fg'], header_font=(self.conf.get("content_font"), 12, "bold"), font=(self.conf.get('content_font'), 14, "normal"), align='center', header_grid_fg=cs['table']['grid'], index_grid_fg=cs['table']['grid'], header_align='center', empty_horizontal=0, empty_vertical=0, header_border_fg=cs['table']['grid'], index_border_fg=cs['table']['grid'], ) self.sheet = sheet sheet.enable_bindings( ("single_select", "column_select", "column_width_resize", "row_height_resize", "rc_select")) self.set_headers() indexes = [x for x, v in self.columns.items() if v['show']] sheet.display_columns(indexes=indexes, enable=True) anchor.columnconfigure(0, weight=1) anchor.rowconfigure(0, weight=1) row = 0 for subject in subjects: videos = self.impartus.get_videos(root_url, subject) slides = self.impartus.get_slides(root_url, subject) video_slide_mapping = self.impartus.map_slides_to_videos( videos, slides) videos = {x['ttid']: x for x in videos} for ttid, video_metadata in videos.items(): video_metadata = Utils.add_fields(video_metadata, video_slide_mapping) video_metadata = Utils.sanitize(video_metadata) video_path = self.impartus.get_mkv_path(video_metadata) slides_path = self.impartus.get_slides_path(video_metadata) video_exists = os.path.exists(video_path) slides_exist = video_slide_mapping.get(ttid) slides_exist_on_disk, slides_path = self.impartus.slides_exist_on_disk( slides_path) metadata = { 'video_metadata': video_metadata, 'video_path': video_path, 'video_exists': video_exists, 'slides_exist': slides_exist, 'slides_exist_on_disk': slides_exist_on_disk, 'slides_url': video_slide_mapping.get(ttid), 'slides_path': slides_path, } row_items = list() button_states = list() for col, item in self.columns.items(): text = '' if item['type'] == 'auto': text = row if item['type'] == 'data': text = video_metadata[item['mapping']] # title case text = text.strip().title() if item.get( 'title_case') else text # truncate long fields if item['truncate'] and len(text) > self.conf.get( 'max_content_chars'): text = '{}..'.format( text[0:self.conf.get('max_content_chars')]) elif item['type'] == 'progressbar': value = 100 if video_exists else 0 text = self.progress_bar_text(value) elif item['type'] == 'button': button_states.append( self.get_button_state(self.names[col], video_exists, slides_exist, slides_exist_on_disk)) text = item.get('text') elif item['type'] == 'state': text = button_states.pop(0) elif item['type'] == 'metadata': text = metadata row_items.append(text) sheet.insert_row(values=row_items, idx='end') row += 1 self.reset_column_sizes() self.decorate() sheet.extra_bindings('column_select', self.sort_table) sheet.extra_bindings('cell_select', self.on_click_button_handler) # update button status self.set_button_status() sheet.grid(row=0, column=0, sticky='nsew')
class edit_oder_demo(tk.Tk): def __init__(self, oid): # oid will be set in edit_order() function in crud_main_window.py to be called from fill_data_from_db() self.oid = oid tk.Tk.__init__(self) self.grid_columnconfigure(0, weight=1) self.grid_rowconfigure(0, weight=1) self.frame = tk.Frame(self) self.fill_data_from_db() self.frame.grid_columnconfigure(0, weight=1) self.frame.grid_rowconfigure(0, weight=1) self.sheet = Sheet( self.frame, page_up_down_select_row=True, # empty_vertical = 0, column_width=120, startup_select=(0, 1, "rows"), headers=headers, data=data, height=500, # height and width arguments are optional width=550, # For full startup arguments see DOCUMENTATION.md ) self.sheet.enable_bindings(( "single_select", # "single_select" or "toggle_select" "drag_select", # enables shift click selection as well "column_drag_and_drop", "row_drag_and_drop", "column_select", "row_select", "column_width_resize", "double_click_column_resize", # "row_width_resize", # "column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", # "rc_insert_column", # "rc_delete_column", "rc_insert_row", "rc_delete_row", # "hide_columns", "copy", # "cut", # "paste", # "delete", "undo", "edit_cell")) self.frame.grid(row=0, column=0, sticky="nswe") self.sheet.grid(row=0, column=0, sticky="nswe") # __________ DISPLAY SUBSET OF COLUMNS __________ # self.sheet.display_subset_of_columns(indexes=[0, 1, 2], enable=True) # __________ BINDING A FUNCTIONS TO USER ACTIONS __________ self.sheet.extra_bindings([ ("end_edit_cell", self.end_edit_cell), ("begin_rc_delete_row", self.row_delete), ("end_insert_row", self.end_insert_row), ]) # __________ GETTING FULL SHEET DATA __________ # self.all_data = self.sheet.get_sheet_data() # __________ GETTING CELL DATA __________ # print (self.sheet.get_cell_data(0, 0)) # __________ GETTING ROW DATA __________ # print (self.sheet.get_row_data(0)) # only accessible by index # __________ GETTING COLUMN DATA __________ # print (self.sheet.get_column_data(0)) # only accessible by index def end_insert_row(self, event): print("cell inserted") try: oi = ORDERITEM(**{'oid': self.oid}) db_session.add(oi) db_session.commit() self.sheet.set_cell_data(event[1], 1, value=self.oid) self.sheet.set_cell_data(event[1], 0, value=oi.iid) self.sheet.dehighlight_rows(rows=[event[0]]) except Exception as e: self.sheet.highlight_rows(rows=[event[0]], fg='red', bg='red') e.with_traceback() def end_edit_cell(self, event): print("cell edited") ORDERITEM.query.filter_by(**{ "iid": self.sheet.get_cell_data(event[0], 0) }).update( {headers[event[1]]: self.sheet.get_cell_data(event[0], event[1])}) db_session.commit() def row_delete(self, event): print("row deleted") print({"iid": self.sheet.get_cell_data(event[1][0], 0)}) ORDERITEM.query.filter_by( **{ "iid": self.sheet.get_cell_data(event[1][0], 0) }).delete() db_session.commit() def fill_data_from_db(self): data.clear() ois = ORDERITEM.query.filter_by(oid=self.oid) k = [[ i.to_dict(rules=('-ORDER1', '-PRODUCT')).get(z) or 0 for z in headers ] for i in ois] data.extend(k)
class demo(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.grid_columnconfigure(0, weight=1) self.grid_rowconfigure(0, weight=1) self.frame = tk.Frame(self) self.frame.grid_columnconfigure(0, weight=1) self.frame.grid_rowconfigure(0, weight=1) self.sheet = Sheet( self.frame, page_up_down_select_row=True, #empty_vertical = 0, column_width=120, startup_select=(0, 1, "rows"), #row_height = "4", #default_row_index = "numbers", #default_header = "both", #empty_horizontal = 0, #show_vertical_grid = False, #show_horizontal_grid = False, #auto_resize_default_row_index = False, #header_height = "3", #row_index_width = 100, #align = "center", #header_align = "w", #row_index_align = "w", data=[[ f"Row {r}, Column {c}\nnewline1\nnewline2" for c in range(50) ] for r in range(1000)], #to set sheet data at startup #headers = [f"Column {c}\nnewline1\nnewline2" for c in range(30)], #row_index = [f"Row {r}\nnewline1\nnewline2" for r in range(2000)], #set_all_heights_and_widths = True, #to fit all cell sizes to text at start up #headers = 0, #to set headers as first row at startup #headers = [f"Column {c}\nnewline1\nnewline2" for c in range(30)], #theme = "light green", #row_index = 0, #to set row_index as first column at startup #total_rows = 2000, #if you want to set empty sheet dimensions at startup #total_columns = 30, #if you want to set empty sheet dimensions at startup height=500, #height and width arguments are optional width=1200 #For full startup arguments see DOCUMENTATION.md ) #self.sheet.hide("row_index") #self.sheet.hide("header") #self.sheet.hide("top_left") self.sheet.enable_bindings(( "single_select", #"single_select" or "toggle_select" "drag_select", #enables shift click selection as well "column_drag_and_drop", "row_drag_and_drop", "column_select", "row_select", "column_width_resize", "double_click_column_resize", #"row_width_resize", #"column_height_resize", "arrowkeys", "row_height_resize", "double_click_row_resize", "right_click_popup_menu", "rc_select", "rc_insert_column", "rc_delete_column", "rc_insert_row", "rc_delete_row", "copy", "cut", "paste", "delete", "undo", "edit_cell")) #self.sheet.disable_bindings() #uses the same strings #self.sheet.enable_bindings() self.frame.grid(row=0, column=0, sticky="nswe") self.sheet.grid(row=0, column=0, sticky="nswe") """_________________________ EXAMPLES _________________________ """ """_____________________________________________________________""" # __________ CHANGING THEME __________ #self.sheet.change_theme("light green") # __________ HIGHLIGHT / DEHIGHLIGHT CELLS __________ self.sheet.highlight_cells(row=5, column=5, bg="#ed4337", fg="white") self.sheet.highlight_cells(row=5, column=1, bg="#ed4337", fg="white") self.sheet.highlight_cells(row=5, bg="#ed4337", fg="white", canvas="row_index") self.sheet.highlight_cells(column=0, bg="#ed4337", fg="white", canvas="header") #self.sheet.highlight_rows(6, "green") #self.sheet.highlight_columns(4, "yellow") # __________ DISPLAY SUBSET OF COLUMNS __________ #self.sheet.display_subset_of_columns(indexes = [1, 0, 2, 10, 15, 20, 25, 35, 30, 36, 39, 9, 5, 11, 12, 43, 45], enable = True) #any order # __________ DATA AND DISPLAY DIMENSIONS __________ #self.sheet.total_rows(4) #will delete rows if set to less than current data rows #self.sheet.total_columns(2) #will delete columns if set to less than current data columns #self.sheet.sheet_data_dimensions(total_rows = 4, total_columns = 2) #self.sheet.sheet_display_dimensions(total_rows = 4, total_columns = 6) #currently resets widths and heights #self.sheet.set_sheet_data_and_display_dimensions(total_rows = 4, total_columns = 2) #currently resets widths and heights # __________ SETTING OR RESETTING TABLE DATA __________ #.set_sheet_data() function returns the object you use as argument #verify checks if your data is a list of lists, raises error if not #self.data = self.sheet.set_sheet_data([[f"Row {r} Column {c}" for c in range(30)] for r in range(2000)], verify = False) # __________ SETTING ROW HEIGHTS AND COLUMN WIDTHS __________ #self.sheet.set_cell_data(0, 0, "\n".join([f"Line {x}" for x in range(500)])) #self.sheet.set_column_data(1, ("" for i in range(2000))) #self.sheet.row_index((f"Row {r}" for r in range(2000))) #any iterable works #self.sheet.row_index("\n".join([f"Line {x}" for x in range(500)]), 2) #self.sheet.column_width(column = 0, width = 300) #self.sheet.row_height(row = 0, height = 60) #self.sheet.set_column_widths([120 for c in range(30)]) #self.sheet.set_row_heights([30 for r in range(2000)]) #self.sheet.set_all_column_widths() #self.sheet.set_all_row_heights() #self.sheet.set_all_cell_sizes_to_text() # __________ BINDING A FUNCTIONS TO USER ACTIONS __________ #self.sheet.extra_bindings([("cell_select", self.cell_select), # ("begin_edit_cell", self.begin_edit_cell), # ("end_edit_cell", self.end_edit_cell), # ("shift_cell_select", self.shift_select_cells), # ("drag_select_cells", self.drag_select_cells), # ("ctrl_a", self.ctrl_a), # ("row_select", self.row_select), # ("shift_row_select", self.shift_select_rows), # ("drag_select_rows", self.drag_select_rows), # ("column_select", self.column_select) # ("shift_column_select", self.shift_select_columns), # ("drag_select_columns", self.drag_select_columns), # ("deselect", self.deselect) # ]) #self.sheet.extra_bindings("bind_all", self.all_extra_bindings) #self.sheet.extra_bindings("begin_edit_cell", self.begin_edit_cell) #self.sheet.extra_bindings([("cell_select", None)]) #unbind cell select #self.sheet.extra_bindings("unbind_all") #remove all functions set by extra_bindings() # __________ BINDING NEW RIGHT CLICK FUNCTION __________ #self.sheet.bind("<3>", self.rc) # __________ SETTING HEADERS __________ #self.sheet.headers((f"Header {c}" for c in range(30))) #any iterable works #self.sheet.headers("Change header example", 2) #print (self.sheet.headers()) #print (self.sheet.headers(index = 2)) # __________ SETTING ROW INDEX __________ #self.sheet.row_index((f"Row {r}" for r in range(2000))) #any iterable works #self.sheet.row_index("Change index example", 2) #print (self.sheet.row_index()) #print (self.sheet.row_index(index = 2)) # __________ INSERTING A ROW __________ #self.sheet.insert_row(values = (f"my new row here {c}" for c in range(30)), idx = 0) # a filled row at the start #self.sheet.insert_row() # an empty row at the end # __________ INSERTING A COLUMN __________ #self.sheet.insert_column(values = (f"my new col here {r}" for r in range(2050)), idx = 0) # a filled column at the start #self.sheet.insert_column() # an empty column at the end # __________ SETTING A COLUMNS DATA __________ # any iterable works #self.sheet.set_column_data(0, values = (0 for i in range(2050))) # __________ SETTING A ROWS DATA __________ # any iterable works #self.sheet.set_row_data(0, values = (0 for i in range(35))) # __________ SETTING A CELLS DATA __________ #self.sheet.set_cell_data(1, 2, "NEW VALUE") # __________ GETTING FULL SHEET DATA __________ #self.all_data = self.sheet.get_sheet_data() # __________ GETTING CELL DATA __________ #print (self.sheet.get_cell_data(0, 0)) # __________ GETTING ROW DATA __________ #print (self.sheet.get_row_data(0)) # only accessible by index # __________ GETTING COLUMN DATA __________ #print (self.sheet.get_column_data(0)) # only accessible by index # __________ GETTING SELECTED __________ #print (self.sheet.get_currently_selected()) #print (self.sheet.get_selected_cells()) #print (self.sheet.get_selected_rows()) #print (self.sheet.get_selected_columns()) #print (self.sheet.get_selection_boxes()) #print (self.sheet.get_selection_boxes_with_types()) # __________ SETTING SELECTED __________ #self.sheet.deselect("all") #self.sheet.create_selection_box(0, 0, 2, 2, type_ = "cells") #type here is "cells", "cols" or "rows" #self.sheet.set_currently_selected(0, 0) #self.sheet.set_currently_selected("row", 0) #self.sheet.set_currently_selected("column", 0) # __________ CHECKING SELECTED __________ #print (self.sheet.cell_selected(0, 0)) #print (self.sheet.row_selected(0)) #print (self.sheet.column_selected(0)) #print (self.sheet.anything_selected()) #print (self.sheet.all_selected()) # __________ HIDING THE ROW INDEX AND HEADERS __________ #self.sheet.hide("row_index") #self.sheet.hide("top_left") #self.sheet.hide("header") # __________ ADDITIONAL BINDINGS __________ #self.sheet.bind("<Motion>", self.mouse_motion) """ UNTIL DOCUMENTATION IS COMPLETE, PLEASE BROWSE THE FILE _tksheet.py FOR A FULL LIST OF FUNCTIONS AND THEIR PARAMETERS """ def all_extra_bindings(self, event): print(event) def begin_edit_cell(self, event): print(event) # event[2] is keystroke return event[ 2] # return value is the text to be put into cell edit window def end_edit_cell(self, event): print(event) def window_resized(self, event): pass #print (event) def mouse_motion(self, event): region = self.sheet.identify_region(event) row = self.sheet.identify_row(event, allow_end=False) column = self.sheet.identify_column(event, allow_end=False) print(region, row, column) def deselect(self, event): print(event, self.sheet.get_selected_cells()) def rc(self, event): print(event) def cell_select(self, response): #print (response) pass def shift_select_cells(self, response): print(response) def drag_select_cells(self, response): pass #print (response) def ctrl_a(self, response): print(response) def row_select(self, response): print(response) def shift_select_rows(self, response): print(response) def drag_select_rows(self, response): pass #print (response) def column_select(self, response): print(response) #for i in range(50): # self.sheet.create_dropdown(i, response[1], values=[f"{i}" for i in range(200)], set_value="100", # destroy_on_select = False, destroy_on_leave = False, see = False) #print (self.sheet.get_cell_data(0, 0)) #self.sheet.refresh() def shift_select_columns(self, response): print(response) def drag_select_columns(self, response): pass
class columnselection(tk.Frame): def __init__(self,parent,C): tk.Frame.__init__(self,parent) self.C = C self.parent_cols = [] self.rowlen = 0 self.grid_rowconfigure(0,weight=1) self.grid_rowconfigure(1,weight=1) self.grid_columnconfigure(1,weight=1) self.flattened_choices = flattened_base_ids_choices(self,command=self.flattened_mode_toggle) self.flattened_choices.grid(row=1,column=0,pady=(0,5),sticky="wnse") self.flattened_selector = flattened_column_selector(self) self.selector = id_and_parent_column_selector(self) self.selector.grid(row=1,column=0,sticky="wnse") self.sheetdisplay = Sheet(self, theme = self.C.theme, header_font = ("Calibri", 13, "normal"), outline_thickness=1) self.sheetdisplay.enable_bindings("enable_all") self.sheetdisplay.extra_bindings([("row_index_drag_drop", self.drag_row), ("column_header_drag_drop", self.drag_col), ("ctrl_x", self.reset_selectors), ("delete_key", self.reset_selectors), ("rc_delete_column", self.reset_selectors), ("rc_delete_row", self.reset_selectors), ("rc_insert_column", self.reset_selectors), ("rc_insert_row", self.reset_selectors), ("ctrl_v", self.reset_selectors), ("ctrl_z", self.reset_selectors), ("edit_cell", self.edit_cell_in_sheet) ]) self.sheetdisplay.grid(row=0,column=1,rowspan=3,sticky="nswe") self.cont_ = button(self, text="Build tree with selections ", style="TF.Std.TButton",command=self.try_to_build_tree) self.cont_.grid(row=2,column=0,sticky="wns",padx=10,pady=(10, 50)) self.cont_.config(width=40) self.flattened_selector.grid(row=0,column=0,pady=(0,9),sticky="nswe") self.selector.grid_forget() self.selector.grid(row=0,column=0,sticky="nswe") self.flattened_selector.grid_forget() def flattened_mode_toggle(self): x = self.flattened_choices.get_choices()[0] if x: self.flattened_selector.grid(row=0,column=0,pady=(0,9),sticky="nswe") self.selector.grid_forget() else: self.selector.grid(row=0,column=0,sticky="nswe") self.flattened_selector.grid_forget() def drag_col(self, event): colsiter = sorted(event[1]) c = event[3] stins = colsiter[0] endins = colsiter[-1] + 1 self.selector.set_columns([h for h in self.sheetdisplay.get_sheet_data()[0]]) self.flattened_selector.set_columns([h for h in self.sheetdisplay.get_sheet_data()[0]]) self.selector.detect_id_col() self.selector.detect_par_cols() def drag_row(self, event): rowsiter = sorted(event[1]) r = event[3] stins = rowsiter[0] endins = rowsiter[-1] + 1 self.selector.set_columns([h for h in self.sheetdisplay.get_sheet_data()[0]]) self.flattened_selector.set_columns([h for h in self.sheetdisplay.get_sheet_data()[0]]) self.selector.detect_id_col() self.selector.detect_par_cols() def reset_selectors(self, event = None): idcol = self.selector.get_id_col() parcols = self.selector.get_par_cols() ancparcols = self.flattened_selector.get_par_cols() self.selector.set_columns([h for h in self.sheetdisplay.get_sheet_data()[0]] if self.sheetdisplay.get_sheet_data() else []) self.flattened_selector.set_columns([h for h in self.sheetdisplay.get_sheet_data()[0]] if self.sheetdisplay.get_sheet_data() else []) try: if idcol is not None and self.sheetdisplay.get_sheet_data(): self.selector.set_id_col(idcol) except: pass try: if parcols and self.sheetdisplay.get_sheet_data(): self.selector.set_par_cols(parcols) except: pass try: if ancparcols and self.sheetdisplay.get_sheet_data(): self.flattened_selector.set_par_cols(ancparcols) except: pass def edit_cell_in_sheet(self, event = None): idcol = self.selector.get_id_col() parcols = self.selector.get_par_cols() ancparcols = self.flattened_selector.get_par_cols() if event[1] == idcol or event[1] in parcols or event[1] in ancparcols or event[0] == 0: self.reset_selectors() def enable_widgets(self): self.selector.enable_me() self.flattened_selector.enable_me() self.flattened_choices.enable_me() self.cont_.config(state="normal") self.sheetdisplay.basic_bindings(True) self.sheetdisplay.enable_bindings("enable_all") self.sheetdisplay.extra_bindings([("row_index_drag_drop", self.drag_row), ("column_header_drag_drop", self.drag_col), ("ctrl_x", self.ctrl_x_in_sheet), ("delete_key", self.del_in_sheet), ("rc_delete_column", self.del_in_sheet), ("rc_delete_row", self.del_in_sheet), ("rc_insert_column", self.reset_selectors), ("rc_insert_row", self.reset_selectors), ("ctrl_v", self.ctrl_v_in_sheet), ("ctrl_z", self.ctrl_z_in_sheet), ("edit_cell", self.edit_cell_in_sheet) ]) def disable_widgets(self): self.selector.disable_me() self.flattened_selector.disable_me() self.flattened_choices.disable_me() self.cont_.config(state="disabled") self.sheetdisplay.basic_bindings(False) self.sheetdisplay.disable_bindings("disable_all") self.sheetdisplay.extra_bindings("unbind_all") self.sheetdisplay.unbind("<Delete>") def populate(self, columns, non_tsrgn_xl_file = False): self.sheetdisplay.deselect("all") self.non_tsrgn_xl_file = non_tsrgn_xl_file self.rowlen = len(columns) self.selector.set_columns([h for h in self.C.treeframe.sheet[0]]) self.flattened_selector.set_columns([h for h in self.C.treeframe.sheet[0]]) self.C.treeframe.sheet = self.sheetdisplay.set_sheet_data(data = self.C.treeframe.sheet, redraw=True) self.sheetdisplay.headers(newheaders=0) if len(self.C.treeframe.sheet) < 3000: self.sheetdisplay.set_all_cell_sizes_to_text() self.selector.detect_id_col() self.selector.detect_par_cols() self.flattened_selector.detect_par_cols() self.C.show_frame("columnselection") def try_to_build_tree(self): baseids, order, delcols = self.flattened_choices.get_choices() if baseids: hiers = list(self.flattened_selector.get_par_cols()) if len(hiers) < 2: return else: hiers = list(self.selector.get_par_cols()) if not hiers: return idcol = self.selector.get_id_col() if idcol in hiers or idcol is None: return self.C.status_bar.change_text("Loading... ") self.C.disable_at_start() self.C.treeframe.sheet = self.sheetdisplay.get_sheet_data() if baseids: if order == "Order: Base → Top": idcol = hiers.pop(0) elif order == "Order: Top → Base": idcol = hiers.pop(len(hiers) - 1) self.C.treeframe.sheet[:] = [row + list(repeat("",self.rowlen - len(row))) if len(row) < self.rowlen else row for row in self.C.treeframe.sheet] self.C.treeframe.sheet, self.rowlen, idcol, newpc = self.C.treeframe.treebuilder.convert_flattened_to_normal(data = self.C.treeframe.sheet, idcol = idcol, parcols = hiers, rowlen = self.rowlen, order = order, delcols = delcols, warnings = self.C.treeframe.warnings) hiers = [newpc] self.C.treeframe.headers = [Header(name) for name in self.C.treeframe.fix_heads(self.C.treeframe.sheet.pop(0),self.rowlen)] self.C.treeframe.ic = idcol self.C.treeframe.hiers = hiers self.C.treeframe.pc = hiers[0] self.C.treeframe.row_len = int(self.rowlen) self.C.treeframe.set_metadata(headers = True) self.C.treeframe.sheet, self.C.treeframe.nodes, self.C.treeframe.warnings = self.C.treeframe.treebuilder.build(self.C.treeframe.sheet, self.C.treeframe.new_sheet, self.C.treeframe.row_len, self.C.treeframe.ic, self.C.treeframe.hiers, self.C.treeframe.nodes, self.C.treeframe.warnings) self.C.treeframe.populate(non_tsrgn_xl_file = self.non_tsrgn_xl_file) self.C.treeframe.show_warnings(str(self.C.open_dict['filepath']),str(self.C.open_dict['sheet']))