Home > Software design >  how to connect api to label widget in python tkinter?
how to connect api to label widget in python tkinter?

Time:10-20

I am making a gui for employee management system using python tkinter and sqlite3. In this gui user can add, view, delete and update employee info. There will be also quote of the day label which will be done using api by data extraction . The quote in gui will change as per the api.

from tkinter import *
from tkinter.messagebox import *
from datetime import *
from tkinter.scrolledtext import *
from sqlite3 import *
import matplotlib.pyplot as plt
import requests
import bs4
def quote_label(mw_lbl_quote):
    def quote():
        try:
            wa = "https://www.brainyquote.com/quote_of_the_day" 
            res = requests.get(wa)
            data = bs4.BeautifulSoup(res.text, "html.parser")
            info = data.find("img",{"class","p-qotd"})
            q = info["alt"]
            label.config(text=str(q))
        except Exception as e:
            showerror("issue ", e)

mw_lbl_quote = Label(main_window, text="Quote of the Day", font=f)
mw_lbl_quote.pack()
quote_label(mw_lbl_quote)

When i run the code the quote is not displayed. I dont know what i am doing wrong since no error is shown when i run it. What should i do to make it right?

CodePudding user response:

A couple of things to point out:

  • You create your Label with the master main_window, which does not exist anywhere i the code you posted. You have to create that by adding main_window = Tk() and also by starting the GUI at the end of your code via main_window.mainloop(). Im sure this is elsewhere in your code, i renamed it to root and added it to be reproducible.
  • Also in your Label, you specified font=f, and f doesnt exist either. Just remove it
  • You call your function wrong. Have a look here on how functions work. In my example i added a button to call the function and corrected it.
  • In your function you try to configure your label label, however you defined your label as mw_lbl_quote
  • In general, star imports are a bad idea, so i replaced them with the actual classes you want to import and commented out the ones you dont use.

Please mark the answer as right if this helped.

from tkinter import Tk, Label, Button
# from tkinter.messagebox import *
# from datetime import *
# from tkinter.scrolledtext import *
# from sqlite3 import *
# import matplotlib.pyplot as plt
import requests
import bs4
def quote_label():
    try:
        wa = "https://www.brainyquote.com/quote_of_the_day" 
        res = requests.get(wa)
        data = bs4.BeautifulSoup(res.text, "html.parser")
        info = data.find("img",{"class","p-qotd"})
        q = info["alt"]
        mw_lbl_quote.config(text=f"Quote of the day: {q}")
    except Exception as e:
        print(e)

root = Tk()

mw_lbl_quote = Label(root, text="Quote of the day: ")
mw_lbl_quote.pack()
mw_btn_quote = Button(root, text="Call the function", command=quote_label)
mw_btn_quote.pack()

root.mainloop()
  • Related