Home > Enterprise >  Tkinter - mainloop and after not working as expected (kanyewest quote training)
Tkinter - mainloop and after not working as expected (kanyewest quote training)

Time:09-08

Super() python newbie here. I am following an online training where I need to create a tkinter window to get Kanye West quotes and to be refreshed every X seconds.

I get the first refresh due to my window.after() but then it never happens again.

From my understanding, once the code between Tk() and mainloop() is done, it reads it again as part of the loop. So I should get a newquote immediately again and another one with the after, all looped.

I think Im missing a concept on the behavior of the mainloop...

Here is the code:

import tkinter, requests
from tkinter import *


def get_quotes():
    kanye = requests.get(url="https://api.kanye.rest")
    if kanye.raise_for_status() is None:
        quote = kanye.json()
        return quote["quote"]


def change_quote(text):
    quote_canvas.itemconfig(TEXT, text=text)


# --- UI design of application ---
window = Tk()
window.title("Kanye quotes API")
window.config(background="white", padx=20, pady=20)

# -- Canvas for quote ---
quote_canvas = Canvas(height=414, width=300, background="white", highlightthickness=0)
quote_img = tkinter.PhotoImage(file="Kanye REST API/images/background.png")
quote_canvas.create_image(150, 200, image=quote_img)
TEXT = quote_canvas.create_text(150, 180, text=get_quotes(), font=("courier", 20, "normal"),
                                      fill="black", justify=CENTER, width=280)
quote_canvas.grid(row=0, column=0)

# -- Canvas for emoji ---
emoji_canvas = Canvas(height=150, width=131, background="white", highlightthickness=0)
emoji_img = tkinter.PhotoImage(file="Kanye REST API/images/kanye.png")
emoji_canvas.create_image(70, 75, image=emoji_img)
emoji_canvas.grid(row=1, column=0)

NEW_TEXT = get_quotes()
window.after(3500, change_quote, NEW_TEXT)
window.mainloop()

CodePudding user response:

As for me. I comment out tkinter.PhotoImage.

import tkinter, requests
from tkinter import *


def get_quotes():
    kanye = requests.get(url="https://api.kanye.rest")
    if kanye.raise_for_status() is None:
        quote = kanye.json()
        return quote["quote"]


def change_quote(text):
    quote_canvas.itemconfig(TEXT, text=text)


# --- UI design of application ---
window = Tk()
window.title("Kanye quotes API")
window.config(background="white", padx=20, pady=20)

# -- Canvas for quote ---
quote_canvas = Canvas(height=414, width=300, background="white", highlightthickness=0)
#quote_img = tkinter.PhotoImage(file="Kanye REST API/images/background.png")
#quote_canvas.create_image(150, 200, image=quote_img)
TEXT = quote_canvas.create_text(150, 180, text=get_quotes(), font=("courier", 20, "normal"),
                                      fill="black", justify=CENTER, width=280)
quote_canvas.grid(row=0, column=0)

# -- Canvas for emoji ---
emoji_canvas = Canvas(height=150, width=131, background="white", highlightthickness=0)
#emoji_img = tkinter.PhotoImage(file="Kanye REST API/images/kanye.png")
#emoji_canvas.create_image(70, 75, image=emoji_img)
#emoji_canvas.grid(row=1, column=0)

NEW_TEXT = get_quotes()
window.after(3500, change_quote, NEW_TEXT)
window.mainloop()

First output:

enter image description here

The second output... wait for 5 seconds:

enter image description here

After that, you can execute it again.

CodePudding user response:

OK

I managed to make this work:

Change the function:

def change_quote():
    window.after(5000, change_quote)
    quote_canvas.itemconfig(TEXT, text=get_quotes())

And Im simply calling change_quote() before the .mainloop().

  • Related