Advertisement
teslariu

tkinter formulario

Sep 7th, 2023
1,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import tkinter as tk
  5. from tkinter import ttk
  6. from tkinter import messagebox
  7. from pprint import pprint
  8. """
  9. alumnos = [
  10.     {"nombre":"Juan", "email":"juan@gmail.com", "edad":23, "curso":"Python"},
  11.     {"nombre":"Ana", "email":"anita@gmail.com", "edad":19, "curso":"HTML"},
  12. ]
  13.  
  14. """
  15.  
  16. alumnos = []
  17.  
  18.  
  19. def borrar_formulario():
  20.     caja_nombre.delete(0,tk.END)
  21.     caja_email.delete(0,tk.END)
  22.     caja_edad.delete(0,tk.END)
  23.  
  24.  
  25.  
  26.  
  27. def guardar():
  28.     global alumnos
  29.    
  30.     # leo los datos del formulario
  31.     nombre = caja_nombre.get()
  32.     if not nombre or not nombre.isalpha():
  33.         messagebox.showerror(title="ERROR", message="Nombre inválido")
  34.         return 1
  35.    
  36.     email = caja_email.get()
  37.     if not "@" in email or not "." in email:
  38.         messagebox.showerror(title="ERROR", message="Email inválido")
  39.         return 1       
  40.    
  41.    
  42.     edad = caja_edad.get()
  43.     if not edad.isdecimal() or int(edad) <= 11 or int(edad) >=100:
  44.         print(type(edad))
  45.         print(edad)
  46.         messagebox.showerror(title="ERROR", message="Edad inválida")
  47.         return 1
  48.     else:
  49.         edad = int(edad)
  50.    
  51.    
  52.     curso = caja_curso.get()
  53.     if not curso:
  54.         messagebox.showwarning(title="ADVERTENCIA", message="Elija un curso")
  55.         return 1
  56.        
  57.    
  58.     # creo el dicc con los datos
  59.     persona = {
  60.         "nombre":nombre, "email":email, "edad":edad, "curso":curso
  61.     }
  62.  
  63.     # agrego la persona a la lista
  64.     alumnos.append(persona)
  65.    
  66.     # imprimo la lista de alumnos
  67.     print("Lista de alumnos")
  68.     pprint(alumnos)
  69.    
  70.     # borro el formulario
  71.     borrar_formulario()
  72.    
  73.  
  74.  
  75.  
  76. ventana = tk.Tk()
  77. ventana.title("Formulario de inscripción")
  78. ventana.config(width=400, height=320, bg='Slategray2')
  79. ventana.resizable(False,False)
  80.  
  81. # icono
  82. icono = tk.PhotoImage(file="energia.png")
  83. ventana.iconphoto(True, icono)
  84.  
  85. # campo nombre
  86. etiqueta = tk.Label(text="Nombre", bg='Slategray2')
  87. etiqueta.place(x=20, y=20)
  88. caja_nombre = tk.Entry(justify="center")
  89. caja_nombre.place(x=120, y=20, width=200, height=25)
  90.  
  91.  
  92. # campo email
  93. etiqueta = tk.Label(text="Email",bg='Slategray2')
  94. etiqueta.place(x=20, y=70)
  95. caja_email = tk.Entry(justify="center")
  96. caja_email.place(x=120, y=70, width=200, height=25)
  97.  
  98.  
  99. # campo edad
  100. etiqueta = tk.Label(text="Edad",bg='Slategray2')
  101. etiqueta.place(x=20, y=120)
  102. caja_edad = tk.Entry(
  103.             justify="center",
  104.             font = ("arial",16,'italic'),
  105.             fg = "red",
  106.             bd = 4
  107.             )
  108. caja_edad.place(x=120, y=120, width=200, height=25)
  109.  
  110.  
  111. # campo curso (se implementa con una lista desplegable)
  112. # el widget es Combobox
  113. etiqueta = tk.Label(text="Curso",bg='Slategray2')
  114. etiqueta.place(x=20, y=170)
  115.  
  116. caja_curso = ttk.Combobox(
  117.                     state = 'readonly',
  118.                     values = ['Visual Basic', 'Python', 'HTML', 'JavaScript']
  119.             )
  120.  
  121. caja_curso.place(x=120,y=170, width=200, height=25)
  122.  
  123. # boton para guardar el formulario
  124. boton = tk.Button(text="Guardar", command=guardar)
  125. boton.place(x=170, y=220, width=100, height=45)
  126.  
  127.  
  128. ventana.mainloop()
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement