Skip to content

Instantly share code, notes, and snippets.

@Advik-B
Created September 5, 2023 01:06
Show Gist options
  • Save Advik-B/82cc3518bb4d4fe42d297306e37c858b to your computer and use it in GitHub Desktop.
Save Advik-B/82cc3518bb4d4fe42d297306e37c858b to your computer and use it in GitHub Desktop.
Temperature Tool
from tkinter import Tk, StringVar, IntVar
from tkinter.font import Font
from tkinter.ttk import Entry, Label
class Temperature:
def __init__(self, temperature: float, unit: str ="C"):
self.celsius: float = 0
self.kelvin: float = 0
self.fahrenheit: float = 0
self.update(float(temperature), str(unit))
def update(self, temperature: float, unit: str ="C"):
unit = unit.casefold()
if unit == 'c':
self.celsius = temperature
self.kelvin = self.celsius + 273.15
self.fahrenheit = self.celsius * 1.8 + 32.0
elif unit == 'f':
self.fahrenheit = temperature
self.celsius = (self.fahrenheit - 32) / 1.8
self.kelvin = self.celsius + 273.15
elif unit == 'k':
self.kelvin = temperature
self.celsius = self.kelvin - 273.15
self.fahrenheit = self.celsius * 1.8 / 32.0
class App(Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title("Temperature Tool")
self.temperature_controller = Temperature(0)
self.big_font = Font(family="Helvetica", size=24)
self.small_font = Font(family="Consolas", size=16)
self.kelvin_lbl = Label(self, text="Kelvin", font=self.big_font)
self.celsius_lbl = Label(self, text="Celsius", font=self.big_font)
self.fahrenheit_lbl = Label(self, text="Fahrenheit", font=self.big_font)
self.kelvin_var = StringVar()
self.celsius_var = StringVar()
self.fahrenheit_var = StringVar()
self.kelvin_var.trace("w", lambda name, index,mode, var=self.kelvin_var: self.write_callback(var, 'k'))
self.celsius_var.trace("w", lambda name, index,mode, var=self.celsius_var: self.write_callback(var, 'c'))
self.fahrenheit_var.trace("w", lambda name, index,mode, var=self.fahrenheit_var: self.write_callback(var, 'f'))
self.kelvin_ent = Entry(self, font=self.small_font, textvariable=self.kelvin_var)
self.celsius_ent = Entry(self, font=self.small_font, textvariable=self.celsius_var)
self.fahrenheit_ent = Entry(self, font=self.small_font, textvariable=self.fahrenheit_var)
self.celsius_lbl.grid(row=0, column=0, padx=50, pady=10)
self.kelvin_lbl.grid(row=0, column=1, padx=50, pady=10)
self.fahrenheit_lbl.grid(row=0, column=2, padx=50, pady=10)
self.celsius_ent.grid(row=1, column=0, padx=50, pady=10)
self.kelvin_ent.grid(row=1, column=1, padx=50, pady=10)
self.fahrenheit_ent.grid(row=1, column=2, padx=50, pady=10)
self.wm_resizable(False, False)
def write_callback(self, temperature_var: StringVar, unit: str = 'C'):
content = temperature_var.get()
if self.is_float(content):
original_unit = unit # Store the original unit before setting Kelvin value
if unit == 'k':
# Set the value in the Kelvin input directly without any conversion
self.kelvin_var.set(content)
else:
self.temperature_controller.update(float(content), unit)
# Round the displayed temperatures to 3 decimal places
kelvin_display = round(self.temperature_controller.kelvin, 3)
celsius_display = round(self.temperature_controller.celsius, 3)
fahrenheit_display = round(self.temperature_controller.fahrenheit, 3)
# Update all three input fields with the rounded values
self.kelvin_var.set(str(kelvin_display))
self.celsius_var.set(str(celsius_display))
self.fahrenheit_var.set(str(fahrenheit_display))
# Update the unit after setting Kelvin value
unit = original_unit
# print(unit) # Print the unit after handling the Kelvin value
def is_float(self, string: str) -> bool:
try:
float(string)
return True
except ValueError:
return False
if __name__ == "__main__":
root = App()
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment