Currency Converter in Python - Beginner Project
Key Takeaways
This video demonstrates how to build a simple currency converter in Python using a graphical user interface and a third-party package called Forex Python. The application allows users to choose a currency, enter an amount, and convert it to another currency.
Full Transcript
what is going on guys welcome back in this video today we're going to build a currency converter in Python so let us get right into [Music] it all right so let us take a brief look at what we're going to end up with here at the end of this video this is going to be our final application a currency converter a simple graphical user interface with two drop downs where you can choose a currency for example USD to Euro and then we can type an amount for example $5 and then we can click convert and we get 5 usds equal to € 4.64 and you can of course change the currency you can also get theate itself if you type one and that is what we're going to build it's a very simple application it's a very beginner friendly project we're going to learn how to work with graphical user interfaces and we're going to use a third party package that gets us the conversion rates so this what we're going to build and we're going to start by installing the package that I just talked about so we're going to say pip or pip pre-install Forex Das python this is the package that gets us the uh conversion rates and then we're going to get right into it by importing TK inter s TK this is going to be for the graphical user interface it's a core python package and then we're going to also from Forex python import currency rates um actually from Forex python. converter import currency rates now as far as I know this package does not have any functionality that gives us a collection of the available currencies so we're going to just Define a list of the currencies that we want to support so we can say something like common currencies and we can say uh for example USD Euro great breaden pound you need to use of course the correct symbols because they are identifiers for the currencies uh Japanese Yen we can use Canadian dollars we can use Australian dollars uh we can use the Swiss currency and New Zealand dollars and yeah basically you can add whatever you want as long as it's a valid currency and then we're going to build everything else as a class so we're going to say class currency currency converter and we're going to define the graphical user interface in the init methods so in the Constructor basically we're going to say self root is equal to tk. TK which is just our basic window we're going to say self root title is going to be currency converter self root geometry is let's say 200 * uh 180 and then what we're going to do is we're going to Define string variables and option menus now the string variables are going to be the currencies that are chosen so from the list here basically and the option menu is the drop down that then is connected to that variable so what we're going to do is we're going to say self. from VAR is going to be equal to a string variable so TK string uh variable and it's going to have root as a parent and we're going to set it to be uh the default to have a default value of USD and then we're going to say from menu is going to be equal to TK option menu self root self from uh VAR which is our string variable and then we need to um unpack the common currencies here to provide them as a uh list of values for the dropdown so this is just a drop- down menu we say it's part of the root window uh the value of it is the string variable here and or this is the string variable associated with it and the possible values are uh the list unpacked so all these values here all right uh then we're going to just pack all this so from menu pack we're going to use a padding of one um on the vertical axis and then I can just copy this and I can repeat the same thing just that I'm going to replace from with two and we're going to set the default here to Euro and the rest is actually the same then we need to have a label for the amount for the final conversion so self amount or actually no we need the label in the end we need to have the um the label for the text box for the entry and then we need to have another label at the bottom uh for the actual converted amount so we're going to say amount label here is going to be equal to TK label um going to be part of self root and the text is just going to be amount I'm going to pack this as well padding y equal 1 and then we want to add an amount entry which is just going to be a simple text box so TK entry part of self root and uh actually we don't need anything else here so just pack it as well with a padding y = to 1 and then we're going to have the convert button and the final result label so self. convertor button is going to be equal to TK button self root text is going to be convert obviously and the command is going to be a function that we don't have yet and this is going to be the convert currency method so we're going to Define it down here convert currency and we're going to just add a pass for now so that we have the function or the method but it doesn't do anything uh and then we're going to say self. convert button pack again with a padding of one on the Y AIS and finally we want to have the result so the result label is going to be equal to tk. label um self. root then the text is going to be empty by default and we're going to pack pack this as well so result label pack with a padding off you guessed it one there you go so in the end just a simple self root main Loop call so that we actually have our user interface running and the only magic now needs to happen in the actual convert currency function which is not too difficult to implement so we're just going to say try uh we do a try to avoid having uh problems with uh invalid numbers and problems with invalid currencies so we're going to try to do the conversion but it can fail of course so we're going to say from Curren is going to be equal to self. from variable get so this gets us the value then uh two currency two variable and then the amount is a typ casted uh float here so we typ cast the content of self amount entry. get so we get the content from the amount entry then we typ cast it to a float that's the amount and then we get the conversion rates so we can say C rates is equal to currency rates and then we can say that the rate we need is uh the following we want to get from C rates get the rate and then from currency to currency this will give us a rate and then we just need to have a converted amount which is going to be the amount times the rate obviously and all we need to do then is we need to take the result label and change the text so the text is then going to be a formatted string we're going to say amount from currency equals converted amount up until two decimal places um and then two currency there you go um that's basically it what's the problem here yeah we don't have an accept block so we need to also say accept and uh yeah depending on what it is if it's a value error we're going to say uh let's just copy this and we're going to change the text here to please enter a valid number and if it's uh any other exception we're going to say error occurred this is how you write that I think so all right so in order to actually use this now all we have to do is we have to create an instance of this class so if uncore name uncore uncore equals uncore uncore maincore uncore then currency converter constructed there you go and we have our application I can choose uh some currency I can enter an amount I can get a conversion there you go and of course if I get the same uh currency I get of course the same conversion so yeah this is how you build a simple currency converter in Python so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye
Original Description
In this video, we build a currency converter in Python with a GUI.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
💼 Services 💼
💻 Freelancing & Tutoring: https://www.neuralnine.com/services
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →Related AI Lessons
🎓
Tutor Explanation
DeepCamp AI