如何将TQDM从终端显示到GUI进度栏中?

问题描述

你好,我是python的新手,我正在为一个大学项目设置进度条。目前,当循环正在运行时,我会在终端上使用tqdm在后台运行进度条,并在循环完成后在主GUI主体中返回结果。我试图将从终端到主GUI主体的进度显示为进度条。任何帮助将不胜感激。

这是代码的一部分:

    keywordlist = open('IP.txt','r')
    loglines = [n for n in logfile]
    keywords = [n for n in keywordlist]
    for line in tqdm.tqdm(loglines):
        progress.update()
        progress['value'] = 100
        time.sleep(0.01)
        for word in keywords:
            if word.strip() in line.strip():
                processes.append(line)
                with open("DNSResults.txt","w") as txt_file:
                    for line in processes:
                        txt_file.write("".join(line)+"\n")
    logfile = open('DNSResults.txt','r')
    keywordlist = open('EmotetIP.txt','r')
    loglines = [n for n in logfile]
    keywords = [n for n in keywordlist]
    
    for line in loglines:
        for word in keywords:
            if word.strip() in line.strip():    
                msg3 ="Matches found with the following Ip Adresses: " + str(word) +"\n"
                txt.insert(0.0,msg3)```

然后我将tkinter进度栏用于GUI。

progress = ttk.Progressbar(root,orient = HORIZONTAL,length = 100,mode = 'determinate')
progress.pack(side=BottOM,fill=X)

解决方法

使用tqdm.gui。

from tqdm.gui import trange,tqdm
for i in trange(<some amount or something>):
     pass
from tqdm.gui import tqdm_gui
for i in tqdm_gui(range(<some amount or something>)):
     pass