Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- import tkinter as tk
- from tkinter import ttk
- from tkinter import messagebox
- from pprint import pprint
- """
- alumnos = [
- {"nombre":"Juan", "email":"[email protected]", "edad":23, "curso":"Python"},
- {"nombre":"Ana", "email":"[email protected]", "edad":19, "curso":"HTML"},
- ]
- """
- alumnos = []
- def borrar_formulario():
- caja_nombre.delete(0,tk.END)
- caja_email.delete(0,tk.END)
- caja_edad.delete(0,tk.END)
- def guardar():
- global alumnos
- # leo los datos del formulario
- nombre = caja_nombre.get()
- if not nombre or not nombre.isalpha():
- messagebox.showerror(title="ERROR", message="Nombre inválido")
- return 1
- email = caja_email.get()
- if not "@" in email or not "." in email:
- messagebox.showerror(title="ERROR", message="Email inválido")
- return 1
- edad = caja_edad.get()
- if not edad.isdecimal() or int(edad) <= 11 or int(edad) >=100:
- print(type(edad))
- print(edad)
- messagebox.showerror(title="ERROR", message="Edad inválida")
- return 1
- else:
- edad = int(edad)
- curso = caja_curso.get()
- if not curso:
- messagebox.showwarning(title="ADVERTENCIA", message="Elija un curso")
- return 1
- # creo el dicc con los datos
- persona = {
- "nombre":nombre, "email":email, "edad":edad, "curso":curso
- }
- # agrego la persona a la lista
- alumnos.append(persona)
- # imprimo la lista de alumnos
- print("Lista de alumnos")
- pprint(alumnos)
- # borro el formulario
- borrar_formulario()
- ventana = tk.Tk()
- ventana.title("Formulario de inscripción")
- ventana.config(width=400, height=320, bg='Slategray2')
- ventana.resizable(False,False)
- # icono
- icono = tk.PhotoImage(file="energia.png")
- ventana.iconphoto(True, icono)
- # campo nombre
- etiqueta = tk.Label(text="Nombre", bg='Slategray2')
- etiqueta.place(x=20, y=20)
- caja_nombre = tk.Entry(justify="center")
- caja_nombre.place(x=120, y=20, width=200, height=25)
- # campo email
- etiqueta = tk.Label(text="Email",bg='Slategray2')
- etiqueta.place(x=20, y=70)
- caja_email = tk.Entry(justify="center")
- caja_email.place(x=120, y=70, width=200, height=25)
- # campo edad
- etiqueta = tk.Label(text="Edad",bg='Slategray2')
- etiqueta.place(x=20, y=120)
- caja_edad = tk.Entry(
- justify="center",
- font = ("arial",16,'italic'),
- fg = "red",
- bd = 4
- )
- caja_edad.place(x=120, y=120, width=200, height=25)
- # campo curso (se implementa con una lista desplegable)
- # el widget es Combobox
- etiqueta = tk.Label(text="Curso",bg='Slategray2')
- etiqueta.place(x=20, y=170)
- caja_curso = ttk.Combobox(
- state = 'readonly',
- values = ['Visual Basic', 'Python', 'HTML', 'JavaScript']
- )
- caja_curso.place(x=120,y=170, width=200, height=25)
- # boton para guardar el formulario
- boton = tk.Button(text="Guardar", command=guardar)
- boton.place(x=170, y=220, width=100, height=45)
- ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement