|
| 1 | +import tkinter as tk |
| 2 | +import requests |
| 3 | +from threading import Thread |
| 4 | + |
| 5 | +api = "http://api.quotable.io/random" |
| 6 | +quotes = [] |
| 7 | +quote_number = 0 |
| 8 | +MAX_QUOTES = 5 |
| 9 | + |
| 10 | +#Configuring Screen |
| 11 | +screen = tk.Tk() |
| 12 | +screen.geometry("900x268") |
| 13 | +screen.title("Random Quote Generator") |
| 14 | +screen.grid_columnconfigure(0, weight=1) |
| 15 | +screen.resizable (False, False) |
| 16 | +screen.configure(bg = '#5271E8') |
| 17 | + |
| 18 | + |
| 19 | +#Functions |
| 20 | +def load_quotes(): |
| 21 | + global quotes |
| 22 | + print('Loading Quotes. Please Wait a Moment') |
| 23 | + for x in range(MAX_QUOTES): |
| 24 | + random_quote = requests.get(api).json() |
| 25 | + content = random_quote['content'] |
| 26 | + author = random_quote['author'] |
| 27 | + quote = '"' + content + '"' + "\n\n" + "By " + author |
| 28 | + # print(quote) |
| 29 | + quotes.append(quote) |
| 30 | + |
| 31 | +load_quotes() |
| 32 | + |
| 33 | +def get_random_quote(): |
| 34 | + global quote_label |
| 35 | + global quotes |
| 36 | + global quote_number |
| 37 | + |
| 38 | + quote_label.configure(text=quotes[quote_number]) |
| 39 | + quote_number = quote_number + 1 |
| 40 | + # print(quote_number) |
| 41 | + if quotes[quote_number] == quotes[-3]: |
| 42 | + thread = Thread(target=load_quotes) |
| 43 | + thread.start() |
| 44 | + |
| 45 | + |
| 46 | +# UI |
| 47 | +quote_label = tk.Label(screen, text="Click to generate new Quote", bg='#C9F4F9',height=6, pady=10, wraplength=800, font=("Times New Roman", 15, 'italic')) |
| 48 | +quote_label.grid(row=0,column=0, stick="WE", padx=20, pady=10) |
| 49 | + |
| 50 | +button = tk.Button(text="Generate", command=get_random_quote, bg='#ffffff', font=("Times New Roman", 14)) |
| 51 | +button.grid(row=1,column=0, stick="WE", padx=20, pady=10) |
| 52 | + |
| 53 | +#Main Loop |
| 54 | +screen.mainloop() |
0 commit comments