python tkinter progressbar_Tkinter:ProgressBar具有不确定的持续时间

我想在Tkinter中实现一个满足以下要求的进度条:

>进度栏是主窗口中的唯一元素

>可以通过启动命令来启动,而无需按下任何按钮

>可以等待,直到持续时间未知的任务完成

>只要任务未完成,进度条的指示灯就会不断移动

>可以通过停止命令将其关闭,而无需按下任何停止杆

到目前为止,我有以下代码:

import Tkinter

import ttk

import time

def task(root):

root.mainloop()

root = Tkinter.Tk()

ft = ttk.Frame()

ft.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)

pb_hD = ttk.Progressbar(ft, orient='horizontal', mode='indeterminate')

pb_hD.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)

pb_hD.start(50)

root.after(0,task(root))

time.sleep(5) # to be replaced by process of unknown duration

root.destroy()

在这里,问题在于进度条在5秒结束后不会停止.

有人可以帮我发现错误吗?

解决方法:

一旦激活了mainloop,脚本将不会移动到下一行,直到根被销毁为止.

可能还有其他方法可以执行此操作,但我更喜欢使用线程来执行此操作.

像这样

import Tkinter

import ttk

import time

import threading

#Define your Progress Bar function,

def task(root):

ft = ttk.Frame()

ft.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)

pb_hD = ttk.Progressbar(ft, orient='horizontal', mode='indeterminate')

pb_hD.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)

pb_hD.start(50)

root.mainloop()

# Define the process of unknown duration with root as one of the input And once done, add root.quit() at the end.

def process_of_unknown_duration(root):

time.sleep(5)

print 'Done'

root.destroy()

# Now define our Main Functions, which will first define root, then call for call for "task(root)" --- that's your progressbar, and then call for thread1 simultaneously which will execute your process_of_unknown_duration and at the end destroy/quit the root.

def Main():

root = Tkinter.Tk()

t1=threading.Thread(target=process_of_unknown_duration, args=(root,))

t1.start()

task(root) # This will block while the mainloop runs

t1.join()

#Now just run the functions by calling our Main() function,

if __name__ == '__main__':

Main()

让我知道是否有帮助.

标签:python-2-7,tkinter,progress-bar,ttk,python

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值