显示 ttk 进度条 (python) 而 运行 一个函数
Show ttk progressbar (python) while running a function
我的代码有问题。我尝试做的是在函数 ToDo()
为 运行 时显示 ttk 不确定进度条。我一直在互联网上搜索,我发现了一些帖子,但在这些帖子中,人们通过 tk 按钮启动他们的功能,而我的目标是从 tk 外部启动它 window。这是我的尝试,它运行我的函数 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 循环的函数,它将 运行 用于您所需的时间并更新 window:
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()
我的代码有问题。我尝试做的是在函数 ToDo()
为 运行 时显示 ttk 不确定进度条。我一直在互联网上搜索,我发现了一些帖子,但在这些帖子中,人们通过 tk 按钮启动他们的功能,而我的目标是从 tk 外部启动它 window。这是我的尝试,它运行我的函数 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 循环的函数,它将 运行 用于您所需的时间并更新 window:
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()