-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathrandom_quote_generator.py
More file actions
49 lines (37 loc) · 1.43 KB
/
random_quote_generator.py
File metadata and controls
49 lines (37 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pip install ttkbootstrap
#pip install requests
import requests
import ttkbootstrap as ttk
#API to fetch quote data
URL = "https://api.quotable.io/random"
#function to fetch a quote from the API above
def fetch_quote():
response = requests.get(URL) #send a HTTP GET request
data = response.json() #Parse JSON into a dictionary
#Extract quote and author data
quote = data["content"]
author = data["author"]
return quote, author
#Function to update displayed quote and author with another fetched data
def update_quote():
quote,author = fetch_quote() #call the fetch_quote function to get new data
quote_label.config(text=quote) #Update the text of the quote label
author_label.config(text=f"~{author}") #Update the author of the quote label
#Create a GUI window
root=ttk.Window(themename="pulse")
root.title("Quotes generator")
#Set ttitle and window size
root.geometry("700x250")
#Create a frame within the window
frame = ttk.Frame(root)
frame.pack(padx=30, pady=40)
#Create a label to display the quote
quote_label = ttk.Label(frame, text="", font=("Helvetica", 16), wraplength=650)
quote_label.pack()
#Create a label to display the author
author_label = ttk.Label(frame, text="", font=("Helvetica", 12))
author_label.pack(pady=10)
#Create a button widget that calls the update_quote function
ttk.Button(frame, text="Get Quote", command=update_quote).pack(pady=20)
#Start the main loop to run the GUI
root.mainloop()