Mundo em Python

Validar o NIF (usando Tkinter)

from tkinter import *

root = Tk()
root.geometry("400x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Validar o NIF") texto_title = Label(root, text="Validar o NIF", font=("Arial", "30", "bold"), bg="#103030", fg="white")
texto_title.place(relx=0.2, rely=0.05) texto_sub1 = Label(root, text="Número de NIF:", font=("Arial", "15", "bold"), bg="#103030", fg="white")
texto_sub1.place(relx=0.1, rely=0.3) Valida_NIF = StringVar()
Valida_NIF_entrada = Entry(root, textvariable=Valida_NIF, font=("Arial", "12", "bold"), bg="white", fg="blue",
justify='center')
Valida_NIF_entrada.place(relx=0.53, rely=0.3, relwidth=0.45)
def limpar():
Valida_NIF_entrada.delete(0, END)
resultado_texto.config(text="")
def app():
nif = str(Valida_NIF.get()).strip() if len(nif) != 9 or not nif.isdigit():
resultado_texto.config(text="NIF inválido: deve conter 9 dígitos.", fg="red")
return

if nif[0] not in '1235789':
resultado_texto.config(text="NIF inválido: dígito inicial inválido.", fg="red")
return

total = sum(int(nif[i]) * (9 - i) for i in range(8))
check_digit = 11 - (total % 11)
check_digit = 0 if check_digit >= 10 else check_digit if check_digit == int(nif[8]):
resultado_texto.config(text="NIF válido.", fg="green")
else:
resultado_texto.config(text="NIF inválido: dígito verificador incorreto.", fg="red")
but1 = Button(root, text="Verificar", bd=2, bg='#107db2', fg='white', font=('verdana', 12, 'bold'), command=app)
but1.place(relx=0.05, rely=0.5, relwidth=0.25, relheight=0.1) but_limpar = Button(root, text="Limpar", bd=2, bg='#107db2', fg='white', font=('verdana', 12, 'bold'), command=limpar)
but_limpar.place(relx=0.35, rely=0.5, relwidth=0.25, relheight=0.1) but_sair = Button(root, text="Sair", bd=2, bg='#107db2', fg='white', font=('verdana', 12, 'bold'), command=root.destroy)
but_sair.place(relx=0.65, rely=0.5, relwidth=0.25, relheight=0.1) resultado_texto = Label(root, text="", font=("Arial", 15, "bold"), bg="#cfe2f3")
resultado_texto.place(relx=0.05, rely=0.7, relwidth=0.9, relheight=0.2) root.mainloop()
0