Python Forum
Currency converter - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Currency converter (/thread-27627.html)



Currency converter - Scott - Jun-14-2020

Hi everyone,

I found a exchange rate calculator online built in tkinter and am trying to update it to reference my dataframe exchange rate variables. Does anyone know how I can do that?

#Building a currency converter
#list of currencies

"""
AUDUSD = 0.8371
CADUSD = 0.8711
USDCNY = 6.1715
EURUSD = 1.2315
GBPUSD = 1.5683
NZDUSD = 0.7750
USDJPY = 119.95
EURCZK = 27.6028
EURDKK = 7.4405
EURNOK = 8.6651
"""

#load currencies into a dataframe for input statement to lookup
import pandas as pd

data = {'AUD': [1, 0.8711/0.8371, (1/6.1715)/0.8371, (1.2315/27.6028)/0.8371, (1.2315/7.4405)/0.8371, 1.2315/0.8371, 1.5683/0.8371, (1/119.95)/0.8371, (1.2315/8.6651)/0.8371, 0.7750/0.8371, 1/0.8371],
        'CAD': [0.8371/0.8711, 1, (1/6.1715)/0.8711, (1.2315/27.6028)/0.8711, (1.2315/7.4405)/0.8711, 1.2315/0.8711, 1.5683/0.8711, (1/119.95)/0.8711, (1.2315/8.6651)/0.8711, 0.7750/0.8711, 1/0.8711],
        'CNY': [0.8371/(1/6.1715), 0.8711/(1/6.1715), 1, (1.2315/27.6028)/(1/6.1715), (1.2315/7.4405)/(1/6.1715), 1.2315/(1/6.1715), 1.5683/(1/6.1715), (1/119.95)/(1/6.1715), (1.2315/8.6651)/(1/6.1715), 0.7750/(1/6.1715), 1/(1/6.1715)],
        'CZK': [0.8371/(1.2315/27.6028), 0.8711/(1.2315/27.6028), (1/6.1715)/(1.2315/27.6028), 1, (1.2315/7.4405)/(1.2315/27.6028), (1/1.2315)/27.6028, 1.5683/(1.2315/27.6028), (1/119.95)/(1.2315/27.6028), (1.2315/8.6651)/(1.2315/27.6028), 0.7750/(1.2315/27.6028), 1/(1.2315/27.6028)],
        'DKK': [0.8371/(1.2315/7.4405), 0.8711/(1.2315/7.4405), (1/6.1715)/(1.2315/7.4405), (1.2315/27.6028)/(1.2315/7.4405), 1, 1/(1.2315/7.4405), 1.5683/(1.2315/7.4405), (1/119.95)/(1.2315/7.4405), (1.2315/8.6651)/(1.2315/7.4405), 0.7750/(1.2315/7.4405), 1/(1.2315/7.4405)],
        'EUR': [0.8371/1.2315, 0.8711/1.2315,  (1/6.1715)/1.2315, 1/27.6028, 1/7.4405, 1, 1.5683/1.2315, (1/119.95)/1.2315, 8.6651, 0.7750/1.2315, 1/1.2315],
        'GBP': [0.8371/1.5683, 0.8711/1.5683,  (1/6.1715)/1.5683, (1.2315/27.6028)/1.5683, (1.2315/7.4405)/1.5683, 1.2315/1.5683, 1, (1/119.95)/1.5683, (1.2315/8.6651)/1.5683, 0.7750/1.5683, 1/1.5683],
        'JPY': [0.8371/(1/119.95), 0.8711/(1/119.95), (1/6.1715)/(1/119.95), (1.2315/27.6028)/(1/119.95), (1.2315/7.4405)/(1/119.95), 1.2315/(1/119.95), 1.5683/(1/119.95), 1, (1.2315/8.6651)/(1/119.95), 0.7750/(1/119.95), 119.95],
        'NOK': [0.8371/(1.2315/8.6651), 0.8711/(1.2315/8.6651), (1/6.1715)/(1.2315/8.6651), (1.2315/27.6028)/(1.2315/8.6651), (1.2315/7.4405)/(1.2315/8.6651), 1.2315/(1.2315/8.6651), 1.5683/(1.2315/8.6651), (1/119.95)/(1.2315/8.6651), 1,  0.7750/(1.2315/8.6651), 1/(1.2315/8.6651)],
        'NZD': [0.8371/0.7750, 0.8711/0.7750, (1/6.1715)/0.7750, (1.2315/27.6028)/0.7750,(1.2315/7.4405)/0.7750,1.2315/0.7750,1.5683/0.7750,(1/119.95)/0.7750,(1.2315/8.6651)/0.7750,1, 1/0.7750],
        'USD': [0.8371, 0.8711, (1/6.1715), 1.2315/27.6028, 1.2315/7.4405, 1.2315, 1.5683, 1/119.95, 1.2315/8.6651, 0.7750, 1]
       }

df = pd.DataFrame(data,
                 index=['AUD','CAD','CNY','CZK','DKK','EUR','GBP','JPY','NOK','NZD','USD'])

df = df.round(decimals=4)
df
from tkinter import *
import requests, json

# Create a GUI window 
root = Tk()

#create and initialise variables
variable1 = StringVar(root)
variable2 = StringVar(root)

variable1.set("currency")
variable2.set("currency")

