Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Ejercicio integrador
- # alumnos = {"Juan":5, "Ana":11, "Josefa":3} alumnos["Juan"] --> da 5
- import tkinter as tk
- ############### Consola
- alumnos = {}
- def borrar_pantalla():
- import os
- if os.name == "posix":
- os.system("clear")
- else:
- os.system("cls")
- def imprimir():
- borrar_pantalla()
- global alumnos
- if alumnos:
- print("Lista de alumnos:")
- for k,v in alumnos.items():
- print(f"{k} - {v} cursos")
- else:
- from tkinter import messagebox
- messagebox.showinfo(title="ERROR", message="No hay alumnos")
- def agregar_alumno():
- from tkinter import messagebox
- global alumnos
- borrar_pantalla()
- nombre = caja_nombre.get()
- if nombre == "":
- messagebox.showinfo(title="ERROR", message="No ha ingresado un nombre")
- cursos = caja_cursos.get()
- if cursos.isdecimal() and int(cursos):
- alumnos[nombre] = cursos
- messagebox.showinfo(title="INFO", message=f"Alumno añadido: {nombre} - {cursos} cursos")
- else:
- messagebox.showinfo(title="ERROR", message="No ha ingresado un número válido")
- def mostrar_cursos():
- from tkinter import messagebox
- global alumnos
- borrar_pantalla()
- nombre = caja_nombre.get()
- if nombre in list(alumnos.keys()):
- print(f"{nombre} - {alumnos[nombre]} cursos")
- else:
- messagebox.showinfo(title="ERROR", message="No existe el alumno")
- ##### Interfaz gráfica
- ventana = tk.Tk()
- ventana.config(width=400, height=300)
- ventana.title("Proyecto Integrador")
- ventana.resizable(0,0) # evita redimensionar la ventana
- boton = tk.Button(text="Ver lista de alumnos",command=imprimir)
- boton.place(x=10, y=10)
- caja_nombre = tk.Entry()
- caja_nombre.place(x=120, y=60, width=200, height=25)
- etiqueta = tk.Label(text="Nombre alumno")
- etiqueta.place(x=20, y=60)
- caja_cursos = tk.Entry()
- caja_cursos.place(x=120, y=110, width=50, height=25)
- etiqueta = tk.Label(text="Cursos")
- etiqueta.place(x=20, y=110)
- boton = tk.Button(text="Agregar a la lista",command=agregar_alumno)
- boton.place(x=10, y=160)
- boton = tk.Button(text="Ver cantidad de cursos",command=mostrar_cursos)
- boton.place(x=130, y=160)
- ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment