Ive been trying to turn degree celsius into fahrenheit and kelvin, but Im new to Tkinter and havent been able to find someone to explain how it does mathematical processes to return the degrees in the new scale, also because It would be too nice to just ask this the code has some spanish in it, im just gonna leave it there, hope someone is able to understand it
import tkinter as tk
root = tk.Tk()
root.title('Conversor de grados')
root.geometry('400x500')
root.resizable(False,False)
name_label = tk.Label(
root,
text = 'Convertir a ºF y K',
font = ('Arial', 200 ),
bg = 'blue',
fg = '#FF0' )
number_label = tk.Label(
root,
text = '¿Que temperatura hace?'
)
number_input = tk.Entry(root)
#number_input = grados introducidos
value = number_input
submit_button1 = tk.Button (root, text = 'convertir a ºF')
output_line = tk.Label(root)
submit_button2 = tk.Button (root, text = 'convertir a K')
output_line = tk.Label(root)
number_label.pack()
number_input.pack()
submit_button1.pack()
submit_button2.pack()
output_line.pack()
def resultadof():
f = int((value*9/5)+32)
def on_submit1():
"""Cuado se pulse el botón"""
print "resultadof"
output_line.configure(text=message)
def on_submit2():
"""Cuado se pulse el botón"""
number = number_input.get()
resultadof=(number*9/5)+32
message = (
f'son {number_input} grados.\n'
)
output_line.configure(text=message)
if submit_button1.configure(command = on_submit1):
submit_button2.configure(command = on_submit2)
root.mainloop()
---
- Im trying to turn celsius into kelvin and fahrenheit through tk, i tried using buttons as it seamed the easiest, I wasnt able, the program should just return the degrees on the scale you choose
I have edited your code and used English.
import tkinter as tk
Firstly, I created 2 functions that calculate celsius to Fahrenheit and Celsius to kelvin. Both have parameters named Celsius.
def celsius_to_fahrenheit(celsius):
fahrenheit = (celsius * 9/5) + 32
return fahrenheit
def celsius_to_kelvin(celsius):
kelvin = celsius + 273.15
return kelvin
convert_temperature is a command function for convert_button
. It basically gets value from number_input
and checks selected_scale
.After that run the calculation functions that I mentioned above, depending on them. After calculation, it will update output_line
.
def convert_temperature():
try:
celsius = float(number_input.get())
if selected_scale.get() == "Fahrenheit":
result = celsius_to_fahrenheit(celsius)
unit = "°F"
elif selected_scale.get() == "Kelvin":
result = celsius_to_kelvin(celsius)
unit = "K"
else:
result = "Error: Select a scale"
unit = ""
output_line.config(text=f"{result:.2f} {unit}")
except ValueError:
output_line.config(text="Invalid input. Enter a number.")
root = tk.Tk()
root.title('Temperature Converter')
root.geometry('400x200')
name_label = tk.Label(
root,
text='Convert to °F and K',
font=('Arial', 16),
bg='blue',
fg='#FF0'
)
name_label.pack()
number_label = tk.Label(
root,
text='Enter temperature in Celsius:'
)
number_label.pack()
number_input = tk.Entry(root)
number_input.pack()
selected_scale = tk.StringVar()
selected_scale.set("Select scale")
scale_options = ["Fahrenheit", "Kelvin"]
scale_dropdown = tk.OptionMenu(root, selected_scale, *scale_options)
scale_dropdown.pack()
convert_button = tk.Button(root, text="Convert", command=convert_temperature)
convert_button.pack()
output_line = tk.Label(root)
output_line.pack()
root.mainloop()