user7386957
user7386957

Reputation: 1

tkinter after method use in Button

I wan to implement a function that when click the Button, then the related function will be done by period. How could I do? I write the code as follows. I suggest when click the button 'begin', then function of fetch will be done by period. I used while True to do this, but the function dead

def fetch(ent,t):    
    i=0    
    w3username=ent[0].get()+'\n'    
    info.append(w3username)    
    w3passwd=ent[1].get()+'\n'    
    info.append(w3passwd)    
    emailname=ent[2].get()+'\n'    
    info.append(emailname)    
    emailpasswd=ent[3].get()+'\n'    
    info.append(emailpasswd)  

    for i in info:    
        t.insert(END,i)

def makeframe(root,fields):    
    entry=[]    
    variables=[]   

    for field in fields:    
        row=Frame(root)    
        lab=Label(row,width=10,text=field)    
        ent=Entry(row)    
        row.pack(side=TOP,fill=X)    
        lab.pack(side=LEFT)    
        ent.pack(side=RIGHT,expand=YES,fill=X)    
        var=StringVar()    
        var.set('enter here')    
        ent.configure(textvariable=var)    
        variables.append(var)
    return variables


if __name__=='__main__':

    info=[]    
    root=Tk()    
    root.title('iCare tool')    
    fileds=['w3username','w3passwd','emailuser','emailpasswd']    
    entry=makeframe(root,fileds)    
    t=Text(root)    
    Button(root,text='begin',command=(lambda:fetch(entry,t))).pack(side=LEFT,expand=YES,fill=X)    
    t.pack(side=BOTTOM)

    root.mainloop()

I also use after method, but still not worked

b = Button(root, text='begin', command=(lambda:fetch(entry,t)))    
b.after(1000, fetch, entry, t)    
b.pack()

Upvotes: 0

Views: 995

Answers (1)

furas
furas

Reputation: 142671

after executes function only once. You have to use after inside fetch()

def fetch(ent, t):

    # ... your code ...

    # run again after 1000ms
    root.after(1000, fetch, ent, t)

Upvotes: 1

Related Questions