How to start ttk.Progressbar?

☆樱花仙子☆ 提交于 2020-01-04 14:19:10

问题


I am having trouble with getting the progressbar to start. I have scoured the internet for answers, and have tried multiple ways for hours, but have been rewarded with errors such as:

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'

Here is (essentially) my code:

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:


Your prog variable does not contain the progressbar because you call the grid method which does return None. That does explain the

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

change your code to

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

after that you can start the Progressbar in foo via

prog.start()


来源:https://stackoverflow.com/questions/6212896/how-to-start-ttk-progressbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!