#Function to preform currency conversion
def CurrencyConversion():
    
    #Currency code
    from_currency = variable1.get()
    to_currency = variable2.get()
    
    #store dataframe rates
    rates = df
    
    # main_rates variable store complete url 
    main_rates = rates + "&from_currency =" + from_currency + "&to_currency =" + to_currency
        
    # get method of requests module  
    # return response object  
    req_ob = requests.get(main_rates) 
  
    # json method return json format 
    # data into python dictionary data type. 
      
    # result contains list of nested dictionaries 
    result = req_ob.json() 
    
    Exchange_Rate = float(result["Exchange rate"]
                                     ['5. Exchange'])
    
    # get method of Entry widget 
    # returns current text as a 
    # string from text entry box. 
    amount = float(Amount1_field.get()) 
  
    # calculation for the conversion 
    new_amount = round(amount * Exchange_Rate, 2) 
  
    # insert method inserting the  
    # value in the text entry box.  
    Amount2_field.insert(0, str(new_amount)) 
    
    # function for clearing entry field
def clear_all():
    Amount1_field.delete(0, END)
    Amount2_field.delete(0, END)

#setup interface for calculator
if __name__ == "__main__" :

    # Set the background colour of GUI window  
    root.configure(background = 'light green')  

    # Set the configuration of GUI window (WidthxHeight) 
    root.geometry("400x175")  

    # Create Currency Convertor label  
    headlabel = Label(root, text = 'Currency Convertor',  
                      fg = 'black', bg = "red")  

    # Create a "Amount :" label  
    label1 = Label(root, text = "Amount :", 
                 fg = 'black', bg = 'dark green') 

    # Create a "From Currency :" label  
    label2 = Label(root, text = "From Currency",  
                   fg = 'black', bg = 'dark green')  

    # Create a "To Currency: " label  
    label3 = Label(root, text = "To Currency :",  
                   fg = 'black', bg = 'dark green') 

    # Create a "Converted Amount :" label  
    label4 = Label(root, text = "Converted Amount :",  
                   fg = 'black', bg = 'dark green') 

    # grid method is used for placing  
    # the widgets at respective positions  
    # in table like structure.
    headlabel.grid(row = 0, column = 1)
    label1.grid(row = 1, column = 0)  
    label2.grid(row = 2, column = 0) 
    label3.grid(row = 3, column = 0) 
    label4.grid(row = 5, column = 0) 

    # Create a text entry box  
    # for filling or typing the information. 
    Amount1_field = Entry(root)
    Amount2_field = Entry(root)

    # ipadx keyword argument set width of entry.
    Amount1_field.grid(row = 1, column = 1, ipadx ="25")
    Amount2_field.grid(row = 5, column = 1, ipadx ="25")

    # list of currency codes 
    CurrencyCode_list = ["AUD", "CAD", "CNY", "CZK", "DKK", "EUR", "GBP", "JPY", "NOK", "NZD", "USD"] #update

    # create a drop down menu using OptionMenu function 
    # which takes window name, variable and choices as 
    # an argument. use * before the name of the list, 
    # to unpack the values 

    FromCurrency_option = OptionMenu(root, variable1, *CurrencyCode_list)
    ToCurrency_option = OptionMenu(root, variable2, *CurrencyCode_list)

    FromCurrency_option.grid(row = 2, columns = 1, ipadx = 10)
    ToCurrency_option.grid(row = 3, columns = 1, ipadx = 10)

    # Create a Convert Button and attached  
    # with CurrencyExchangeRate function

    button1 = Button(root, text = "Convert", bg = 'blue', fg = 'black',
                                command = CurrencyConversion)

    button1.grid(row = 4, column = 1)

    # Create a Clear Button and attached  
    # with delete function  
    button2 = Button(root, text = "Clear", bg = "red",  
                     fg = "black", command = clear_all) 
    button2.grid(row = 6, column = 1) 

    # Start the GUI  
    root.mainloop()  
Any help is appreciated.

Thanks


RE: Currency converter - DPaul - Jun-14-2020

Is it the dictionary part the you want to come from somewhere else,
instead of hardcoded ?

Paul


RE: Currency converter - Scott - Jun-14-2020

Hi Paul,

I want this part:

    FromCurrency_option = OptionMenu(root, variable1, *CurrencyCode_list)
    ToCurrency_option = OptionMenu(root, variable2, *CurrencyCode_list)
to reference the dataframe df above to access the exchange rate by using the drop down boxes in the calculator to lookup the column and row in the dataframe.

The original code is in this link:

https://www.geeksforgeeks.org/python-real-time-currency-convertor-using-tkinter/

and the part I changed is this:

   #store dataframe rates
    rates = df
    
    # main_rates variable store complete url 
    main_rates = rates + "&from_currency =" + from_currency + "&to_currency =" + to_currency
thanks


RE: Currency converter - pyzyx3qwerty - Jun-14-2020

Please use proper code tags while posting - see BBCode to know more


RE: Currency converter - DPaul - Jun-14-2020

Figuring out 170 lines of code, not always easy :-)
But i did something similar with panda, and maybe it will be of some help.
XYZ= panda.DataFrame(excelsheet) 
        e = []
        for i in XYZ.index:
            e.append(XYZ['column1name'][i])
            e.append(XYZ['column2name'][i])
etc...
            
Now you have a list, which makes it easier to manipulate.

Paul


RE: Currency converter - Scott - Jun-14-2020

Thanks Paul,

I was able to solve the issue with this simple code:

exchange_rate = df[from_currency][to_currency]
Regards