-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathadvice.py
36 lines (27 loc) · 923 Bytes
/
advice.py
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
# Importing libraries
import requests
import tkinter as tk
from tkinter import messagebox
# Fetching advice from the advice api
def advice():
try:
res = requests.get("https://api.adviceslip.com/advice").json()
advice_text.set(res["slip"]["advice"])
except requests.exceptions.RequestException:
messagebox.showerror(
"Error", "Failed to fetch advice. Please check your internet connection.")
# Create the main window
root = tk.Tk()
root.title("Random Advisor Application")
# Create and configure widgets
advice_text = tk.StringVar()
advice_label = tk.Label(root, textvariable=advice_text,
wraplength=400, font=("Arial", 14))
get_advice_button = tk.Button(root, text="Get Advice", command=advice)
# Pack widgets
advice_label.pack(pady=20)
get_advice_button.pack(pady=10)
# Initial advice fetching
advice()
# Run the main event loop
root.mainloop()