I am making a small project currency converter. When I convert USD to any other currency, it correctly displays the correct answer. If I choose another currency except for USD, the result comes as the same digit/amount as I put. But I want to convert entered currency into to_currency. How can I fix it?
# Import the modules needed
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import requests
url = 'https://v6.exchangerate-api.com/v6/8a0fcd7df32ea4704f4fc48d/latest/USD'
response = requests.get(url)
response.raise_for_status()
data = response.json()
currency_key = data['conversion_rates']
def convert():
input_02.delete(0, END)
amount = input_01.get()
if amount == "":
messagebox.showinfo(title="Invalid", message="Please Insert the Amount")
return False
currency_02 = to_currency.get()
for i, j in currency_key.items():
var01 = i.upper()
var02 = currency_02.upper()
if var01 in var02:
try:
if from_currency.get() != 'USD':
amount = float(amount)/j
value = amount*j
input_02.insert(0, f"{value}")
else:
value = j*float(amount)
value = round(value, 4)
input_02.insert(0, f"{value}")
except ValueError:
messagebox.showinfo(title="Invalid", message="Please insert Amount in Digits")
window = Tk()
window.title('Currency Converter!')
window.iconbitmap('currency.ico')
window.config(padx=50, bg='#0CA7D3')
label_01 = Label(text="Currency Converter GUI", bg='#0CA7D3', foreground='white', font=("arial", 20, "bold"))
label_01.grid(column=0, row=0, columnspan=3, pady=15)
from_currency = StringVar()
from_currency.set(" USD ")
dropdown_01 = ttk.Combobox(textvariable=from_currency, width=16, font=("arial", 10, "bold"), state='enable',
values=list(currency_key))
dropdown_01.grid(column=0, row=1)
input_01 = Entry(font=("arial", 10, "bold"), width=19)
input_01.focus()
input_01.grid(column=0, row=2, pady=5)
to_currency = StringVar()
to_currency.set(" PKR ")
dropdown_02 = ttk.Combobox(textvariable=to_currency, width=16, font=("arial", 10, "bold"), state='readonly',
values=list(currency_key))
dropdown_02.grid(column=2, row=1)
input_02 = Entry(font=("arial", 10, "bold"), width=19)
input_02.grid(column=2, row=2, pady=5)
button = Button(text="Convert", command=convert, width=8, bg='#0084AB', fg='white', font=("arial", 10, "bold"))
button.grid(column=0, row=3, pady=20)
window.mainloop()
CodePudding user response:
I have not tried to run the code, but looking at this:
amount = float(amount)/j
value = amount*j
input_02.insert(0, f"{value}")
You are multiplying and dividing amount
by j
, so the final value
is equal to the initial amount
.
As a side note, I would try to use variable names which are a little bit more descriptive.
CodePudding user response:
The rate for from_currency
to to_currency
can be calculated by:
rate = currency_key[to_currency.get()] / currency_key[from_currency.get()]
Below is the modified convert()
:
def convert():
amount = input_01.get().strip()
if amount == "":
messagebox.showinfo(title="Invalid", message="Please insert the amount")
return False
try:
rate = currency_key[to_currency.get()] / currency_key[from_currency.get()]
value = float(amount) * rate
input_02.delete(0, "end")
input_02.insert("end", value)
except ValueError:
messagebox.showinfo(title="Invalid", message="Please insert amount in digits")