Advertisement
Najeebsk

TEMPERATURE-CONVERTER.py

Jan 11th, 2022
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. import tkinter as tk
  2.  
  3. def fahrenheit_to_celsius():
  4.     """Convert the value for Fahrenheit to Celsius and insert the
  5.    result into lbl_result.
  6.    """
  7.     fahrenheit = ent_temperature.get()
  8.     celsius = (5/9) * (float(fahrenheit) - 32)
  9.     lbl_result["text"] = f"{round(celsius, 2)} \N{DEGREE CELSIUS}"
  10.  
  11. # Set-up the window
  12. window = tk.Tk()
  13. window.title("Temperature Converter")
  14. window.resizable(width=False, height=False)
  15.  
  16. # Create the Fahrenheit entry frame with an Entry
  17. # widget and label in it
  18. frm_entry = tk.Frame(master=window)
  19. ent_temperature = tk.Entry(master=frm_entry, width=10)
  20. lbl_temp = tk.Label(master=frm_entry, text="\N{DEGREE FAHRENHEIT}")
  21.  
  22. # Layout the temperature Entry and Label in frm_entry
  23. # using the .grid() geometry manager
  24. ent_temperature.grid(row=0, column=0, sticky="e")
  25. lbl_temp.grid(row=0, column=1, sticky="w")
  26.  
  27. # Create the conversion Button and result display Label
  28. btn_convert = tk.Button(
  29.     master=window,
  30.     text="\N{RIGHTWARDS BLACK ARROW}",
  31.     command=fahrenheit_to_celsius
  32. )
  33. lbl_result = tk.Label(master=window, text="\N{DEGREE CELSIUS}")
  34.  
  35. # Set-up the layout using the .grid() geometry manager
  36. frm_entry.grid(row=0, column=0, padx=10)
  37. btn_convert.grid(row=0, column=1, pady=10)
  38. lbl_result.grid(row=0, column=2, padx=10)
  39.  
  40. # Run the application
  41. window.mainloop()
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement