Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading