Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- # método place
- #
- # Script que implementa un formulario de inscripción a cursos
- #
- import tkinter as tk
- from tkinter import ttk
- from tkinter import messagebox
- from pprint import pprint
- def guardar():
- global alumnos
- # leo los datos
- nombre = caja_nombre.get()
- if nombre.isspace() or not nombre:
- messagebox.showerror(title="ERROR", message="Nombre inválido")
- return 1
- edad = caja_edad.get()
- if not edad.isdecimal() or int(edad) <= 0 or int(edad) > 100:
- messagebox.showerror(title="ERROR", message="Edad incorrecta")
- return 1
- dni = caja_dni.get()
- if not dni.isdecimal() or int(dni) <= 0 or int(dni) > 100_000_000:
- messagebox.showerror(title="ERROR", message="DNI inválido")
- return 1
- email = caja_email.get()
- if not "@" in email:
- messagebox.showerror(title="ERROR", message="Email inválido")
- return 1
- curso = lista_cursos.get()
- if not curso:
- messagebox.showerror(title="ERROR", message="Seleccione un curso")
- return 1
- aceptacion = estado.get()
- if not aceptacion:
- messagebox.showwarning(title="Advertencia", message="Debe aceptar los términos y condiciones")
- return 1
- # guardo los datos
- alumno = {
- "nombre": nombre,
- "edad": edad,
- "dni": dni,
- "email": email,
- "curso": curso
- }
- alumnos.append(alumno)
- # muestro los datos
- print()
- pprint(alumnos)
- # borro el formulario
- caja_nombre.delete(0,tk.END)
- caja_email.delete(0,tk.END)
- caja_dni.delete(0,tk.END)
- caja_edad.delete(0,tk.END)
- estado.set("False")
- # Datos: una lista de diccionarios (cada dicc es un alumno)
- alumnos = []
- ############# Parte gráfica ####################
- # creo la ventana de mi formulario
- form = tk.Tk()
- form.title("Formulario de inscripción")
- form.config(width=400, height=400)
- # evito que mi formulario sea redimensionable
- form.resizable(False,False)
- # campo nombre
- label = ttk.Label(text="Nombre")
- label.place(x=20, y=20)
- caja_nombre = ttk.Entry()
- caja_nombre.place(x=120, y=20, width=250, height=25)
- # campo edad
- label = ttk.Label(text="Edad")
- label.place(x=20, y=70)
- caja_edad = ttk.Entry()
- caja_edad.place(x=120, y=70, width=250, height=25)
- # campo dni
- label = ttk.Label(text="Dni")
- label.place(x=20, y=120)
- caja_dni = ttk.Entry()
- caja_dni.place(x=120, y=120, width=250, height=25)
- # campo email
- label = ttk.Label(text="Email")
- label.place(x=20, y=170)
- caja_email = ttk.Entry()
- caja_email.place(x=120, y=170, width=250, height=25)
- # campo cursos
- label = ttk.Label(text="Cursos")
- label.place(x=20, y=220)
- lista_cursos = ttk.Combobox(
- state = 'readonly',
- values = ["C++", "Java", "Python", "JavaScript", "HTML"]
- )
- lista_cursos.place(x=120, y=220, width=250, height=25)
- # casilla de verificación
- estado = tk.BooleanVar()
- estado.set("False")
- casilla = ttk.Checkbutton(text="Acepto términos y condiciones",variable=estado)
- casilla.place(x=80, y=270)
- # boton
- boton = ttk.Button(text="Guardar", command=guardar)
- boton.place(x=140, y=320, width=120, height=40)
- form.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement