如何添加tkinter进度栏不确定功能
from tkinter import *
from tkinter import ttk
def DOTEST_GUI():
GUI = Tk()
w = 1920
h = 1080
ws = GUI.winfo_screenwidth()
hs = GUI.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
GUI.geometry(f'{w}x{h}+{x:.0f}+{y:.0f}')
def start_p():
progress.start(5)
def stop_P():
progress.stop()
def print_cf(event = None):
import time
print('s')
start_p()
time.sleep(5)
stop_P()
B_TEST = ttk.Button(GUI, text = "test", width = 15, command = print_cf)
B_TEST.pack()
progress = ttk.Progressbar(GUI, orient = HORIZONTAL, length = 100, mode = 'indeterminate')
progress.pack(pady = 10)
GUI.bind("<Return>", print_cf)
GUI.focus()
GUI.mainloop()
DOTEST_GUI()
遵循此代码进度栏无法正常运行。
我尝试删除stop_p()
,它在time.sleep(5)
的5秒后工作。
我希望它开始运行5秒钟,直到stop_p()
代码。
from tkinter import *
from tkinter import ttk
def DOTEST_GUI():
GUI = Tk()
w = 1920
h = 1080
ws = GUI.winfo_screenwidth()
hs = GUI.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
GUI.geometry(f'{w}x{h}+{x:.0f}+{y:.0f}')
def start_p():
progress.start(5)
def stop_P():
progress.stop()
def print_cf(event = None):
import time
print('s')
start_p()
time.sleep(5)
stop_P()
B_TEST = ttk.Button(GUI, text = "test", width = 15, command = print_cf)
B_TEST.pack()
progress = ttk.Progressbar(GUI, orient = HORIZONTAL, length = 100, mode = 'indeterminate')
progress.pack(pady = 10)
GUI.bind("<Return>", print_cf)
GUI.focus()
GUI.mainloop()
DOTEST_GUI()
follow this code progress bar is not running properly.
I tried to remove stop_P()
, it's work after 5 second of time.sleep(5)
.
I would like it to start running progress 5 second until the stop_P()
code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要在一定的时间内运行
progressbar
,然后停止它,则可以执行这样的操作。合并print_cf
函数以启动并停止进度,并设置5个范围,在两者之间睡1秒,然后停止progressbar
。将其放在线程上可以让您做一些比睡一秒钟并打印一些东西要耗时的事情。If you want to run a
Progressbar
for a set amount of time and then stop it, you could do something like this. Consolidate theprint_cf
function to start and stop the progress and set a range of 5, sleep for 1 second in between and then stop theProgressbar
. Placing it on a thread would allow you to do something more time consuming than sleeping one second and printing something.