Mundo em Python

Analisador de Tempo Online (%)

from tkinter import *

root = Tk()
root.geometry("700x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Analisador de Tempo Online (%)")
titulo = Label(
text="Analisador de Tempo Online (%)",
font=("Arial", 28, "bold"),
bg="#103030",
fg="#49e3e3"
)
titulo.place(relx=0.05, rely=0.05)
texto_sub1 = Label(
text="Tempo no telemóvel (em horas):",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub1.place(relx=0.05, rely=0.32) # Entrada
Tempo_telemóvel = StringVar()
Tempo_telemóvel_entrada = Entry(
textvariable=Tempo_telemóvel,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
Tempo_telemóvel_entrada.place(relx=0.59, rely=0.33, relwidth=0.35)
Tempo_telemóvel_entrada.focus() def limpar():
Tempo_telemóvel_entrada.delete(0, END)
resultado_texto.config(text="") def app():
try:
tempo_total = 24
tempo_telemovel = float(Tempo_telemóvel.get())
if tempo_telemovel < 0:
resultado_texto.config(text="O tempo não pode ser negativo!")
return
if tempo_telemovel > tempo_total:
resultado_texto.config(text="O tempo não pode exceder 24 horas!")
return

percentagem = (tempo_telemovel / tempo_total) * 100

# Classificação
if percentagem <= 25:
nivel = " Baixo"
elif percentagem <= 50:
nivel = "Razoável"
elif percentagem <= 75:
nivel = "Alto"
else:
nivel = "Muito alto"

mensagem = f"Usaste {percentagem:.2f}% do teu dia no telemóvel\nClassificação: {nivel}"
resultado_texto.config(text=mensagem)
except ValueError:
resultado_texto.config(text="Por favor, insira valores numéricos válidos")
but1 = Button(
text="Calcular",
bd=2,
bg='#107db2',
fg='white',
font=('verdana', 12, 'bold'),
command=app
)
but1.place(relx=0.1, rely=0.52, relwidth=0.25, relheight=0.1) but_limpar = Button(
text="Limpar",
bd=2,
bg='#107db2',
fg='white',
font=('verdana', 12, 'bold'),
command=limpar
)
but_limpar.place(relx=0.4, rely=0.52, relwidth=0.25, relheight=0.1) but_sair = Button(
text="Sair",
bd=2,
bg='#107db2',
fg='white',
font=('verdana', 12, 'bold'),
command=root.destroy
)
but_sair.place(relx=0.7, rely=0.52, relwidth=0.25, relheight=0.1) resultado_texto = Label(
text="",
font=("Arial", 12, "bold"),
bg="#cfe2f3"
)
resultado_texto.place(relx=0.05, rely=0.7, relwidth=0.9, relheight=0.25) root.mainloop()
0