Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Currency converter
#1
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
Reply
#2
Is it the dictionary part the you want to come from somewhere else,
instead of hardcoded ?

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
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-rea...g-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
Reply
#4
Please use proper code tags while posting - see BBCode to know more
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#5
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#6
Thanks Paul,

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

exchange_rate = df[from_currency][to_currency]
Regards
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  currency converter using forex-python preethy12ka4 8 732 Mar-08-2024, 06:59 PM
Last Post: preethy12ka4
  Help with basic weight converter PythonSquizzel 3 1,423 Jun-29-2022, 02:42 PM
Last Post: deanhystad
  xml extract currency 3lnyn0 5 1,703 Apr-30-2022, 10:29 AM
Last Post: snippsat
  Converter Souls99 2 2,395 Aug-01-2020, 01:27 AM
Last Post: Souls99
  Help me get this image converter code working? NeTgHoSt 0 2,090 Jul-14-2020, 10:36 PM
Last Post: NeTgHoSt
  How to use 2to3.py converter under Windows OS samsonite 2 7,327 Mar-02-2019, 05:49 AM
Last Post: samsonite
  Currency formatting the third element in a 2D list RedSkeleton007 3 3,218 Mar-09-2018, 12:53 PM
Last Post: j.crater
  Can't get a downloaded file converter to work Godotisnothere 1 3,191 Jan-24-2017, 06:01 PM
Last Post: Larz60+
  rounding locale.currency birdieman 8 10,611 Dec-28-2016, 01:48 AM
Last Post: birdieman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020