from tkinter import *
import math
root = Tk()
root.geometry("700x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Calculadora de Arredondamento de Algarismos Significativos")
titulo = Label(
text="Calculadora de Arredondamento de Algarismos Significativos",
font=("Arial", "17", "bold"), bg="#103030", fg="#49e3e3"
)
titulo.place(relx=0.01, rely=0.05)
texto_sub1 = Label(
text="Número que deseja arredondar:",
font=("Arial", "15", "bold"), bg="#103030", fg="#49e3e3"
)
texto_sub1.place(relx=0.1, rely=0.25)
numero_var = StringVar()
numero_entrada = Entry(
textvariable=numero_var, font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center'
)
numero_entrada.place(relx=0.6, rely=0.26, relwidth=0.35)
texto_sub2 = Label(
text="Número de algarismos significativos desejado:",
font=("Arial", "15", "bold"), bg="#103030", fg="#49e3e3"
)
texto_sub2.place(relx=0.05, rely=0.45)
algarismos_var = StringVar()
algarismos_entrada = Entry(
textvariable=algarismos_var, font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center'
)
algarismos_entrada.place(relx=0.7, rely=0.45, relwidth=0.24)
def arredondar_algarismos_significativos(numero, algarismos):
if numero == 0:
return 0
ordem_magnitude = int(math.floor(math.log10(abs(numero))))
fator = 10 ** (ordem_magnitude - (algarismos - 1))
return round(numero / fator) * fator
def limpar():
algarismos_entrada.delete(0, END)
numero_entrada.delete(0, END)
resultado_texto.config(text="")
def app():
try:
numero = float(numero_var.get())
algarismos = int(algarismos_var.get())
if algarismos <= 0:
mensagem = "O número de algarismos significativos deve ser maior que zero."
else:
resultado = arredondar_algarismos_significativos(numero, algarismos)
mensagem = f"O número {numero} arredondado para" \
f" {algarismos} algarismos significativos é: {resultado}"
except ValueError:
mensagem = "Por favor, insira valores válidos."
resultado_texto.config(text=mensagem)
but_calcular = Button(
text="Calcular", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=app
)
but_calcular.place(relx=0.1, rely=0.65, 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.65, 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.65, relwidth=0.25, relheight=0.1)
resultado_texto = Label(
text="", font=("Arial", 12, "bold"), bg="#cfe2f3", anchor="w"
)
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15)
root.mainloop()