Welcome folks today in this post we will be building a realtime international currency converter
in tkinter using web scraping in python. All the full source code of the application is shown below.
Get Started
In order to get started you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
import requests import tkinter as tk from xml.etree import ElementTree as ET r = requests.get('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml', stream=True) tree = ET.parse(r.raw) rootXML = tree.getroot() namespaces = {'ex': 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref'} currencyList = [] for cube in rootXML.findall('.//ex:Cube[@currency]', namespaces=namespaces): currencyList.append(cube.attrib['currency']) print(cube.attrib['currency'], cube.attrib['rate']) class CurrencyConverter(tk.Tk): def __init__(self, parent): tk.Tk.__init__(self, parent) self.parent = parent self.geometry('550x300+800+400') self.resizable(width=False, height=False) self.initialize() def initialize(self): self.grid() # Title self.label = tk.Label(self, text="Currency Converter", fg='darkblue', font=("Helvetica", 16)) self.label.grid(row=0, padx=15, pady=15) # Amount self.label = tk.Label(self, text="Convert this amount") self.label.grid(row=2, column=0, pady=5, padx=18, sticky=tk.E) self.var = tk.DoubleVar() self.E1 = tk.Entry(self, bd=2, textvariable=self.var, justify=tk.RIGHT) self.E1.grid(row=3, column=0, pady=5, padx=5, sticky=tk.E, ipady=2) #OptionMenu self.label = tk.Label(self, text="From this currency") self.label.grid(row=2, column=1, pady=10, padx=15, sticky=tk.W) self.variable1 = tk.StringVar(self) self.variable1.set(currencyList[0]) self.E2 = tk.OptionMenu(self, self.variable1, *currencyList) self.E2.config(width=15) self.E2.grid(row=3, column=1, sticky=tk.E) #OptionMenu self.label = tk.Label(self, text="To this currency") self.label.grid(row=2, column=2, pady=10, padx=15, sticky=tk.W) self.variable2 = tk.StringVar(self) self.variable2.set(currencyList[0]) self.E3 = tk.OptionMenu(self, self.variable2, *currencyList) self.E3.config(width=15) self.E3.grid(row=3, column=2, padx=10, sticky=tk.E) #Result self.label = tk.Label(self, text="Result", font=("Helvetica", 18)) self.label.grid(row=4, column=0, pady=35, sticky=tk.E) self.variable3 = tk.DoubleVar() self.E4 = tk.Entry(self, bd=2, textvariable=self.variable3, justify=tk.RIGHT) self.E4.grid(row=4, column=1, ipady=3, padx=10, sticky=tk.E) #Buttons self.button = tk.Button(self, text='Convert', command=self.currencyCalculate, width=12, height=2) self.button.grid(row=5, column=1, padx=5, sticky=tk.E) self.button = tk.Button(self, text='Close', command=self.currencyCalculate, width=12, height=2) self.button.grid(row=5, column=2, sticky=tk.W) def currencyCalculate(self): user_input = self.var.get() option_menu_input1 = self.variable1.get() option_menu_input2 = self.variable2.get() for cube in rootXML.findall('.//ex:Cube[@currency]', namespaces=namespaces): if cube.attrib['currency'] == option_menu_input1: to_dollars = float(user_input) / float(cube.attrib['rate']) for cube in rootXML.findall('.//ex:Cube[@currency]', namespaces=namespaces): if cube.attrib['currency'] == option_menu_input2: result = to_dollars * float(cube.attrib['rate']) self.variable3.set(round(result, 2)) return result if __name__ == "__main__": app = CurrencyConverter(None) app.title('My Currency Converter') app.mainloop() |
Now if you execute the python script by typing the below command as shown below
python app.py