如何启动ttk.Progressbar?

时间:2011-06-02 09:50:05

标签: python debugging progress-bar ttk

我无法启动进度条。我已经在互联网上搜索了答案,并且已经尝试了几个小时的多种方式,但是已经得到了错误,例如:

TypeError: unbound method start() must be called with Progressbar instance as first argument (got nothing instead)

TypeError: unbound method start() must be called with Progressbar instance as first argument (got NoneType instance instead)

AttributeError: 'NoneType' object has no attribute 'stop'

这是(基本上)我的代码:

from Tkinter import *
import ttk

def foo():
    #make progressbar start here
    do_stuff()
    #make progressbar end here


root = Tk()
root.title("foo")

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
prog = ttk.Progressbar(mainframe, mode='indeterminate').grid(column=1, row=100, sticky=W)

ttk.Button(mainframe, text="Check", command=foo).grid(column=1, row=100, sticky=E)

for child in mainframe.winfo_children():
    child.grid_configure(padx=5, pady=5)
root.bind('<Return>', check)

root.mainloop()

1 个答案:

答案 0 :(得分:2)

您的prog变量不包含进度条,因为您调用的网格方法确实返回None。这确实解释了

AttributeError: 'NoneType' object has no attribute 'stop'

将您的代码更改为

prog = ttk.Progressbar(mainframe, mode='indeterminate')
prog.grid(column=1, row=100, sticky=W)

之后你可以通过

在foo中启动Progressbar
prog.start()