You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# we're going to use a module called requests to handle our API
importrequests
# we're going to use the API from randomfox.ca which gives us an image
dictionary=requests.get("https://randomfox.ca/floof/").json() # gives us a dictionary with two parts, the one we want is called: image, and is the url to the fox's image (example - ./image.png)
imageurl=dictionary["image"]
# now we can use Tkinter to display this image
importtkinter
myWindow=tkinter.Tk()
# we want to get the data from our imageurl
alldata=requests.get(imageurl)
# .content gives our data in a byte array
bytearray=alldata.content
# we're going to use BytesIO to convert our array into a photo
fromioimportBytesIO
fromPILimportImage
photo=Image.open(BytesIO(bytearray))
# we're going to use ImageTK to add an image to a Tkinter label
fromPILimportImageTk
TkinterImage=ImageTk.PhotoImage(photo)
myLabel=tkinter.Label(image=TkinterImage)
myLabel.pack() # "pack" (add) the label to the window