1

I have my process working with Tkinter, but when I run my code my program freeze I understandt It´s because of the main loop, I have been reading the documentation of Tkinter and reading some other questions but I can´t really understand how to implement the Progress Bar to my process, my code It´s really simple it just dowload some information and then save It like an excel but It takes some time to do the process, so how can I implement the progress bar here.

root=Tk()
root.title('Info Dowload')
root.iconbitmap('Logo.ico')
root['bg'] = '#E1FAF9'
#VariablesInicio
fecha1=Entry(root)
fecha2=Entry(root)
date1.insert(0, "ejm:2022-03-15")
date2.insert(0, "ejm:2022-03-15")
#Label
label1=Label(root,text="First Date(aaaa-mm-dd):",font=('Bahnschrift',11))
label2=Label(root,text="Second date(aaaa-mm-dd):",font=('Bahnschrift',11))

def Click():
    
    boton1.config(state= "disable")
    label3=Label(root,text="Working with: "+date1.get())
    label4=Label(root,text="Working with: "+date2.get())
    label3.grid(row=3,column=0)
    label4.grid(row=4,column=0)
    startFac=str(date1.get())+' 00:00:00' 
    endFac=str(date2.get())+' 23:59:59'
    
    ##First Query
    startMF=str(date1.get()) 
    endMF=str(date2.get())
    startMF=startMF.replace('-','/') 
    endMF=endMF.replace('-','/') 
   
    df=query1(startMF, endMF)
    df1=query2(startFac, endFac, df)
    sales=pd.merge(left=df,right=df1,how='left',on=['code1','code2','code3'])    
    sales.to_excel('sales.xlsx',index=None)
    

#Button
import tkinter as tk   
boton1=tk.Button(root,text='Ejecutar',bg='#20bebe',fg='white',height=1,width=6,command=Click)
    #Print info
    label1.grid(row=0,column=0)
    label2.grid(row=1,column=0)
    fecha1.grid(row=0,column=1)
    fecha2.grid(row=1,column=1)
    boton1.grid(row=2,column=0)
    root.mainloop()
0

1 Answer 1

2

In order to add a progress bar, you can use the Progressbar class as follows:

progressbar = Progressbar(root, orient='horizontal',mode='indeterminate',length=<your preferred length>)

Additionally, you would also have to define when to start and stop displaying your progress bar. In order to do this, add the pb.start() and pb.stop() commands at the start and end of your click() function respectively.

[Suggested by @Matiss] In order to solve the problem of the progress bar not moving, you can import threading module and use it as follows:

root=Tk()
root.title('Info Dowload')
root.iconbitmap('Logo.ico')
root['bg'] = '#E1FAF9'
#VariablesInicio
fecha1=Entry(root)
fecha2=Entry(root)
date1.insert(0, "ejm:2022-03-15")
date2.insert(0, "ejm:2022-03-15")
#Label
label1=Label(root,text="First Date(aaaa-mm-dd):",font =('Bahnschrift',11))
label2=Label(root,text="Second date(aaaa-mm-dd):",font =('Bahnschrift',11))

def Click():
    boton1.config(state= "disable")
    label3=Label(root,text="Working with: "+date1.get())
    label4=Label(root,text="Working with: "+date2.get())
    label3.grid(row=3,column=0)
    label4.grid(row=4,column=0)
    progressbar.start()
    t1.start()
    
def download():
    
    startFac=str(date1.get())+' 00:00:00' 
    endFac=str(date2.get())+' 23:59:59'

    ##First Query
    startMF=str(date1.get()) 
    endMF=str(date2.get())
    startMF=startMF.replace('-','/') 
    endMF=endMF.replace('-','/') 

    df=query1(startMF, endMF)
    df1=query2(startFac, endFac, df)
    sales=pd.merge(left=df,right=df1,how='left',on=['code1','code2','code3'])    
    sales.to_excel('sales.xlsx',index=None)
    progressbar.stop()

#Code for multithreading
t1 = threading.Thread(target=download)    

#Code for progress bar
progressbar = Progressbar(root, orient='horizontal',mode='indeterminate',length=<your preferred length>)
progressbar.grid()
Sign up to request clarification or add additional context in comments.

6 Comments

I just put it on my code and when I run It, It´s just display the ProgressBar but It won´t move until the program It´s over.
Is it possible for you to share your whole code so that I can try running it on my computer to see what you exactly mean? If you mean that you want to hide the progress bar, then you could use grid_forget() command
They want to run two processes at the same time which means either using threading (mostly this) module or multiprocessing module, then putting the downloading part in that separate "place" and sending info from there to tkinter's "place" and updating the widgets there, in this case it is less complex due to the "indeterminate" "flag"
Yeah, exactly what Matiis said, the code I´m using is the one you see the only difference is that those queries functions are SQL Queries.
PaulCoder Let me know whether the above code works! Thanks for the suggestion @Matiiss
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.