如何在Python Tkinter中创建线程进度条

时间:2017-06-20 20:39:33

标签: python multithreading python-3.x tkinter

我正在使用Python创建一个Tkinter GUI应用程序,该应用程序中的一个功能是ping不同的IP地址。我试图在ping过程中显示一个不确定的进度条,但我似乎无法使它工作。这是我尝试过的:

def pingSwitches(ipDictionary):
    validIPDictionary = {}
    switchCounter = 1
    popup = tk.Tk()
    popup.wm_title("Processing...")
    progress = ttk.Progressbar(popup, orient="horizontal", length=len(ipDictionary.keys()), mode="determinate")
    progress.pack()
    progress.start(50)
    popup.mainloop()

    for name, ip in ipDictionary.items():
        #progress.step(1)

        response = subprocess.Popen(["ping", "-n", "1", ip],stdout = subprocess.PIPE).communicate()[0]

        if((b"unreachable" in response) or (b"timed out" in response) or (b"demande" in response)):
            print(ip + " is down")
        else:
            validIPDictionary[name] = ip
            print(ip + " is up")

        print("Pinged " + str(switchCounter) + " of " + str(len(ipDictionary.keys())) + " total ips")
        switchCounter = switchCounter + 1



    progress.stop()
    popup.destroy()
    return validIPDictionary

问题是当我运行程序时,我可以看到代码在popup.mainloop()行停止并且不会继续。如果我在ping过程之后放置popup.mainloop(),则在ping完成后会出现弹出窗口,使其无效。

如何在我的代码ping时弹出窗口?它只是通过多线程吗?

0 个答案:

没有答案