gpt4 book ai didi

python - 运行函数时显示 ttk 进度条(python)

转载 作者:太空宇宙 更新时间:2023-11-03 21:43:52 25 4
gpt4 key购买 nike

我的代码遇到问题。我尝试做的是在函数 ToDo() 运行时显示 ttk 不确定的进度条。我一直在互联网上搜索,发现了一些帖子,但在这些帖子中,人们从 tk 按钮启动其功能,而我的目标是从 tk 窗口外部启动它。这是我的尝试,它运行我的函数 ToDo() 但不显示进度条。我需要使用线程吗?如果是这样,我应该如何使用它?我是 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()

关于python - 运行函数时显示 ttk 进度条(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52648384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com