Python simple currency converter


SUBMITTED BY: Guest

DATE: Dec. 1, 2013, 4:18 p.m.

FORMAT: Python

SIZE: 1.9 kB

HITS: 793

  1. #import the right GUI libraries
  2. from tkinter import *
  3. from tkinter import ttk
  4. #Function to convert pounds to euros
  5. def convert():
  6. try:
  7. #get what the user typed in
  8. value = float(pounds.get())
  9. euros.set(value * 1.18)
  10. #skip if they input something invalid
  11. #Error cathching
  12. except ValueError:
  13. pass
  14. #Tell them that something is wrong
  15. print("Please provide a valid number. A letter or symbol is NOT a number.")
  16. #main window
  17. root = Tk()
  18. #title of windows
  19. root.title("Pounds to Euros")
  20. #frame to make the window look better
  21. frame = ttk.Frame(root, padding="3 3 12 12")
  22. frame.grid(column=0, row=0, sticky=(N, W, E, S))
  23. frame.columnconfigure(0, weight=1)
  24. frame.rowconfigure(0, weight=1)
  25. #variables for the input and output
  26. pounds = StringVar()
  27. euros = StringVar()
  28. #input for how many pounds they want to convert
  29. pounds_entry = ttk.Entry(frame, width=7, textvariable=pounds)
  30. #make it all neat
  31. pounds_entry.grid(column=2, row=1, sticky=(W, E))
  32. #other widgets like the text you see and the convert button
  33. ttk.Label(frame, textvariable=euros).grid(column=2, row=2, sticky=(W, E))
  34. ttk.Button(frame, text="Convert", command=convert).grid(column=3, row=3, sticky=W)
  35. ttk.Label(frame, text="pounds").grid(column=3, row=1, sticky=W)
  36. ttk.Label(frame, text="is equivalent to").grid(column=1, row=2, sticky=E)
  37. ttk.Label(frame, text="euros").grid(column=3, row=2, sticky=W)
  38. #padding it all equally
  39. for child in frame.winfo_children():
  40. child.grid_configure(padx=5, pady=5)
  41. #focuses the window automatically so we dont have to press on it
  42. pounds_entry.focus()
  43. #binds the enter key so you can press it to convert
  44. root.bind('<Return>', convert)
  45. #Continuously runs until you press the big red X
  46. root.mainloop()

comments powered by Disqus