I am currently having problems with my currency converter program in my python class. I am trying to convert an amount, the entry widget, from the starting currency, the first option menu, to the desired currency, the second option menu. I have spent hours reading the documentation, so I hope you guys can give me some input! Currently I only have two currencies in my list too so it will be easier to test. The desired outcome would be a dynamic program that will be able to read the amount, currency 1, and currency 2 once the convert button is clicked. Here is my code:
# Module imports
import Tkinter as tk
import tkMessageBox
# Function Definitions
class Application(tk.Frame):
def __init__(self, parent = None):
tk.Frame.__init__(self, parent)
self.parent = parent
self.setupUI()
self.createWidgets()
def setupUI(self):
self.parent.title("User Input")
self.grid()
self.centerWindow()
def centerWindow(self):
app_width = 307
app_height = 350
sw = self.parent.winfo_screenwidth()
sh = self.parent.winfo_screenheight()
x = (sw - app_width)/2
y = (sh - app_height)/2
self.parent.geometry('%dx%d+%d+%d' % (app_width, app_height, x, y))
def createWidgets(self):
self.emptyFrame = tk.Frame(self.parent,bg="white")
self.emptyFrame.grid(row=0,column=0,sticky="news")
self.parent.grid_columnconfigure(0,weight=1)
self.parent.grid_rowconfigure(0,weight=1)
self.label1 = tk.Label(self.emptyFrame,text="Currency Converter",bg="white",font="Arial 26")
self.label1.grid(row=0,column=0, sticky="news")
self.label2 = tk.Label(self.emptyFrame,text="Visit http://www.xe.com/iso4217.php\nFor Currency Code Information",bg="white",font="Arial 8")
self.label2.grid(row=0,column=0, sticky="news")
self.label2.place(x = 55, y = 40)
currAmount1 = tk.StringVar() # Adding user imput for what value they want to convert
self.currEntry1 = tk.Entry(self.parent, textvariable=currAmount1,bd=2,width=28,background="white")
self.currEntry1.grid()
self.currEntry1.place(x = 27, y = 125)
currAmount2 = tk.StringVar()
self.currEntry2 = tk.Entry(self.parent,textvariable=currAmount2,bd=2,width=28,background="white")
self.currEntry2.grid()
self.currEntry2.place(x = 27, y = 195)
self.currency_list1 = ['USD','GBP'] #Creating the Currency Options menu and adding a list for all the currencies
self.curr_start1 = tk.StringVar()
self.curr_start1.set(self.currency_list1[0])
self.currencyMenu1 = tk.OptionMenu(self.parent, self.curr_start1, *self.currency_list1)
self.currencyMenu1.grid(row=0,column=0)
self.currencyMenu1.place(x = 230, y = 120)
self.currency_list2 = ['USD','GBP'] #Creating the Currency Options menu and adding a list for all the currencies
self.curr_start2 = tk.StringVar()
self.curr_start2.set(self.currency_list2[0])
self.currencyMenu2 = tk.OptionMenu(self.parent, self.curr_start2, *self.currency_list2)
self.currencyMenu2.grid(row=0,column=0)
self.currencyMenu2.place(x = 230, y = 190)
self.convertBtn = tk.Button(self.parent, text="Convert", font="Times 12", command=self.curr_search)
self.convertBtn.grid()
self.convertBtn.place(x = 120,y = 225)
def curr_search(self):
cur1 = self.curr_start1
cur2 = self.curr_start2
amount = currAmount1
url = 'https://www.google.com/finance/converter?'
final_url = url + 'a=' + str(amount) + '&from=' + str(cur1) + '&to=' + str(cur2) # Adds the paramters the user inputs to the general url for converting to currencies
web_obj = urllib2.urlopen(final_url)
results_str = str(web_obj.read())
final_str = re.search('class=bld>(.+?)</span>',results_str).group(1) # Extracts the string between the class and </span> which is the final conversion
final_num = map(int, re.findall(r'\d+', final_str)) # Extracts the integers from the final_str so that it wouldn't have the currency code in the final. Puts in a list
final = str(final_num[0]) + '.' + str(final_num[1]) # Adds the decimal back from
web_obj.close()
return final
# Main body
root = tk.Tk()
root.resizable(width=False, height=False)
app = Application(root)
root.mainloop()