def __init__(self): '''Crea la pantalla inicial, mostrando botones y notas''' self.ventana_principal = tkinter.Tk() self.anotador = Anotador() self.ventana_principal.title("Anotador") botonAgregar=tkinter.Button(self.ventana_principal,text="Agregar nota", command = self.agregar_nota).grid(row=0, column=0) botonModificar=tkinter.Button(self.ventana_principal,text="Modificar", command = self.modificar_nota).grid(row=0, column=1) botonEliminar=tkinter.Button(self.ventana_principal,text="Eliminar", command = self.eliminar_nota).grid(row=0, column=2) tkinter.Label(self.ventana_principal,text="Buscar").grid(row=1,column=0) self.cajaBuscar = tkinter.Entry(self.ventana_principal) self.cajaBuscar.grid(row=1, column=1) botonBuscar = tkinter.Button(self.ventana_principal, text="Buscar", command = self.buscar_notas).grid(row=1, column=2) self.treeview = ttk.Treeview(self.ventana_principal, columns=("texto","etiquetas")) self.treeview.heading("#0",text="id") self.treeview.column("#0",minwidth=0, width=40) self.treeview.heading("texto",text="Texto") self.treeview.heading("etiquetas",text="Etiquetas") self.treeview.grid(row=2, columnspan=3) botonSalir = tkinter.Button(self.ventana_principal, text = "Salir", command = self.ventana_principal.destroy) botonSalir.grid(row=3, column=1)
def __init__(self): self.anotador = Anotador() self.opciones = { "1": self.mostrar_notas, "2": self.buscar_notas, "3": self.agregar_nota, "4": self.modificar_nota, "5": self.salir }
class Gui(): '''Provee una interfaz gráfica de usuario al anotador''' def __init__(self): '''Crea la pantalla inicial, mostrando botones y notas''' self.ventana_principal = tkinter.Tk() self.anotador = Anotador() self.ventana_principal.title("Anotador") botonAgregar = tkinter.Button(self.ventana_principal, text="Agregar nota", command=self.agregar_nota).grid(row=0, column=0) botonModificar = tkinter.Button(self.ventana_principal, text="Modificar", command=self.modificar_nota).grid( row=0, column=1) botonEliminar = tkinter.Button(self.ventana_principal, text="Eliminar", command=self.eliminar_nota).grid( row=0, column=2) tkinter.Label(self.ventana_principal, text="Buscar").grid(row=1, column=0) self.cajaBuscar = tkinter.Entry(self.ventana_principal) self.cajaBuscar.grid(row=1, column=1) botonBuscar = tkinter.Button(self.ventana_principal, text="Buscar", command=self.buscar_notas).grid(row=1, column=2) self.treeview = ttk.Treeview(self.ventana_principal, columns=("texto", "etiquetas"), selectmode='browse') self.treeview.heading("#0", text="id") self.treeview.column("#0", minwidth=0, width=40) self.treeview.heading("texto", text="Texto") self.treeview.heading("etiquetas", text="Etiquetas") self.treeview.grid(row=2, columnspan=3) botonSalir = tkinter.Button(self.ventana_principal, text="Salir", command=self.ventana_principal.destroy) botonSalir.grid(row=3, column=1) self.poblar_tabla() def agregar_nota(self): '''Agrega una nueva nota a la lista de notas''' self.modal_agregar = tkinter.Toplevel(self.ventana_principal) self.modal_agregar.grab_set() tkinter.Label(self.modal_agregar, text="Nota: ").grid() self.texto = tkinter.Entry(self.modal_agregar) self.texto.grid(row=0, column=1, columnspan=2) self.texto.focus() tkinter.Label(self.modal_agregar, text="Etiquetas: ").grid(row=1) self.etiquetas = tkinter.Entry(self.modal_agregar) self.etiquetas.grid(row=1, column=1, columnspan=2) boton_ok = tkinter.Button(self.modal_agregar, text="Guardar", command=self.agregar_ok) boton_ok.grid(row=2) self.modal_agregar.bind("<Return>", self.agregar_ok) boton_cancelar = tkinter.Button(self.modal_agregar, text="Cancelar", command=self.modal_agregar.destroy) boton_cancelar.grid(row=2, column=2) def agregar_ok(self): '''Guarda en la lista de notas y muestra en el treeview la nota recientemente agregada en agregar_nota''' nota = self.anotador.nueva_nota(self.texto.get(), self.etiquetas.get()) self.modal_agregar.destroy() self.treeview.insert("", tkinter.END, text=nota.id, values=(nota.texto, nota.etiquetas)) def modificar_nota(self): '''Modifica la nota seleccionada (texto o etiquetas)''' if not self.treeview.selection(): messagebox.showwarning( "Sin selección", "Seleccione primero qué nota quiere modificar") return False i = self.treeview.selection() id = self.treeview.item(i)['text'] nota = self.anotador._buscar_por_id(id) self.modal_modificar = tkinter.Toplevel(self.ventana_principal) self.modal_modificar.grab_set() tkinter.Label(self.modal_modificar, text="Nota: ").grid() self.texto = tkinter.Entry(self.modal_modificar) self.texto.insert(0, nota.texto) self.texto.grid(row=0, column=1, columnspan=2) self.texto.focus() tkinter.Label(self.modal_modificar, text="Etiquetas: ").grid(row=1) self.etiquetas = tkinter.Entry(self.modal_modificar) self.etiquetas.insert(0, nota.etiquetas) self.etiquetas.grid(row=1, column=1, columnspan=2) boton_ok = tkinter.Button(self.modal_modificar, text="Modificar", command=self.modificar_ok) self.modal_modificar.bind("<Return>", self.modificar_ok) boton_ok.grid(row=2) boton_cancelar = tkinter.Button(self.modal_modificar, text="Cancelar", command=self.modal_modificar.destroy) boton_cancelar.grid(row=2, column=2) def modificar_ok(self): '''Modifica en la lista de notas y muestra en el treeview la nota recientemente modificada en modificar_nota''' i = self.treeview.selection() id = self.treeview.item(i)['text'] self.anotador.modificar_nota(id, self.texto.get()) self.anotador.modificar_etiquetas(id, self.etiquetas.get()) self.poblar_tabla() self.modal_modificar.destroy() def eliminar_nota(self): '''Elimina la nota seleccionada''' if not self.treeview.selection(): messagebox.showwarning( "Sin selección", "Seleccione primero qué nota quiere eliminar") return False i = self.treeview.selection() id = self.treeview.item(i)['text'] resp = messagebox.askokcancel("Confirmar", "¿Está seguro de eliminar la nota?") if resp: if self.anotador.eliminar_nota(id): self.treeview.delete(i) return True return False def buscar_notas(self): '''Busca y muestra las notas que coincidan con un filtro de búsqueda dado''' filtro = self.cajaBuscar.get() notas = self.anotador.buscar(filtro) if notas: self.poblar_tabla(notas) else: messagebox.showwarning("Sin resultados", "Ninguna nota coincide con la búsqueda") def poblar_tabla(self, notas=None): '''Recibe una lista de notas. Vacía el treeview y lo completa con la lista de notas recibidas. Si no recibe ninguna lista (o recibe la lista vacía), completa el treeview con la lista de notas completa''' # Vaciar el treeview for i in self.treeview.get_children(): self.treeview.delete(i) # Si no recibió la lista de notas, trabajará con la lista completa if not notas: notas = self.anotador.notas for nota in notas: self.treeview.insert("", tkinter.END, text=nota.id, values=(nota.texto, nota.etiquetas), iid=nota.id)
class Menu: '''Mostrar un menú y responder a las opciones''' def __init__(self): self.anotador = Anotador() self.opciones = { "1": self.mostrar_notas, "2": self.buscar_notas, "3": self.agregar_nota, "4": self.modificar_nota, "5": self.salir } def mostrar_menu(self): print(""" Menú del anotador: 1. Mostrar todas las notas 2. Buscar Notas 3. Agregar Nota 4. Modificar Nota 5. Salir """) def ejecutar(self): '''Mostrar el menu y responder a las opciones.''' while True: self.mostrar_menu() opcion = input("Ingresar una opción: ") accion = self.opciones.get(opcion) if accion: accion() else: print("{0} no es una opción válida".format(opcion)) def mostrar_notas(self, notas=None): if not notas: notas = self.anotador.notas for nota in notas: print("{0}: {1}\n{2}".format(nota.id, nota.etiquetas, nota.texto)) def buscar_notas(self): filtro = input("Buscar: ") notas = self.anotador.buscar(filtro) if notas: self.mostrar_notas(notas) else: print("Ninguna nota coincide con la búsqueda") def agregar_nota(self): texto = input("Ingrese el texto de la nota: ") self.anotador.nueva_nota(texto) print("Su nota ha sido añadida.") def modificar_nota(self): id = input("Ingrese el id de la nota a modificar: ") texto = input("Ingrese el texto de la nota: ") etiquetas = input("Ingrese las etiquetas: ") if texto: self.anotador.modificar_nota(id, texto) if etiquetas: self.anotador.modificar_etiquetas(id, etiquetas) def salir(self): print("Gracias por utilizar el sistema.") sys.exit(0)
class Gui(): '''Provee una interfaz gráfica de usuario al anotador''' def __init__(self): '''Crea la pantalla inicial, mostrando botones y notas''' self.ventana_principal = tkinter.Tk() self.anotador = Anotador() self.ventana_principal.title("Anotador") botonAgregar=tkinter.Button(self.ventana_principal,text="Agregar nota", command = self.agregar_nota).grid(row=0, column=0) botonModificar=tkinter.Button(self.ventana_principal,text="Modificar", command = self.modificar_nota).grid(row=0, column=1) botonEliminar=tkinter.Button(self.ventana_principal,text="Eliminar", command = self.eliminar_nota).grid(row=0, column=2) tkinter.Label(self.ventana_principal,text="Buscar").grid(row=1,column=0) self.cajaBuscar = tkinter.Entry(self.ventana_principal) self.cajaBuscar.grid(row=1, column=1) botonBuscar = tkinter.Button(self.ventana_principal, text="Buscar", command = self.buscar_notas).grid(row=1, column=2) self.treeview = ttk.Treeview(self.ventana_principal, columns=("texto","etiquetas")) self.treeview.heading("#0",text="id") self.treeview.column("#0",minwidth=0, width=40) self.treeview.heading("texto",text="Texto") self.treeview.heading("etiquetas",text="Etiquetas") self.treeview.grid(row=2, columnspan=3) botonSalir = tkinter.Button(self.ventana_principal, text = "Salir", command = self.ventana_principal.destroy) botonSalir.grid(row=3, column=1) def agregar_nota(self): '''Agrega una nueva nota a la lista de notas''' ''' Construye una ventana de diálogo ''' self.dialogonota = tkinter.Toplevel() self.dialogonota.title("Agregar nota") boton_cerrar = tkinter.Button(self.dialogonota, text='Cerrar', command=self.dialogonota.destroy) boton_cerrar.grid(row=3, column=3) boton_agregar = tkinter.Button(self.dialogonota, text='Agregar', command=self.guardar) boton_agregar.grid(row=3, column=2) self.cajanota = tkinter.Entry(self.dialogonota) self.cajanota.grid(row=0, column=1) tkinter.Label(self.dialogonota,text="Nota").grid(row=0,column=0) self.cajaetiqueta = tkinter.Entry(self.dialogonota) self.cajaetiqueta.grid(row=1, column=1) tkinter.Label(self.dialogonota,text="etiqueta").grid(row=1,column=0) self.dialogonota.grab_set() self.ventana_principal.wait_window(self.dialogonota) def guardar(self): nota=self.anotador.nueva_nota(self.cajanota.get(),self.cajaetiqueta.get()) self.dialogonota.destroy() self.treeview.insert("",tkinter.END,text=nota.id, values=(nota.texto,nota.etiquetas),iid=nota.id) def modificar_nota(self): '''Modifica la nota seleccionada (texto o etiquetas)''' pass def eliminar_nota(self): '''Elimina la nota seleccionada''' pass def buscar_notas(self): '''Busca y muestra las notas que coincidan con un filtro de búsqueda dado''' filtro = self.cajaBuscar.get() notas = self.anotador.buscar(filtro) if notas: self.poblar_tabla(notas) else: messagebox.showwarning("sin resultados","la busqueda no arrojo resultados") def poblar_tabla(self,notas=None): '''limpia el treeview y desoues agrega las notas que recibe en "notas" ,sino recibe nada trabaja con todas las notas''' #limpiar treeview for i in self.treeview.get_children(): self.treeview.delete(i) #insertar datos de notas if not notas: notas=self.anotador.notas else: for nota in notas: self.treeview.insert("",tkinter.END,text=nota.id, values=(nota.texto,nota.etiquetas),iid=nota.id)
class Menu: def __init__(self): self.anotador = Anotador() self.opciones = { "1": self.mostrarNotas, "2": self.buscarNotas, "3": self.agregarNota, "4": self.modificarNota, "5": self.salir } def display_menu(self): print(""" Anotador-Menu: 1. Mostrar las notas 2. Buscar notas 3. Agregar nota 4. Modificar nota 5. Salir """) def run(self): '''Muestra el menu y responde a las opciones.''' while True: self.display_menu() opcion = input("Ingrese una opcion: ") accion = self.opciones.get(opcion) if accion: accion() else: print(f"{opcion} no es valido.") def mostrarNotas(self, notas=None): if not notas: notas = self.anotador.notas for nota in notas: print(f"{nota.id}: {nota.tags} \n{nota.texto}") def buscarNotas(self): filtro = input("Busqueda: ") notas = self.anotador.buscar(filtro) self.mostrarNotas(notas) def agregarNota(self): texto = input("Escriba la nota: ") tags = input("Ingrese sus tags: ") self.anotador.nuevaNota(texto, tags) print("Su nota ha sido agregada.") def modificarNota(self): id = int(input("Ingrese el id de la nota: ")) nota = self.anotador.buscarNota(id) if nota: texto = input("Ingrese el texto: ") tags = input("Ingrese sus tags: ") if texto: nota.texto = texto if tags: nota.tags = tags print("Se a modificado la nota con exito.") else: print(f"No existe la nota de id:{id}") def salir(self): print("Gracias por usar el cuaderno de notas.") sys.exit(0)
class Menu: '''Mostrar un menú y responder a las opciones''' def __init__(self): self.anotador = Anotador() self.opciones= { "1": self.mostrar_notas, "2": self.buscar_notas, "3": self.agregar_nota, "4": self.modificar_nota, "5": self.salir } def mostrar_menu(self): print(""" Menú del anotador: 1. Mostrar todas las notas 2. Buscar Notas 3. Agregar Nota 4. Modificar Nota 5. Salir """) def ejecutar(self): '''Mostrar el menu y responder a las opciones.''' while True: self.mostrar_menu() opcion = input("Ingresar una opción: ") accion = self.opciones.get(opcion) if accion: accion() else: print("{0} no es una opción válida".format(opcion)) def mostrar_notas(self, notas=None): '''Si recibe como parámetro una lista de notas, muestra id, texto y etiquetas de esas notas. Si no recibe el parámetro, muestra id, texto y etiquetas de todas las notas''' #TODO: Construir este método, borrar el siguiente renglón: if notas: for nota in notas: print("ID: "+str(nota.id)) print("TEXTO: "+nota.texto) print("ETIQUETAS: "+nota.etiquetas) else: for nota in self.anotador.notas: print("ID: "+ str(nota.id)) print("TEXTO: "+nota.texto) print("ETIQUETAS: "+nota.etiquetas) def buscar_notas(self): filtro = input("Buscar: ") notas = self.anotador.buscar(filtro) if notas: self.mostrar_notas(notas) else: print("Ninguna nota coincide con la búsqueda") def agregar_nota(self): '''Solicita un texto al usuario y agrega una nueva nota con ese texto''' #TODO: Construir este método, borrar el siguiente renglón: texto=input("ingresar el texto:") self.anotador.nueva_nota(texto) def modificar_nota(self): id = input("Ingrese el id de la nota a modificar: ") texto = input("Ingrese el texto de la nota: ") etiquetas = input("Ingrese las etiquetas: ") if texto: self.anotador.modificar_nota(id, texto) if etiquetas: self.anotador.modificar_etiquetas(id, etiquetas) def salir(self): print("Gracias por utilizar el sistema.") sys.exit(0)