当前位置: 代码迷 >> python >> 在运行函数时显示ttk进度栏(python)
  详细解决方案

在运行函数时显示ttk进度栏(python)

热度:70   发布时间:2023-07-16 10:14:46.0

我的代码有麻烦。 我想做的是在函数ToDo()运行时显示ttk不确定进度条。 我一直在互联网上搜索,发现了一些帖子,但其中的人们是从tk按钮启动其功能的,而我的目标是从tk窗口外部启动该功能。 这是我的尝试,它运行我的函数ToDo()但不显示进度ToDo() 我需要使用threading吗? 如果是这样,我应该如何使用它? 我是python的新手...

try:
    import tkinter as tk
    from tkinter import ttk
except ImportError:
    import Tkinter as tk
    import ttk
import time

def ToDo():
    time.sleep(1)
    print("1 sec")
    time.sleep(1)
    print("2 sec")
    time.sleep(1)
    print("3 sec")
    time.sleep(1)
    print("4 sec")
    time.sleep(1)
    print("5 sec")
    global variable_check_final
    variable_check_final = True


class Application(tk.Frame):
    def __init__(self, main_window):
        # super().__init__(main_window)
        tk.Frame.__init__(self)
        main_window.title("Please wait")

        self.progressbar = ttk.Progressbar(self, mode="indeterminate")
        self.progressbar.pack()
        self.progressbar.place(x=30, y=60, width=200)
        self.place(width=200, height=200)
        main_window.geometry("300x200")
        self.progressbar.start()
        self.checkfinal()
        ToDo()


    def checkfinal(self):
        if variable_check_final == True:
            self.progressbar.stop()
            main_window.destroy()
        else:
            print("not yet")
            self.after(1000, self.checkfinal)



if __name__ == "__main__":
    print("started")
    global variable_check_final
    variable_check_final = False
    main_window = tk.Tk()
    app = Application(main_window)
    app.mainloop()

sleep将在所需时间内停止整个程序,并且不允许您更新进度栏。 因此,制作一个包含while循环的函数,该函数将在您需要的时间运行并更新窗口:

def Sleep(t, func):
    t1 = time.time()
    while time.time()-t1 < t:
        func()

t是时间, func是您将提供的输入功能,它将运行。 要更新,您可以使用:

Sleep(1, main_window.update)

这是完整的工作代码:

try:
    import tkinter as tk
    from tkinter import ttk
except ImportError:
    import Tkinter as tk
    import ttk
import time

def Sleep(t, func):
    t1 = time.time()
    while time.time()-t1 < t:
        func()
def ToDo():
    Sleep(1, main_window.update)
    print("1 sec")
    Sleep(1, main_window.update)
    print("2 sec")
    Sleep(1, main_window.update)
    print("3 sec")
    Sleep(1, main_window.update)
    print("4 sec")
    Sleep(1, main_window.update)
    print("5 sec")
    global variable_check_final
    variable_check_final = True


class Application(tk.Frame):
    def __init__(self, main_window):
        # super().__init__(main_window)
        tk.Frame.__init__(self)
        main_window.title("Please wait")
        self.progressbar = ttk.Progressbar(self, mode="indeterminate")
        self.progressbar.place(x=30, y=60, width=200)
        self.place(width=200, height=200)
        main_window.update()
        main_window.geometry("300x200")
        self.progressbar.start()
        self.checkfinal()
        ToDo()


    def checkfinal(self):
        if variable_check_final == True:
            self.progressbar.stop()
            main_window.destroy()
        else:
            print("not yet")
            self.after(1000, self.checkfinal)



if __name__ == "__main__":
    print("started")
    global variable_check_final
    variable_check_final = False
    main_window = tk.Tk()
    app = Application(main_window)
    app.mainloop()