function passed in tkinter after() method can't give return value

I’m trying to replace sleep() with after() but I need to create a function which will give me a return value that I can store and I can’t figure out how. Let’s take this code :

import tkinter
root = tkinter.Tk()

def test(i):
    o=i*2
    return o

print(root.after(5000,test,6))

root.mainloop()

This results in this output:
after#2914 which is a string

What can I do? I tried storing the return of the function in a first variable like so:

v=test(6)
print(root.after(5000,v))

but this error pops up:
'int' object has no attribute '__name__'

I also tried using threading instead of after() but it doesn’t solve the initial problem (the tkinter window stops responding during sleep).

>Solution :

Hope this help

import tkinter
root = tkinter.Tk()

class App:
    def __init__(self):
        self.result = None
        self.label = tkinter.Label(root, text="Result will be shown here")
        self.label.pack()

    def test(self, i):
        o = i * 2
        self.result = o
        self.label.config(text="Result: {}".format(o))

app = App()

root.after(5000, app.test, 6)
root.mainloop()

Leave a Reply