teslariu

integrador tkinter

May 11th, 2023
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Ejercicio integrador
  5.  
  6. # alumnos = {"Juan":5, "Ana":11, "Josefa":3}  alumnos["Juan"] --> da 5
  7.  
  8. import tkinter as tk
  9.  
  10.  
  11. ############### Consola
  12.  
  13. alumnos = {}
  14.  
  15. def borrar_pantalla():
  16.     import os
  17.     if os.name == "posix":
  18.         os.system("clear")
  19.     else:
  20.         os.system("cls")
  21.  
  22.  
  23.  
  24. def imprimir():
  25.     borrar_pantalla()
  26.     global alumnos
  27.     if alumnos:
  28.         print("Lista de alumnos:")
  29.         for k,v in alumnos.items():
  30.             print(f"{k} - {v} cursos")
  31.     else:
  32.         from tkinter import messagebox
  33.         messagebox.showinfo(title="ERROR", message="No hay alumnos")
  34.          
  35.    
  36. def agregar_alumno():
  37.     from tkinter import messagebox
  38.     global alumnos
  39.     borrar_pantalla()
  40.     nombre = caja_nombre.get()
  41.     if nombre == "":
  42.         messagebox.showinfo(title="ERROR", message="No ha ingresado un nombre")
  43.      
  44.     cursos = caja_cursos.get()
  45.     if cursos.isdecimal() and int(cursos):
  46.         alumnos[nombre] = cursos
  47.         messagebox.showinfo(title="INFO", message=f"Alumno añadido: {nombre} - {cursos} cursos")
  48.     else:
  49.         messagebox.showinfo(title="ERROR", message="No ha ingresado un número válido")
  50.                
  51.        
  52. def mostrar_cursos():
  53.     from tkinter import messagebox
  54.     global alumnos
  55.     borrar_pantalla()
  56.     nombre = caja_nombre.get()
  57.     if nombre in list(alumnos.keys()):
  58.         print(f"{nombre} - {alumnos[nombre]} cursos")
  59.     else:
  60.          messagebox.showinfo(title="ERROR", message="No existe el alumno")
  61.        
  62.    
  63.  
  64.  
  65. ##### Interfaz gráfica
  66. ventana = tk.Tk()
  67. ventana.config(width=400, height=300)
  68. ventana.title("Proyecto Integrador")
  69. ventana.resizable(0,0)  # evita redimensionar la ventana
  70.  
  71. boton = tk.Button(text="Ver lista de alumnos",command=imprimir)
  72. boton.place(x=10, y=10)
  73.  
  74. caja_nombre = tk.Entry()
  75. caja_nombre.place(x=120, y=60, width=200, height=25)
  76.  
  77. etiqueta = tk.Label(text="Nombre alumno")
  78. etiqueta.place(x=20, y=60)
  79.  
  80. caja_cursos = tk.Entry()
  81. caja_cursos.place(x=120, y=110, width=50, height=25)
  82.  
  83. etiqueta = tk.Label(text="Cursos")
  84. etiqueta.place(x=20, y=110)
  85.  
  86. boton = tk.Button(text="Agregar a la lista",command=agregar_alumno)
  87. boton.place(x=10, y=160)
  88.  
  89. boton = tk.Button(text="Ver cantidad de cursos",command=mostrar_cursos)
  90. boton.place(x=130, y=160)
  91.  
  92. ventana.mainloop()
  93.  
  94.  
Advertisement
Add Comment
Please, Sign In to add comment