Litro para Galão (usando tkinter)

import tkinter as tk
from functools import partial

# Variável global
tempVal = "Galão Americano"

def armazenar_temp(sel_temp):
global tempVal
tempVal = sel_temp

def chamar_converter(rlabel1, inputn):
tem = inputn.get()
try:
tem = float(tem)
if tempVal == 'Galão Americano':
gi1 = float(tem * 0.832674)
l1 = float(tem * 3.78541)
mensagem = f"Galão Imperial: {round(gi1, 2)}\nLitros: {round(l1, 2)}"
rlabel1.config(text=mensagem)

elif tempVal == 'Galão Imperial':
l2 = float(tem * 4.54609)
ga2 = float(tem / 0.832674)
mensagem = f"Galão Americano: {round(ga2, 2)}\nLitros: {round(l2, 2)}"
rlabel1.config(text=mensagem)

elif tempVal == 'Litro':
galao_americano = float(tem / 3.78541)
galao_imperial = float(tem / 4.54609)
mensagem = f"Galão Americano: {round(galao_americano, 2)}\nGalão Imperial: {round(galao_imperial, 2)}"
rlabel1.config(text=mensagem)
except ValueError:
rlabel1.config(text="Por favor, insira um valor numérico válido.")
return

root = tk.Tk()
root.geometry('300x300')
root.title('Converter Litros para Galões e vice-versa')
root.configure(background='#09A3BA')
root.resizable(width=False, height=False)
root.grid_columnconfigure(1, weight=1)
root.grid_rowconfigure(0, weight=1)

numberInput = tk.StringVar()
var = tk.StringVar()

input_label = tk.Label(root, text="Valor: ", background='#09A3BA', foreground="#FFFFFF", font=("Arial", "17", "bold"))
input_entry = tk.Entry(root, textvariable=numberInput)
input_label.place(relx=0.15, rely=0.12)
input_entry.place(relx=0.5, rely=0.14)

result_label1 = tk.Label(root, background='#09A3BA', foreground="#FFFFFF")
result_label1.grid(row=3, columnspan=4)

dropDownList = ["Galão Americano", "Galão Imperial", "Litro"]
dropdown = tk.OptionMenu(root, var, *dropDownList, command=armazenar_temp)
var.set(dropDownList[0])
dropdown.place(relx=0.02, rely=0.3, relwidth=0.9, relheight=0.15)
dropdown.config(background='#09A3BA', foreground="#FFFFFF", font=("Arial", "15", "bold"))
dropdown["menu"].config(background='#09A3BA', foreground="#FFFFFF", font=("Arial", "15", "bold"))

chamar_converter = partial(chamar_converter, result_label1, numberInput)
result_button = tk.Button(root, text="Converter", command=chamar_converter,
background='#09A3BA', foreground="#FFFFFF", font=("Arial", "15", "bold"))
result_button.place(relx=0.02, rely=0.54, relwidth=0.9, relheight=0.15)

root.mainloop()

Comentários

Mensagens populares deste blogue

Criar Cartões de Visita

12 signos egípcios

Calcular a percentagem de ocupação