The TKinter 'after' method is executing immediately, then pausing for the 3 second time after execution. If I also use the 'after' method in my CheckStatus function, it goes onto a rapid loop and never gets to the mainloop().
What am I doing wrong? the documentation says the function would be called after the pause time, but its actually happening before. I want to call CheckStatus every second for a hardware input on Raspberry Pi, as well as have the normal mainloop responding to user events.
from tkinter import *
def DoClick(entries):
global ButCount
ButCount += 1
print("ButCount", ButCount, "TimeCount", TimeCount)
def newform(root):
L1 = Label(root, text = "test of 'after' method which seems to call before time")
L1.pack()
def CheckStatus():
global TimeCount
TimeCount += 1
print("In CheckStatus. ButCount", ButCount, "TimeCount", TimeCount)
# root.after(3000, DoTime())
root = Tk()
ButCount = 0
TimeCount = 0
if __name__ == '__main__':
FormData = newform(root)
root.bind('<Return>', (lambda event, e=FormData: fetch(e)))
b1 = Button(root, text='Click me', command=(lambda e=FormData: DoClick(e)))
b1.pack()
print("Before root.after(")
root.after(3000, CheckStatus())
print("Done root.after(")
root.mainloop()
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…