good morning.
I am developing a program in Python that I enter the number of students according to their performance in the notes and generate a statistical graph, and would like to know how to put restrictions with graphical interfaces that by typing a decimal number, a variable string and leave blank spaces, show me warning messages saying that this data is not valid.
I could not, only with negative numbers. This would be very helpful, you would be grateful ...
SO I TAKE MY PROGRAM
import tkinter as tk
import sys
from tkinter import font
from tkinter import ttk
from tkinter import messagebox
import math
class Aplicacion:
def __init__(self):
# opciones de ventana
self.ventana1 = tk.Tk()
self.ventana1.title("Academic statistics")
self.ventana1.config(bg="gray")
self.ventana1.geometry("400x330")
fuente = font.Font(family="roboto", size=10, weight="bold")
fuentetitulo = font.Font(family="roboto", size=14, weight="bold")
self.labeltitle = tk.Label(self.ventana1, text="Academic statistics", font=fuentetitulo, bg="#808080",
fg="#FFFFFF")
self.labeltitle.place(x=110, y=10)
self.label1 = tk.Label(self.ventana1, text="Students with low notes", font=fuente, bg="#808080")
self.label1.place(x=20, y=80)
self.dato1 = tk.StringVar()
self.entry1 = tk.Entry(self.ventana1, width=20, textvariable=self.dato1)
self.entry1.place(x=220, y=80)
self.label2 = tk.Label(self.ventana1, text="Students with middle notes", font=fuente, bg="#808080")
self.label2.place(x=20, y=120)
self.dato2 = tk.StringVar()
self.entry2 = tk.Entry(self.ventana1, width=20, textvariable=self.dato2)
self.entry2.place(x=220, y=120)
self.label3 = tk.Label(self.ventana1, text="Students with high notes", font=fuente, bg="#808080")
self.label3.place(x=20, y=160)
self.dato3 = tk.StringVar()
self.entry3 = tk.Entry(self.ventana1, width=20, textvariable=self.dato3)
self.entry3.place(x=220, y=160)
self.label4 = tk.Label(self.ventana1, text="Students with top notes", font=fuente, bg="#808080")
self.label4.place(x=20, y=200)
self.dato4 = tk.StringVar()
self.entry4 = tk.Entry(self.ventana1, width=20, textvariable=self.dato4)
self.entry4.place(x=220, y=200)
# botones
self.boton1 = tk.Button(self.ventana1, text="Exit", command=self.finalizar, font=fuente, bg="#DF013A",
fg="#FFFFFF")
self.boton1.place(x=100, y=270)
self.boton2 = tk.Button(self.ventana1, text="Generar ", font=fuente, bg="#04B431", fg="#FFFFFF",
command=self.barras)
self.boton2.place(x=200, y=270)
# para hacer visible la ventana
self.ventana1.resizable(False, False)
self.ventana1.mainloop()
# comandos para los botones
def finalizar(self):
sys.exit()
def barras(self):
valor1 = int(self.dato1.get())
valor2 = int(self.dato2.get())
valor3 = int(self.dato3.get())
valor4 = int(self.dato4.get())
tot = (valor1 + valor2 + valor3 + valor4)
if valor1 < 0 or valor2 < 0 or valor3 < 0 or valor4 < 0:
messagebox.showwarning("Dato Inválido", "Números negativos, por favor ingrese números enteros positivos")
print("Números negativos, por favor ingrese números enteros positivos")
else:
fuente = font.Font(family="roboto", size=10, weight="bold")
self.ventana2 = tk.Tk()
self.canvas1 = tk.Canvas(self.ventana2, width=800, height=500, background="gray")
self.canvas1.grid(column=0, row=0)
self.canvas1.delete(tk.ALL)
# if valor1>0 and valor2>0 and valor3>0 and valor4>0 :
largo1 = (valor1 * 100) / tot
largo2 = (valor2 * 100) / tot
largo3 = (valor3 * 100) / tot
largo4 = (valor4 * 100) / tot
porc1 = math.floor(largo1)
porc2 = math.floor(largo2)
porc3 = math.floor(largo3)
porc4 = math.floor(largo4)
self.canvas1.create_rectangle(10, 10, 10 + largo1, 90, fill="orange")
self.canvas1.create_rectangle(10, 120, 10 + largo2, 200, fill="purple")
self.canvas1.create_rectangle(10, 230, 10 + largo3, 310, fill="green")
self.canvas1.create_rectangle(10, 340, 10 + largo4, 420, fill="yellow")
self.canvas1.create_text(largo1 + 70, 50, text="Middle Notes", fill="white", font=fuente)
self.canvas1.create_text(largo1 + 300, 50, text=porc1, fill="white", font=fuente)
self.canvas1.create_text(largo1 + 320, 50, text="%", fill="white", font=fuente)
self.canvas1.create_text(largo2 + 70, 160, text="Middle Notes", fill="white", font=fuente)
self.canvas1.create_text(largo2 + 300, 160, text=porc2, fill="white", font=fuente)
self.canvas1.create_text(largo2 + 320, 160, text="%", fill="white", font=fuente)
self.canvas1.create_text(largo3 + 70, 270, text="Hight Notes", fill="white", font=fuente)
self.canvas1.create_text(largo3 + 300, 270, text=porc3, fill="white", font=fuente)
self.canvas1.create_text(largo3 + 320, 270, text="%", fill="white", font=fuente)
self.canvas1.create_text(largo4 + 70, 380, text="Top Notes", fill="white", font=fuente)
self.canvas1.create_text(largo4 + 300, 380, text=porc4, fill="white", font=fuente)
self.canvas1.create_text(largo4 + 320, 380, text="%", fill="white", font=fuente)
self.ventana2.mainloop()
# bloque principal
aplicacion1 = Aplicacion()