Advertisement
teslariu

formulario tkinter

Jul 18th, 2023
1,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # formulario
  5. # posicionamiento place
  6.  
  7. import tkinter as tk
  8. from tkinter import ttk
  9. from tkinter import messagebox
  10.  
  11. '''estructura de datos
  12. alumnos = [
  13.        {"nombre": "Juan", "email":"juan@gmail.com", "tel":"456454", "curso":"Python"},
  14.        {"nombre": "JuanJose", "email":"juaao@gmail.com", "tel":"45677454", "curso":"C"}
  15.    ]
  16. '''
  17. def leer_datos():
  18.     global alumnos
  19.     # verifico que acepte las condiciones
  20.     valor = estado_casilla.get()
  21.     if not valor:
  22.         messagebox.showinfo(title="Términos y condiciones", message="Debe aceptar")
  23.         return
  24.          
  25.    
  26.    
  27.     nombre = caja_nombre.get()
  28.     if nombre.isspace() or not nombre:
  29.         messagebox.showerror(title="Error en nombre", message="Nombre inválido")
  30.         return
  31.        
  32.     email = caja_email.get()
  33.     if "@" not in email:
  34.         messagebox.showerror(title="Error en email", message="Dirección inválida")
  35.         return
  36.        
  37.    
  38.     tel = caja_tel.get()
  39.     curso = lista_cursos.get()
  40.    
  41.     alumno = {"nombre": nombre, "email":email, "tel":tel, "curso":curso}
  42.     alumnos.append(alumno)
  43.    
  44. def borrar_formulario():
  45.     caja_nombre.delete(0, tk.END)
  46.     caja_tel.delete(0, tk.END)
  47.     caja_email.delete(0, tk.END)
  48.    
  49.  
  50.  
  51. def guardar_datos():
  52.     from pprint import pprint
  53.     global alumnos
  54.     leer_datos()
  55.     pprint(alumnos)
  56.     borrar_formulario()
  57.  
  58.  
  59. alumnos = []
  60.  
  61. ventana = tk.Tk()
  62. ventana.title("Formulario de inscripción")
  63. ventana.config(width=400, height=370)
  64. ventana.resizable(False,False)
  65.  
  66. # campo nombre
  67. etiqueta = ttk.Label(text="Nombre")
  68. etiqueta.place(x=10, y=10)
  69. caja_nombre = ttk.Entry()
  70. caja_nombre.place(x=130, y=10, width=180, height=25)
  71.  
  72. # campo email
  73. etiqueta = ttk.Label(text="Email")
  74. etiqueta.place(x=10, y=60)
  75. caja_email = ttk.Entry()
  76. caja_email.place(x=130, y=60, width=180, height=25)
  77.  
  78. # campo telefono
  79. etiqueta = ttk.Label(text="Teléfono")
  80. etiqueta.place(x=10, y=110)
  81. caja_tel = ttk.Entry()
  82. caja_tel.place(x=130, y=110, width=180, height=25)
  83.  
  84. # campo cursos
  85. etiqueta = ttk.Label(text="Cursos")
  86. etiqueta.place(x=10, y=160)
  87. lista_cursos = ttk.Combobox(
  88.             state='readonly',
  89.             values=['Python', 'JavaScript','Python & Django', 'C']
  90.         )
  91. lista_cursos.place(x=130, y=160, width=180, height=25)
  92.  
  93.  
  94. # Casilla de verificación para aceptar términos y condiciones
  95. estado_casilla = tk.BooleanVar()
  96. estado_casilla.set("False")
  97. casilla = ttk.Checkbutton(text="Acepto términos y condiciones", variable=estado_casilla)
  98. casilla.place(x=100, y=230)
  99.  
  100. boton = ttk.Button(text="Enviar datos", command=guardar_datos)
  101. boton.place(x=130, y=290, width=120, height=35)
  102.  
  103. ventana.mainloop()
  104.  
  105.  
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement