Skip to content
Related Articles

Related Articles

Get Silver and Gold Price in Tkinter Using Python
  • Last Updated : 24 Jan, 2021

Prerequisite: BeautifulSoup, requests, tkinter, datetime

In this article, we are going to discuss how to get Gold and Silver Price in India Using Python in Tkinter. There is a variation in price with time to time, price of gold and silver is moved by a combination of supply, demand, and investor behavior.

We will use goodreturns website for fetching gold and silver price. We will create multiple functions for different operation:

  • silver_price(): This function is used for getting the silver price. For silver, we will use this URL.
  • gold_price(): This function is used for getting the gold price. For Gold, we will use this URL.
  • get_number_from_string(): This function is used to extract integer or float from string.
  • change_price(): This function is used to get the gold or silver price of a particular weight. 

Step-by-step Approach:

Step1#

Import Required Libraries.



Python3




# Import Required library
from bs4 import BeautifulSoup
import requests
from tkinter import *
from datetime import date
from tkinter import ttk

Step2#

Get Silver and Gold Price.

Python3




# method to get the price of silver
def silver_price():
  
    # getting the request from url
    data = requests.get(
        "https://www.goodreturns.in/silver-rates/#Today+24+Carat+Gold+Rate+Per+Gram+in+India+%28INR%29")
  
    # converting the text
    soup = BeautifulSoup(data.text, 'html.parser')
  
    # finding metha info for the current price
    price = soup.find("div", class_="gold_silver_table right-align-content").find(
        "tr", class_="odd_row").findAll("td")
  
    # returnng the price in text
    return price[1].text
  
  
# method to get the price of gold
def gold_price():
  
    # getting the request from url
    data = requests.get(
        "https://www.goodreturns.in/gold-rates/#Today+24+Carat+Gold+Rate+Per+Gram+in+India+%28INR%29")
  
    # converting the text
    soup = BeautifulSoup(data.text, 'html.parser')
  
    # finding metha info for the current price
    price = soup.find("div", class_="gold_silver_table right-align-content").find(
        "tr", class_="odd_row").findAll("td")
  
    # returnng the price in text
    return price[1].text

Step3#

Get Integer or float value from the string.

Python3




# Extract Integer or Float from String
def get_number_from_string(string):
    return float(''.join([x for x in string if x.isdigit() or x == '.']))

Step4#



Get the price of gold and silver for a particular weight.

Python3




# Get price for a particular weight
def change_price(weight_value):
  
    g_price = float(weight_value)*get_number_from_string(gold_price())
    s_price = float(weight_value)*get_number_from_string(silver_price())
  
    silver_price_label.config(text=f"{s_price} Rs")
    gold_price_label.config(text=f"{g_price} Rs")

Below is the implementation based on the above approach:

Python3




# Import Required library
from bs4 import BeautifulSoup
import requests
from tkinter import *
from datetime import date
from tkinter import ttk
  
  
# Extract Integer or Float from String
def get_number_from_string(string):
    return float(''.join([x for x in string if x.isdigit() or x == '.']))
  
  
# Returns the current local date
today = date.today()
  
  
# method to get the price of silver
def silver_price():
  
    # getting the request from url
    data = requests.get(
        "https://www.goodreturns.in/silver-rates/#Today+24+Carat+Gold+Rate+Per+Gram+in+India+%28INR%29")
  
    # converting the text
    soup = BeautifulSoup(data.text, 'html.parser')
  
    # finding metha info for the current price
    price = soup.find("div", class_="gold_silver_table right-align-content").find(
        "tr", class_="odd_row").findAll("td")
  
    # returnng the price in text
    return price[1].text
  
  
# method to get the price of gold
def gold_price():
  
    # getting the request from url
    data = requests.get(
        "https://www.goodreturns.in/gold-rates/#Today+24+Carat+Gold+Rate+Per+Gram+in+India+%28INR%29")
  
    # converting the text
    soup = BeautifulSoup(data.text, 'html.parser')
  
    # finding metha info for the current price
    price = soup.find("div", class_="gold_silver_table right-align-content").find(
        "tr", class_="odd_row").findAll("td")
  
    # returnng the price in text
    return price[1].text
  
  
# Get price for a particular weight
def change_price(weight_value):
  
    g_price = float(weight_value)*get_number_from_string(gold_price())
    s_price = float(weight_value)*get_number_from_string(silver_price())
  
    silver_price_label.config(text=f"{s_price} Rs")
    gold_price_label.config(text=f"{g_price} Rs")
  
  
# Create Object
root = Tk()
  
# Set Geometry
root.geometry("400x400")
  
# Create Label
Label(root, text="SILVER AND GOLD PRICE", font=(
    "Helvetica 15 bold"), fg="blue").pack()
  
# Frame1
frame1 = Frame(root)
frame1.pack(pady=20)
  
Label(frame1, text="Today Date:- ", font=("Helvetica 15 bold")).pack(side=LEFT)
Label(frame1, text=today, font=("Helvetica 15")).pack()
  
  
# Frame2
frame2 = Frame(root)
frame2.pack(pady=20)
  
# Set Variable
variable = StringVar(root)
variable.set("1")
  
Label(frame2, text="Select Weight:- ",
      font=("Helvetica 15 bold")).pack(side=LEFT)
w = OptionMenu(frame2, variable, "1", "8", "100",
               "500", "1000", command=change_price)
w.pack(side=LEFT)
Label(frame2, text="gm", font=("Helvetica 15")).pack(side=LEFT)
  
  
# Frame3
frame3 = Frame(root)
frame3.pack()
  
Label(frame3, text="Silver Price:- ", font=("Helvetica 15 bold")).pack(side=LEFT)
silver_price_label = Label(frame3, text="", font=("Helvetica 15"))
silver_price_label.pack(pady=20)
  
  
# Frame4
frame4 = Frame(root)
frame4.pack()
  
Label(frame4, text="Gold Price:- ", font=("Helvetica 15 bold")).pack(side=LEFT)
gold_price_label = Label(frame4, text="", font=("Helvetica 15"))
gold_price_label.pack(pady=20)
  
# Execute Tkinter
root.mainloop()

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :