Python Forum
tkinter - Random Quote from internet
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter - Random Quote from internet
#1
Gets random quote from internet

import requests
import json
import tkinter as tk



root = tk.Tk()
root.title('Random quote of the day')

def get_quote():
    frame = tk.Frame(root)
    frame.grid(column=0, row=1, sticky='news', padx=1)

    url = 'https://api.quotable.io/random'
    quote = json.loads(requests.get(url).text)
    edits = ['dateAdded', 'dateModified']
    unwanted = ['length','_id', 'tags', 'authorSlug']
    index = 0

    for prefix, data in quote.items():
        prefix = 'quote' if prefix == 'content' else prefix 
        if prefix in edits:
            prefix = f'{prefix[0:4].title()}  {prefix[4:]}'
        if prefix not in unwanted:
            label = tk.Label(frame, text=prefix.title(), anchor='w')
            label['highlightbackground'] = 'black'
            label['highlightcolor'] = 'black'
            label['highlightthickness'] = 1
            label['font'] = None, 11, 'bold'
            label.grid(column=0, row=index, sticky='new', padx=(5,1), pady=5)

            label = tk.Label(frame, text=data, anchor='w', justify='left', padx=5, pady=5)
            label['highlightbackground'] = 'black'
            label['highlightcolor'] = 'black'
            label['highlightthickness'] = 1
            label['wraplength'] = 400
            label.grid(column=1, row=index, sticky='new', padx=(1,5), pady=5)

        index += 1

title = tk.Label(root, text='Quote of the day', font=(None, 25, 'bold'), pady=8, padx=8)
title['bg'] = 'slategray'
title['fg'] = 'white'
title.grid(column=0, row=0, sticky='new', padx=5, pady=5)



get_quote()

button = tk.Button(root, text='Get Random Quote', command=get_quote)
button.grid(column=0, row=2, sticky='news', padx=5, pady=5)

root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
tkinter - Random Quote from internet - by menator01 - Apr-02-2024, 07:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Test internet connection Larz60+ 14 12,273 Oct-14-2016, 03:14 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020