Why ttk Progressbar appears after process in Tkinter

后端 未结 2 1430
暖寄归人
暖寄归人 2020-12-17 04:21

I want to create a large text upon Tkinter menu command and provide visual support by a progress bar. Although the progress bar is meant to start before the sub

2条回答
  •  甜味超标
    2020-12-17 05:03

    Here's another considerably simpler solution that doesn't require mixing Tkinter and multi-threading. To use it requires the ability to call the progressbar widget's update_idletasks() method multiple times during the time-consuming function.

    from Tkinter import *
    import ttk
    
    import time
    
    def foo(progressbar):
        progressbar.start()
        for _ in range(50):
            time.sleep(.1) # simulate some work
            progressbar.step(10)
            progressbar.update_idletasks()
        progressbar.stop()
    
    root = Tk()
    
    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)
    progressbar = ttk.Progressbar(mainframe, mode='indeterminate')
    progressbar.grid(column=1, row=100, sticky=W)
    
    ttk.Button(mainframe, text="Check",
               command=lambda:foo(progressbar)).grid(column=1, row=200, sticky=E)
    
    for child in mainframe.winfo_children():
        child.grid_configure(padx=5, pady=5)
    root.bind('', lambda event:foo(progressbar))
    
    root.mainloop()
    

提交回复
热议问题