teslariu

form con tkinter

Jul 11th, 2023
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4.  
  5. import tkinter as tk
  6. import pprint
  7.  
  8. # defino mi estructura de datos:
  9. # lista = [
  10. #       {"nombre": "Ana"},
  11. #       {"nac": "Argentina"},
  12. #       {"tel": "11-2525-2257"}
  13. # ]
  14. #
  15.  
  16. def guardar_datos():
  17.     global lista
  18.    
  19.     # leo los datos del formulario
  20.     nombre = caja_nombre.get()
  21.     nac = caja_nac.get()
  22.     tel = caja_tel.get()
  23.    
  24.     # creo el diccionario con los datos leídos
  25.     persona = {"nombre":nombre, "nac":nac, "tel":tel}
  26.    
  27.     # agrego el diccionario de datos a la lista
  28.     lista.append(persona)
  29.     pprint.pprint(lista)
  30.    
  31.     # borro el formulario
  32.     caja_nac.delete(0, tk.END)
  33.     caja_nombre.delete(0, tk.END)
  34.     caja_tel.delete(0, tk.END)
  35.    
  36.    
  37.    
  38.  
  39. lista = []
  40.  
  41.  
  42. ventana = tk.Tk()
  43. ventana.config(width=300, height=280)
  44. ventana.title("Formulario")
  45. ventana.resizable(False,False) # evita redimensionar la ventana
  46.  
  47. # campo nombre
  48. etiqueta = tk.Label(text="Nombre")
  49. etiqueta.place(x=10, y=10)
  50. caja_nombre = tk.Entry()
  51. caja_nombre.place(x=100, y=10, width=150, height=24)
  52.  
  53.  
  54. # campo nacionalidad
  55. etiqueta = tk.Label(text="Nacionalidad")
  56. etiqueta.place(x=10, y=70)
  57. caja_nac = tk.Entry()
  58. caja_nac.place(x=100, y=70, width=150, height=24)
  59.  
  60.  
  61. # campo telefono
  62. etiqueta = tk.Label(text="Teléfono")
  63. etiqueta.place(x=10, y=130)
  64. caja_tel = tk.Entry()
  65. caja_tel.place(x=100, y=130, width=150, height=24)
  66.  
  67.  
  68. # boton
  69. boton = tk.Button(text="Guardar", command=guardar_datos)
  70. boton.place(x=120, y=200, width=80, height=35)
  71.  
  72.  
  73. ventana.mainloop()
  74.  
Advertisement
Add Comment
Please, Sign In to add comment