{"id":3278,"date":"2022-08-20T20:59:11","date_gmt":"2022-08-20T15:29:11","guid":{"rendered":"https:\/\/cbsepython.in\/?p=3278"},"modified":"2022-08-20T21:01:34","modified_gmt":"2022-08-20T15:31:34","slug":"real-time-currency-converter-python-project-class-12","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/real-time-currency-converter-python-project-class-12\/","title":{"rendered":"Real Time Currency Converter Python Project Class 12"},"content":{"rendered":"<h2><span style=\"color: #000000;\">Real Time Currency Converter Python Project Class 12<\/span><\/h2>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Source Code:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#Python Project Currency Converter\r\n\r\nimport requests\r\nfrom tkinter import *\r\nimport tkinter as tk\r\nfrom tkinter import ttk\r\n\r\n\r\nclass RealTimeCurrencyConverter():\r\n    def __init__(self,url):\r\n            self.data = requests.get(url).json()\r\n            self.currencies = self.data['rates']\r\n\r\n    def convert(self, from_currency, to_currency, amount): \r\n        initial_amount = amount \r\n        if from_currency != 'USD' : \r\n            amount = amount \/ self.currencies[from_currency] \r\n  \r\n        # limiting the precision to 4 decimal places \r\n        amount = round(amount * self.currencies[to_currency], 4) \r\n        return amount\r\n\r\nclass App(tk.Tk):\r\n\r\n    def __init__(self, converter):\r\n        tk.Tk.__init__(self)\r\n        self.title = 'Currency Converter'\r\n        self.currency_converter = converter\r\n\r\n        #self.configure(background = 'blue')\r\n        self.geometry(\"500x200\")\r\n        \r\n        # Label\r\n        self.intro_label = Label(self, text = 'Real Time Currency Convertor @cbsepytho.in',  fg = 'blue', relief = tk.RAISED, borderwidth = 3)\r\n        self.intro_label.config(font = ('Courier',15,'bold'))\r\n\r\n        self.date_label = Label(self, text = f\"1 Indian Rupee equals = {self.currency_converter.convert('INR','USD',1)} USD \\n Date : {self.currency_converter.data['date']}\", relief = tk.GROOVE, borderwidth = 5)\r\n\r\n        self.intro_label.place(x = 10 , y = 5)\r\n        self.date_label.place(x = 160, y= 50)\r\n\r\n        # Entry box\r\n        valid = (self.register(self.restrictNumberOnly), '%d', '%P')\r\n        self.amount_field = Entry(self,bd = 3, relief = tk.RIDGE, justify = tk.CENTER,validate='key', validatecommand=valid)\r\n        self.converted_amount_field_label = Label(self, text = '', fg = 'black', bg = 'white', relief = tk.RIDGE, justify = tk.CENTER, width = 17, borderwidth = 3)\r\n\r\n        # dropdown\r\n        self.from_currency_variable = StringVar(self)\r\n        self.from_currency_variable.set(\"INR\") # default value\r\n        self.to_currency_variable = StringVar(self)\r\n        self.to_currency_variable.set(\"USD\") # default value\r\n\r\n        font = (\"Courier\", 12, \"bold\")\r\n        self.option_add('*TCombobox*Listbox.font', font)\r\n        self.from_currency_dropdown = ttk.Combobox(self, textvariable=self.from_currency_variable,values=list(self.currency_converter.currencies.keys()), font = font, state = 'readonly', width = 12, justify = tk.CENTER)\r\n        self.to_currency_dropdown = ttk.Combobox(self, textvariable=self.to_currency_variable,values=list(self.currency_converter.currencies.keys()), font = font, state = 'readonly', width = 12, justify = tk.CENTER)\r\n\r\n        # placing\r\n        self.from_currency_dropdown.place(x = 30, y= 120)\r\n        self.amount_field.place(x = 36, y = 150)\r\n        self.to_currency_dropdown.place(x = 340, y= 120)\r\n        #self.converted_amount_field.place(x = 346, y = 150)\r\n        self.converted_amount_field_label.place(x = 346, y = 150)\r\n        \r\n        # Convert button\r\n        self.convert_button = Button(self, text = \"Convert\", fg = \"black\", command = self.perform) \r\n        self.convert_button.config(font=('Courier', 10, 'bold'))\r\n        self.convert_button.place(x = 225, y = 135)\r\n\r\n    def perform(self):\r\n        amount = float(self.amount_field.get())\r\n        from_curr = self.from_currency_variable.get()\r\n        to_curr = self.to_currency_variable.get()\r\n\r\n        converted_amount = self.currency_converter.convert(from_curr,to_curr,amount)\r\n        converted_amount = round(converted_amount, 2)\r\n\r\n        self.converted_amount_field_label.config(text = str(converted_amount))\r\n    \r\n    def restrictNumberOnly(self, action, string):\r\n        regex = re.compile(r\"[0-9,]*?(\\.)?[0-9,]*$\")\r\n        result = regex.match(string)\r\n        return (string == \"\" or (string.count('.') &lt;= 1 and result is not None))\r\n\r\nif __name__ == '__main__':\r\n    url = 'https:\/\/api.exchangerate-api.com\/v4\/latest\/USD'\r\n    converter = RealTimeCurrencyConverter(url)\r\n\r\n    App(converter)\r\n    mainloop()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Output:<\/span><\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-3279 size-full\" src=\"https:\/\/cbsepython.in\/wp-content\/uploads\/2022\/08\/Currency-Converter.jpg\" alt=\"\" width=\"558\" height=\"245\" title=\"\" srcset=\"https:\/\/cbsepython.in\/wp-content\/uploads\/2022\/08\/Currency-Converter.jpg 558w, https:\/\/cbsepython.in\/wp-content\/uploads\/2022\/08\/Currency-Converter-300x132.jpg 300w, https:\/\/cbsepython.in\/wp-content\/uploads\/2022\/08\/Currency-Converter-400x176.jpg 400w\" sizes=\"(max-width: 558px) 100vw, 558px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Real Time Currency Converter Python Project Class 12 &nbsp; Source Code: #Python Project Currency Converter import requests from tkinter import * import tkinter as tk from tkinter import ttk class RealTimeCurrencyConverter(): def __init__(self,url): self.data = requests.get(url).json() self.currencies = self.data[&#8216;rates&#8217;] def convert(self, from_currency, to_currency, amount): initial_amount = amount if from_currency != &#8216;USD&#8217; : amount = amount [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":3279,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,43],"tags":[],"class_list":["post-3278","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cbse-sample-papers-class-12","category-python-projects"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/3278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=3278"}],"version-history":[{"count":0,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/3278\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media\/3279"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=3278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=3278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=3278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}