By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.


Gold Coder
Gets a random quote from internet. Tkinter and python

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)


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


Buy us a coffee!

Top Bottom