tkinter gui with progress bar

后端 未结 2 1123
抹茶落季
抹茶落季 2020-12-30 06:51

I have a simple TK gui and a long process in a function attached to a button and I want a progress bar when I click on the button. I want a progress bar went i click on the

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-30 07:16

    For all the GUI elements to modify themselves (in your case, for the progress bar to move) the execution must hit app.mainloop().

    In your case, def traitement(self): starts and then stops the progressbar without hitting the mainloop, so it fails to visibly reflect the intended progressbar movement on the GUI. The catch here is, when the execution hits mainloop, progressbar is configured to 'stop' state.

    Hence, it is a good idea to execute time consuming activities on a different Thread as shown by @xmcp

    However, if you DO NOT want to use threading, you can use the after method to achieve what you want:

    def stop_progressbar(self):
        self.progress.stop()
    
    def traitement(self):
        self.progress.grid()
        self.progress.start()
        self.after(15000, self.stop_progressbar) 
        ## Call Just like you have many, many code lines...
    

    The above code used self.after() method which will execute the stop_progressbar method to stop after 15 seconds, instead of time.sleep() which blocks the mainthread.

提交回复
热议问题