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

Question:

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).

Asked By: v_suspicious

||

Answers:

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()
Answered By: Fahrizalucky

It will easier for you/

import tkinter
root = tkinter.Tk()

def test(i):
    o=i*2
    label.configure(text=o)
    return o

root.after(5000,test,6)
 
label = tkinter.Label(root, text="Result will be shown here")
label.pack()
root.mainloop()

test(6)
Answered By: toyota Supra
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.