Skip to content

Commit 04c3a2c

Browse files
committed
add currency converter gui tutorial
1 parent 6d67a16 commit 04c3a2c

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -229,5 +229,6 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
229229
- [How to Make a Typing Speed Tester with Tkinter in Python](https://www.thepythoncode.com/article/how-to-make-typing-speed-tester-in-python-using-tkinter). ([code](gui-programming/type-speed-tester))
230230
- [How to Make a Planet Simulator with PyGame in Python](https://www.thepythoncode.com/article/make-a-planet-simulator-using-pygame-in-python). ([code](gui-programming/planet-simulator))
231231
- [How to Make a Markdown Editor using Tkinter in Python](https://www.thepythoncode.com/article/markdown-editor-with-tkinter-in-python). ([code](gui-programming/markdown-editor))
232+
- [How to Build a GUI Currency Converter using Tkinter in Python](https://www.thepythoncode.com/article/currency-converter-gui-using-tkinter-python). ([code](gui-programming/currency-converter-gui/))
232233

233234
For any feedback, please consider pulling requests.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Build a GUI Currency Converter using Tkinter in Python](https://www.thepythoncode.com/article/currency-converter-gui-using-tkinter-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# importing everything from tkinter
2+
from tkinter import *
3+
# importing ttk widgets from tkinter
4+
from tkinter import ttk
5+
import requests
6+
# tkinter message box for displaying errors
7+
from tkinter.messagebox import showerror
8+
9+
10+
API_KEY = 'put your API key here'
11+
12+
# the Standard request url
13+
url = f'https://v6.exchangerate-api.com/v6/{API_KEY}/latest/USD'
14+
15+
# making the Standard request to the API
16+
response = requests.get(f'{url}').json()
17+
18+
# converting the currencies to dictionaries
19+
currencies = dict(response['conversion_rates'])
20+
21+
def convert_currency():
22+
# will execute the code when everything is ok
23+
try:
24+
# getting currency from first combobox
25+
source = from_currency_combo.get()
26+
# getting currency from second combobox
27+
destination = to_currency_combo.get()
28+
# getting amound from amount_entry
29+
amount = amount_entry.get()
30+
# sending a request to the Pair Conversion url and converting it to json
31+
result = requests.get(f'https://v6.exchangerate-api.com/v6/{API_KEY}/pair/{source}/{destination}/{amount}').json()
32+
# getting the conversion result from response result
33+
converted_result = result['conversion_result']
34+
# formatting the results
35+
formatted_result = f'{amount} {source} = {converted_result} {destination}'
36+
# adding text to the empty result label
37+
result_label.config(text=formatted_result)
38+
# adding text to the empty time label
39+
time_label.config(text='Last updated,' + result['time_last_update_utc'])
40+
# will catch all the errors that might occur
41+
# ConnectionTimeOut, JSONDecodeError etc
42+
except:
43+
showerror(title='Error', message="An error occurred!!. Fill all the required field or check your internet connection.")
44+
45+
46+
# creating the main window
47+
window = Tk()
48+
49+
# this gives the window the width(310), height(320) and the position(center)
50+
window.geometry('310x340+500+200')
51+
52+
# this is the title for the window
53+
window.title('Currency Converter')
54+
55+
# this will make the window not resizable, since height and width is FALSE
56+
window.resizable(height=FALSE, width=FALSE)
57+
58+
# colors for the application
59+
primary = '#081F4D'
60+
secondary = '#0083FF'
61+
white = '#FFFFFF'
62+
63+
# the top frame
64+
top_frame = Frame(window, bg=primary, width=300, height=80)
65+
top_frame.grid(row=0, column=0)
66+
67+
# label for the text Currency Converter
68+
name_label = Label(top_frame, text='Currency Converter', bg=primary, fg=white, pady=30, padx=24, justify=CENTER, font=('Poppins 20 bold'))
69+
name_label.grid(row=0, column=0)
70+
71+
72+
# the bottom frame
73+
bottom_frame = Frame(window, width=300, height=250)
74+
bottom_frame.grid(row=1, column=0)
75+
76+
# widgets inside the bottom frame
77+
from_currency_label = Label(bottom_frame, text='FROM:', font=('Poppins 10 bold'), justify=LEFT)
78+
from_currency_label.place(x=5, y=10)
79+
80+
to_currency_label = Label(bottom_frame, text='TO:', font=('Poppins 10 bold'), justify=RIGHT)
81+
to_currency_label.place(x=160, y=10)
82+
83+
# this is the combobox for holding from_currencies
84+
from_currency_combo = ttk.Combobox(bottom_frame, values=list(currencies.keys()), width=14, font=('Poppins 10 bold'))
85+
from_currency_combo.place(x=5, y=30)
86+
87+
# this is the combobox for holding to_currencies
88+
to_currency_combo = ttk.Combobox(bottom_frame, values=list(currencies.keys()), width=14, font=('Poppins 10 bold'))
89+
to_currency_combo.place(x=160, y=30)
90+
91+
# the label for AMOUNT
92+
amount_label = Label(bottom_frame, text='AMOUNT:', font=('Poppins 10 bold'))
93+
amount_label.place(x=5, y=55)
94+
95+
# entry for amount
96+
amount_entry = Entry(bottom_frame, width=25, font=('Poppins 15 bold'))
97+
amount_entry.place(x=5, y=80)
98+
99+
# an empty label for displaying the result
100+
result_label = Label(bottom_frame, text='', font=('Poppins 10 bold'))
101+
result_label.place(x=5, y=115)
102+
103+
# an empty label for displaying the time
104+
time_label = Label(bottom_frame, text='', font=('Poppins 10 bold'))
105+
time_label.place(x=5, y=135)
106+
107+
# the clickable button for converting the currency
108+
convert_button = Button(bottom_frame, text="CONVERT", bg=secondary, fg=white, font=('Poppins 10 bold'), command=convert_currency)
109+
convert_button.place(x=5, y=165)
110+
111+
112+
# this runs the window infinitely until it is closed
113+
window.mainloop()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

0 commit comments

Comments
 (0)