Python

Currency Converter Using Gui In Python

A ‘Currency converter’ is an app or tool that enables you to quickly convert any amount from one currency to another, anytime and anywhere. Creating this app in Python language is an immensely fun and interesting task.

We will have two tabs in this application. One is going to be our currency tab where we mention our home/base currency and the currency we want to convert our amount into, along with the rate of conversion. In the second tab, we define the amount we want to convert and then we get our desired result.

Via this article, you will get a clear understanding of the logic behind the code and working of this application.

Creating a GUI ‘TIC TAC TOE’ Using Python Language

Importing Tkinter Library and Starter Code
Import the Tkinter and message box library as shown below. We import ttk from Tkinter separately because we need to use and build tabs/notebooks.

Create an instance of Tkinter’s window, Tk(). Set the title of the window as ‘Currency Converter’. Now, set the dimensions of the window using the geometry method. We are taking the dimensions of windows as 500 X 500 pixels here.

The Code Block

Creating Our Notebook/ Two tabs

Create a notebook named ‘my_notebook’. Add it to the root and add a bit of padding too. This notebook will contain two tabs: Currencies and Convert. In the first, we define the names of the currencies we want the conversion to take place between and the conversion rate. In the latter, we mention the value of one currency we want to convert and then we get the value in the other currency as the result.

So, create two frames ‘currency_frame’ and ‘conversion_frame’ using the frame() function and set the height and width of the frame to 480 pixels. Next, pack both such that they cover the entire screen. This is done using ‘fill= both’ and ‘expand=1’ as shown below.

Now, add these frames or tabs to the notebook we have created above i.e., my_notebook. This is done using the ‘add’ function. Pass the frame names and the text you want to appear as the name of the tabs, as arguments in the function.

The Code Block

Creating and Defining the ‘CURRENCY’ Tab

First, we create the Home Currency frame. Create a label frame named ‘home’ and pass the arguments ‘currency_frame’ (the frame you are working on) and the text i.e., ‘Your Home Currency’. Add some padding.

Create an entry box for the home currency named ‘home_entry’ using the entry() function. Pass the label i.e., home and the font style and font size as the arguments. Add some padding along both the axis.

Next, we will create the Conversion Currency frame. Create a label frame named ‘conversion’ and pass the arguments ‘currency_frame’ and the text i.e., ‘Conversion Currency’.  Add some padding along the y axis.

This frame will have two labels along with their entry boxes namely, ‘Currency to convert to’ and ‘Conversion Rate’.

Create a label frame named ‘conversion_label’ and pass the arguments ‘conversion’ and the text i.e., ‘Currency to convert to’. Add some padding.

Let’s make an entry box for this label. Create an entry box named ‘conversion _entry’ using the entry() function. Pass the label i.e., conversion and the font style and font size as the arguments. Add some padding along the x and y-axis.

Create a label frame named ‘rate_label’ and pass the arguments ‘conversion’ (the frame we want to put it in) and the text i.e., ‘Currency Conversion Rate’. Add some padding.

Let’s make an entry box for this label. Create an entry box named ‘rate _entry’ using the entry() function. Pass the label i.e., conversion and the font style and font size as the arguments. Add some padding along the y axis.

Lastly, we have to create two buttons.

For that, first create a frame named ‘button_frame’ using the frame() function with ‘currency_frame’ as its argument. Add some padding along the y axis.

Next, create two buttons ‘lock_button’ and ‘unlock_button’ using the Button() function with button_frame, the respective texts, and their command/functions as arguments. Grid the buttons using the grid function and add some padding too (refer to the coding block).

We will define the two functions ‘Lock’ and ‘Unlock’ later in step 5.

The Code Block

Creating and Defining the ‘Conversion’ Tab

Create a label frame named ‘amount_label’ and pass the arguments ‘conversion_frame’ (the frame we want to put it in) and the text i.e., ‘Amount to Convert’. Add some padding along the y-axis.

Let’s make an entry box for this label. Create an entry box named ‘amount _entry’ using the entry() function. Pass the label i.e., ‘amount_label’ and the font style (Helvetica) and font size (24) as the arguments. Add some padding along both the x and y axis.

Next, Create a button named ‘convert_button’ using the button() function. Pass ‘amount_label’, text=”Convert” and command/function = Convert.Add some padding. We will define the ‘convert’ function later.

Next, we create our second label_frame named ‘converted_label’ and pass the arguments ‘conversion_frame’ (the frame we want to put it in) and the text i.e., ‘Converted Currency’. Add some padding along the y axis.

Let’s make an entry box for this label. Create an entry box named ‘converted _entry’ using the entry() function. Pass the following arguments: the label i.e., ‘converted_label’, set the font style (Helvetica) and font size (24), border to 0, and background to ‘systembuttonface’.Add some padding along both the x and y axis.

Finally, we have to make a clear button named ‘clear_button’ using the Button() function with conversion_frame, text=” Clear” and command/function= clear as arguments.Add some padding too along both the axis (refer to the coding block).

Let’s define the ‘clear’ function right now. We have to delete both the entries in the entry boxes by using the ‘delete’ function with 0 and END as arguments.

You will notice that the frames aren’t taking the full width of the screen, so we create a fake label named ‘spacer’ with width=68 for spacing it out. (Check the code block)

The Code Block

Lock and Unlock Functions

Let’s define both our functions.

LOCK
If any of the three entry boxes i.e., home_entry, conversion_entry, and rate_entry is not filled, then we pop up a warning using the message box module (Tkinter library) and the showwarning function.

Else if all of them are filled, we ‘disable’ all the three entry boxes (so that they aren’t edited or changed) and change the state of the second tab ‘conversion’ back to ‘normal’. So, we can now click and shift to our second tab once we have locked all the entries. (refer to the code block below).

We also want to change the amount_label, and converted_label (in our second tab according to what we have entered in our fields in the first/current tab). For this, we use the get() function and change the text in these labels. Refer to the code below.

UNLOCK
When we click on this button, we want to make our three entry boxes ‘editable’ (opposite of what we did in the lock). So, we just change their states back to ‘normal’. We also disable our second tab in this case

The Code Block

Convert Function

We firstly clear whatever was already there (just in case) in our ‘converted_entry’ box. Next, we will perform the conversion.

This is done by:

conversion = float( rate_entry.get() * amount_entry.get())

We round this to two decimal places by using the round function.

We also add commas format in our number. This is done by using ‘{:;}’.format(conversion).

Finally, we will now update our ‘converted_entry’ with ‘conversion’ calculated above by using the insert function.

The Code Block

Finally, our program is ready for execution.

The Conclusion

In this manner, you can create ‘CURRENCY CONVERTER’ on your own. It’s a fun and great learning experience to create this application using python GUI. Please note that this post is divided into parts to maintain the neatness of the code and for your easy understanding.

I hope you enjoyed reading this article and found it useful.

Have a nice day☺

About the author

Shalini Salotra