Converter Temperatura (usando tkinter)

from tkinter import *

root = Tk()
root.title("Converter Temperatura")
root.geometry("300x150")
root.resizable(False,False)

def fahrenheit_para_celsius():
fahrenheit = ent_temperaturaf.get()
celsius = (5/9) * (float(fahrenheit) - 32)
lbl_result["text"] = f"{round(celsius, 2)} \N{DEGREE CELSIUS}"
# Fahrenheit para CELSIUS
lab_temperaturaf = Label(root,text="Fahrenheit", font=("Arial","10","bold"))
lab_temperaturaf.place(relx=0.05,rely=0.05)
lab_temperaturaf = Label(root,text="Celsius", font=("Arial","10","bold"))
lab_temperaturaf.place(relx=0.45,rely=0.05)
ent_temperaturaf=DoubleVar()
ent_temperaturaf = Entry(textvariable=ent_temperaturaf)
ent_temperaturaf.place(relx=0.05,rely=0.2,relwidth=0.25)
btn_convert =Button(text="\N{RIGHTWARDS BLACK ARROW}",
command=fahrenheit_para_celsius)
btn_convert.place(relx=0.35,rely=0.18)
lbl_result = Label(text="\N{DEGREE CELSIUS}")
lbl_result.place(relx=0.45,rely=0.18)

def celsius_para_fahrenheit():
celsius = ent_temperaturac.get()
fahrenheit = (9*float(celsius)+32*5)/5
lbl_resultf["text"] = f"{round(fahrenheit, 2)} \N{DEGREE FAHRENHEIT}"
# CELSIUS para Fahrenheit
lab_temperaturac = Label(root,text="Celsius", font=("Arial","10","bold"))
lab_temperaturac.place(relx=0.05,rely=0.35)
ent_temperaturac=DoubleVar()
ent_temperaturac = Entry(textvariable=ent_temperaturac)
ent_temperaturac.place(relx=0.05,rely=0.55,relwidth=0.25)
btn_convertf =Button(text="\N{RIGHTWARDS BLACK ARROW}",
command= celsius_para_fahrenheit)
btn_convertf.place(relx=0.35,rely=0.55)
lbl_resultf = Label(text="\N{DEGREE FAHRENHEIT}")
lbl_resultf.place(relx=0.45,rely=0.55)
root.mainloop()

 


Ou esta versão:


from tkinter import *
from functools import partial

root = Tk()
root.title('Temperatura ')
root.geometry("400x250")
root.configure(background='#09A3BA')
root.resizable(False, False)
tempVal = "Celsius"
def store_temp(sel_temp):
global tempVal
tempVal = sel_temp
def converter(rlabel1, rlabe12, entrada_var):
tem = entrada_var.get()
if tempVal == 'Celsius':
f = float((float(tem) * 9 / 5) + 32)
k = float((float(tem) + 273.15))
rlabel1.config(text="%f Fahrenheit" %f)
rlabe12.config(text="%f Kelvin" %k)
if tempVal == 'Fahrenheit':
c = float((float(tem) - 32) * 5 / 9)
k = c + 273
rlabel1.config(text="%f Celsius" %c)
rlabe12.config(text="%f Kelvin" %k)
if tempVal == 'Kelvin':
c = float((float(tem) - 273.15))
f = float((float(tem) - 273.15) * 1.8000 + 32.00)
rlabel1.config(text="%f Celsius" %c)
rlabe12.config(text="%f Fahrenheit" %f)
return
# Introduzir temperatura
texto = Label(text="Digite a temperatura", background='#09A3BA',
foreground="#FFFFFF")
texto.place(relx=0.1,rely=0.1)
entrada_var = DoubleVar()
var = Entry(textvariable=entrada_var)
var.place(relx=0.4,rely=0.1)

# Mostrar resultados

result_label1 =Label(root, background='#09A3BA', foreground="#FFFFFF")
result_label1.place(relx=0.4, rely=0.6)
result_label2 =Label(root, background='#09A3BA', foreground="#FFFFFF")
result_label2.place(relx=0.4, rely=0.7)

# Lista
var = StringVar()
lista = ["Celsius", "Fahrenheit", "Kelvin"]
oplista = OptionMenu(root, var, *lista, command=store_temp)
var.set(lista[0])
oplista.place(relx=0.4, rely=0.3)
oplista.config(background='#09A3BA', foreground="#FFFFFF")
oplista["menu"].config(background='#09A3BA', foreground="#FFFFFF")

converter = partial(converter, result_label1, result_label2, entrada_var)
result_button = Button(root, text="Converter", command=converter,
background='#09A3BA', foreground="#FFFFFF")
result_button.place(relx=0.4, rely=0.5)
mainloop()

 


 

Comentários

Mensagens populares deste blogue

Criar Cartões de Visita

12 signos egípcios

Calcular a percentagem de ocupação