Advertisement
teslariu

form con tkinter

Aug 31st, 2023
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. #  método place
  5. #
  6. # Script que implementa un formulario de inscripción a cursos
  7. #
  8.  
  9. import tkinter as tk
  10. from tkinter import ttk
  11. from tkinter import messagebox
  12. from pprint import pprint
  13.  
  14.  
  15. def guardar():
  16.     global alumnos
  17.    
  18.     # leo los datos
  19.    
  20.     nombre = caja_nombre.get()
  21.     if nombre.isspace() or not nombre:
  22.         messagebox.showerror(title="ERROR", message="Nombre inválido")
  23.         return 1
  24.    
  25.    
  26.     edad = caja_edad.get()
  27.     if not edad.isdecimal() or int(edad) <= 0 or int(edad) > 100:
  28.         messagebox.showerror(title="ERROR", message="Edad incorrecta")
  29.         return 1
  30.    
  31.    
  32.     dni = caja_dni.get()
  33.     if not dni.isdecimal() or int(dni) <= 0 or int(dni) > 100_000_000:
  34.         messagebox.showerror(title="ERROR", message="DNI inválido")
  35.         return 1
  36.    
  37.    
  38.    
  39.     email = caja_email.get()
  40.     if not "@" in email:
  41.         messagebox.showerror(title="ERROR", message="Email inválido")
  42.         return 1
  43.    
  44.     curso = lista_cursos.get()
  45.     if not curso:
  46.         messagebox.showerror(title="ERROR", message="Seleccione un curso")
  47.         return 1
  48.    
  49.     aceptacion = estado.get()
  50.     if not aceptacion:
  51.         messagebox.showwarning(title="Advertencia", message="Debe aceptar los términos y condiciones")
  52.         return 1
  53.  
  54.    
  55.    
  56.     # guardo los datos
  57.     alumno = {
  58.         "nombre": nombre,
  59.         "edad": edad,
  60.         "dni": dni,
  61.         "email": email,
  62.         "curso": curso
  63.     }
  64.     alumnos.append(alumno)
  65.    
  66.     # muestro los datos
  67.     print()
  68.     pprint(alumnos)
  69.    
  70.     # borro el formulario
  71.     caja_nombre.delete(0,tk.END)
  72.     caja_email.delete(0,tk.END)
  73.     caja_dni.delete(0,tk.END)
  74.     caja_edad.delete(0,tk.END)
  75.     estado.set("False")
  76.  
  77.  
  78.  
  79. # Datos: una lista de diccionarios (cada dicc es un alumno)
  80. alumnos = []
  81.  
  82.  
  83. ############# Parte gráfica ####################
  84.  
  85. # creo la ventana de mi formulario
  86. form = tk.Tk()
  87. form.title("Formulario de inscripción")
  88. form.config(width=400, height=400)
  89.  
  90. # evito que mi formulario sea redimensionable
  91. form.resizable(False,False)
  92.  
  93.  
  94. # campo nombre
  95. label = ttk.Label(text="Nombre")
  96. label.place(x=20, y=20)
  97. caja_nombre = ttk.Entry()
  98. caja_nombre.place(x=120, y=20, width=250, height=25)
  99.  
  100.  
  101. # campo edad
  102. label = ttk.Label(text="Edad")
  103. label.place(x=20, y=70)
  104. caja_edad = ttk.Entry()
  105. caja_edad.place(x=120, y=70, width=250, height=25)
  106.  
  107.  
  108. # campo dni
  109. label = ttk.Label(text="Dni")
  110. label.place(x=20, y=120)
  111. caja_dni = ttk.Entry()
  112. caja_dni.place(x=120, y=120, width=250, height=25)
  113.  
  114.  
  115. # campo email
  116. label = ttk.Label(text="Email")
  117. label.place(x=20, y=170)
  118. caja_email = ttk.Entry()
  119. caja_email.place(x=120, y=170, width=250, height=25)
  120.  
  121.  
  122. # campo cursos
  123. label = ttk.Label(text="Cursos")
  124. label.place(x=20, y=220)
  125. lista_cursos = ttk.Combobox(
  126.                     state = 'readonly',
  127.                     values = ["C++", "Java", "Python", "JavaScript", "HTML"]
  128.             )
  129. lista_cursos.place(x=120, y=220, width=250, height=25)
  130.  
  131.  
  132. # casilla de verificación
  133. estado = tk.BooleanVar()
  134. estado.set("False")
  135. casilla = ttk.Checkbutton(text="Acepto términos y condiciones",variable=estado)
  136. casilla.place(x=80, y=270)
  137.  
  138. # boton
  139. boton = ttk.Button(text="Guardar", command=guardar)
  140. boton.place(x=140, y=320, width=120, height=40)
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. form.mainloop()
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement