I'm a beginner, i just started programming and im trying to make a currency app, and its not working. What is the problem, and how do i fix it and make it work? im also new to stackoverflow so bare with me. im trying to make a first page with frame1 where you have 3 options/buttons to press where each button takes you to a new page.
import tkinter as tk
from requests import get
from pprint import PrettyPrinter
printer = PrettyPrinter()
def get_currencies():
endpoint = f"api/v7/currencies?apiKey={APY_KEY}"
url = BASE_URL + endpoint
data = get(url).json()['results']
data = list(data.items())
data.sort()
return data
def list_currencies(currencies):
for name, currency in currencies:
name = currency['currencyName']
_id = currency['id']
symbol = currency.get('currencySymbol', '')
print(f"{_id} - {name} - {symbol}")
def exchange_rate(currency1, currency2):
endpoint = f"api/v7/convert?q={currency1}_{currency2}&compact=ultra&apiKey={APY_KEY}"
url = BASE_URL + endpoint
response = get(url)
data = response.json()
if len(data) == 0:
print('Invalid currency')
return
rate = list(data.values())[0]
print(f"1:{currency1} = {rate}:{currency2}")
return rate
def convert(currency1, currency2, amount):
rate = exchange_rate(currency1, currency2)
if rate is None:
return
try:
amount = float(amount)
except:
print('Invalid amount! ')
return
conv_amount = rate * amount
print(f"{amount}{currency1} = {conv_amount}{currency2}")
root = tk.Tk()
root.title('The currency converter!')
root.geometry('400x500')
def frame2():
frame2 = tk.Frame(root)
frame2.pack()
lable = tk.Label(frame2, text='Here you can compare values of diffrent cirrencies')
lable.pack()
text = tk.Text(frame2)
text.insert(tk.END,'Write in the ticker of the first currency')
text.pack()
currency1 = tk.Entry(frame2,)
currency1.grid(row=0, column=0, sticky= tk.W +tk.E)
text = tk.Text(frame2)
text.insert(tk.END,'Write in the ticker of the second currency')
text.pack()
currency2 = tk.Entry(frame2,)
currency2.grid(row=1, column=0, sticky= tk.W +tk.E)
response = tk.Text(frame3)
response.insert(exchange_rate(currency1,currency2))
response.pack()
exchange_rate(currency1,currency2)
def frame3():
frame3 = tk.Frame(root)
frame3.pack()
lable = tk.Label(frame3, text='Here you can convert values of diffrent currencies')
lable.pack()
text = tk.Text(frame3)
text.insert(tk.END,'Write in the ticker of the first currency')
text.pack()
currency1 = tk.Entry(frame3,)
currency1.grid(row=0, column=0, sticky= tk.W +tk.E)
text = tk.Text(frame3)
text.insert(tk.END,'Write in the amount yhat you would like to convert')
text.pack()
amount = tk.Entry(frame3,)
amount.grid(row=1, column=0, sticky= tk.W +tk.E)
text = tk.Text(frame3)
text.insert(tk.END,'Write in the ticker of the second currency')
text.pack()
currency2 = tk.Entry(frame3,)
currency2.grid(row=2, column=0, sticky= tk.W +tk.E)
response = tk.Text(frame3)
response.insert(convert(currency1,currency2,amount))
response.pack()
def frame4():
frame4 = tk.Frame(root)
frame4.pack()
lable = tk.Label(frame3, text='Here you can see all currencies')
lable.pack()
currency_list = tk.Listbox(list_currencies())
currency_list.pack()
frame1 = tk.Frame(root)
frame1.pack()
lable = tk.Label(frame1, text='Welcome to the curency converter')
lable.pack(padx= 20, pady=20)
btn1 = tk.Button(frame1, text='Compare value', command= frame2())
btn1.grid(row=0, column=0, sticky= tk.W +tk.E)
btn2 = tk.Button(frame1, text='Convert value',command=frame3())
btn2.grid(row=1, column=0, sticky= tk.W +tk.E)
btn3 =tk.Button(frame1, text='List all currencies',command=frame4())
btn3.grid(row=2, column=0, sticky= tk.W +tk.E)
root.mainloop()