Guest User

Untitled

a guest
Nov 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. import Tkinter
  2.  
  3. # global variables
  4. temp_c = None
  5. temp_f = None
  6.  
  7. def convert():
  8.  
  9. global temp_c
  10. global temp_f
  11.  
  12. try:
  13. val = temp_c.get()
  14. temp_f.set((val * 9.0 / 5) + 32)
  15. except:
  16. pass
  17.  
  18. # main window
  19. window = Tkinter.Tk()
  20. window.title("Temperature Converter")
  21.  
  22. # main container
  23. frame = Tkinter.Frame(window)
  24.  
  25. # lay out the main container, specify that we want it to grow with window size
  26. frame.pack(fill=Tkinter.BOTH, expand=True)
  27.  
  28. # allow middle cell of grid to grow when window is resized
  29. frame.columnconfigure(1, weight=1)
  30. frame.rowconfigure(1, weight=1)
  31.  
  32. # variables for holding temperature data
  33. temp_c = Tkinter.DoubleVar()
  34. temp_f = Tkinter.DoubleVar()
  35.  
  36. # create widgets
  37. entry_celsius = Tkinter.Entry(frame, width=7, textvariable=temp_c)
  38. label_unitc = Tkinter.Label(frame, text="degrees C")
  39. label_equal = Tkinter.Label(frame, text="is equal to")
  40. label_fahrenheit = Tkinter.Label(frame, textvariable=temp_f)
  41. label_unitf = Tkinter.Label(frame, text="degrees F")
  42. button_convert = Tkinter.Button(frame, text="Convert", command=convert)
  43.  
  44. # lay out widgets
  45. entry_celsius.grid(row=0, column=1, padx=5, pady=5)
  46. label_unitc.grid(row=0, column=2, padx=5, pady=5, sticky=Tkinter.W)
  47. label_equal.grid(row=1, column=0, padx=5, pady=5, sticky=Tkinter.E)
  48. label_fahrenheit.grid(row=1, column=1, padx=5, pady=5)
  49. label_unitf.grid(row=1, column=2, padx=5, pady=5, sticky=Tkinter.W)
  50. button_convert.grid(row=2, column=1, columnspan=2, padx=5, pady=5, sticky=Tkinter.E)
  51.  
  52. # Place cursor in entry box by default
  53. entry_celsius.focus()
  54.  
  55. # Run forever!
  56. window.mainloop()
Add Comment
Please, Sign In to add comment