简体   繁体   English

使用 tkinter python 向我的进程添加进度条

[英]Adding a progress bar to my process with tkinter python

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.我的进程使用 Tkinter,但是当我运行我的代码时,我的程序冻结了在我的流程中实现进度条,我的代码非常简单,只需下载一些信息,然后像 excel 一样保存它,但是完成这个过程需要一些时间,所以我如何在这里实现进度条。

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

In order to add a progress bar, you can use the Progressbar class as follows:为了添加进度条,您可以使用 Progressbar class 如下:

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.为此,请分别在click() function 的开头和结尾添加pb.start()pb.stop()命令。

[ 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: [ @Matiss 建议] 为了解决进度条不动的问题,可以import threading模块,使用方法如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM