I'm learning Python and I'm working on my CS50P final project. I'm trying to make a simple Pokedex with a Tkinter UI. One function I'm trying to implement is when I input a Pokemon name in the search field and click search, the program gets the pokemon sprite image from PokeAPI using the requests library, then stores the image in a Tk label.
The problem I'm having is the image does not update in the UI unless it raises an exception. This are the lines that are supposed to update the images:
global img_default_label
def get_image(url):
image_response = requests.get(url)
resized_img = Image.open(BytesIO(image_response.content)).resize((200, 200))
return ImageTk.PhotoImage(resized_img)
def search_entry():
search_input = search_entry_field.get()
for entry in pokedex:
if entry["name_id"] == search_input:
img_default_url = entry["sprite"]
img_default = get_image(img_default_url)
img_default_label.configure(image=img_default)
img_default_label.grid(row=0, column=0)
If I change the last two lines to this:
img_default_label.configure(image=img_default).grid(row=0, column=0)
It raises an exception because I'm trying to apply .grid() to the return of .configure() which apparently is None. But when I do this, THE IMAGE UPDATES CORRECTLY!!!
Please, what am I doing wrong? what could I try? another, easier way to handle images in tkinter?
Thanks in advance!
If I change the last two lines to this:
img_default_label.configure(image=img_default).grid(row=0, column=0)
It raises an exception because I'm trying to apply .grid() to the return of .configure() which apparently is None. But when I do this, THE IMAGE UPDATES CORRECTLY!!!