Open a progress bar until function task ends

I have an event listener on left double click inside a wx.Grid that opens a dialog box to confirm the change. If the user clicks on “Yes” I call a function that takes around 6 seconds to execute, meanwhile the window freezes. What I want to do is to open a progress bar so the user can wait until the function finishes its task, or better, have the progress bar in the dialog box.

I have no idea where to start as I’ve never needed a progress bar until now. A lot of the solutions I’ve looked at suggest threading, but I’m pretty inexperienced with threads in Python.

I’m hoping someone will be able to give me a hand displaying the progress for a running task using wxPython.

Here is my code so far:

def OnCellLeftDClick(self, evt):
    if evt.GetCol() == 17:
        dlg = wx.MessageDialog(None, "Do you want to change " + self.GetCellValue(evt.GetRow(), 1) + " bid?",'Updater',wx.YES_NO | wx.ICON_QUESTION)
        result = dlg.ShowModal()
        if result == wx.ID_YES:
            from chanbeBid import changeBidTb
changeBidTb(self.GetCellValue(evt.GetRow(), 1), self.GetCellValue(evt.GetRow(), 16))

    evt.Skip()

Thank you,

``

``

If your long running task is able to be broken into very small chunks, and you can call wx.Yield between those chunks, then keeping it all in the main thread is possible. The key point is you don’t want to block the event loop (which makes the application appear frozen.) wx.Yield gives the event loop a chance to run until there are no more pending events. Care needs to be taken with wx.Yield to not get into a recursive situation where some event triggers the task to be run again before the first one is done. Because of this and the chunking it’s often simpler to use a thread.

Either way, there is a wx.ProgressDialog class that can help with displaying the progress bar and any messages you want to display along the way.

This page is old but still helpful: https://wiki.wxpython.org/LongRunningTasks

···

On Monday, May 20, 2019 at 9:13:05 PM UTC-7, Youn-Bo wrote:

I have an event listener on left double click inside a wx.Grid that opens a dialog box to confirm the change. If the user clicks on “Yes” I call a function that takes around 6 seconds to execute, meanwhile the window freezes. What I want to do is to open a progress bar so the user can wait until the function finishes its task, or better, have the progress bar in the dialog box.

I have no idea where to start as I’ve never needed a progress bar until now. A lot of the solutions I’ve looked at suggest threading, but I’m pretty inexperienced with threads in Python.

I’m hoping someone will be able to give me a hand displaying the progress for a running task using wxPython.

Robin

First, thank you for your answer. I tried injecting wx.Yield between small chunks of code in my function but that doesn’t seem to work. No difference in execution time and the main program freezes while the function is executing. I don’t know what to think, but maybe I’m missing something.

For the wx.ProgressDialog class, as I said in my question, I’m new to threading and I don’t understand what it’s all about. I tried learning but the language used to explain it is not part of my dictionary. I’ve also tried to re-use some existing scripts but I was unable to make them work with mine. This is very frustrating I confess :frowning:

···

On Tuesday, 21 May 2019 14:29:44 UTC-4, Robin Dunn wrote:

On Monday, May 20, 2019 at 9:13:05 PM UTC-7, Youn-Bo wrote:

I have an event listener on left double click inside a wx.Grid that opens a dialog box to confirm the change. If the user clicks on “Yes” I call a function that takes around 6 seconds to execute, meanwhile the window freezes. What I want to do is to open a progress bar so the user can wait until the function finishes its task, or better, have the progress bar in the dialog box.

I have no idea where to start as I’ve never needed a progress bar until now. A lot of the solutions I’ve looked at suggest threading, but I’m pretty inexperienced with threads in Python.

I’m hoping someone will be able to give me a hand displaying the progress for a running task using wxPython.

If your long running task is able to be broken into very small chunks, and you can call wx.Yield between those chunks, then keeping it all in the main thread is possible. The key point is you don’t want to block the event loop (which makes the application appear frozen.) wx.Yield gives the event loop a chance to run until there are no more pending events. Care needs to be taken with wx.Yield to not get into a recursive situation where some event triggers the task to be run again before the first one is done. Because of this and the chunking it’s often simpler to use a thread.

Either way, there is a wx.ProgressDialog class that can help with displaying the progress bar and any messages you want to display along the way.

This page is old but still helpful: https://wiki.wxpython.org/LongRunningTasks

Robin

I understand that a lot about threading is confusing for newbies, it was for me too. I think part of the problem is that tutorials tend to very quickly move from simple to advanced topics, like the various synchronization and locking schemes and thread safety, without giving the learner time to digest the simple things. However, if you just need to do something simple with little or no interaction with other threads or shared data, then threading can be rather simple too.

Using simple threading in a wxPython application has just one (minor!) complicating factor, which is that all interaction with GUI elements must be done in the main thread, but there are simple ways to initiate that interaction from other threads in a safe manner. For simple threading tasks, wx.CallAfter is likely to be all you need to communicate from a worker thread to the GUI thread until you need to move on to more advanced things.

Here are a couple searches to help you learn more and find examples:

https://www.google.com/search?q=wxpython+threading

https://www.google.com/search?q=python+threading+tutorial+beginners

https://stackoverflow.com/search?q=wxpython+threading

···

On Tuesday, May 21, 2019 at 3:03:32 PM UTC-7, Youn-Bo wrote:

For the wx.ProgressDialog class, as I said in my question, I’m new to threading and I don’t understand what it’s all about. I tried learning but the language used to explain it is not part of my dictionary. I’ve also tried to re-use some existing scripts but I was unable to make them work with mine. This is very frustrating I confess :frowning:

Robin