Hi All,
I have a wx.button, that executed something that takes in total 0.2 seconds. This includes a time.sleep(0.1). I don’t want that button to be pressed again before the previous button click action has not finished yet. However, even though the total execution time seems to be only 0.2 seconds. I printed it using the time module, it seems that I can only repressed the button after like 1 second. I do have other threads running in my wx.python program. When I completely remove the time.sleep, my button seems to be very responsive again. Can anyone explain to me what the problem is and how I can solve it ?
regards,
pluto mars
The call to time.sleep blocks wxPython main loop so it doesn’t appear to accept events while that is happening. Once the sleep ends, then the events start working again and your application becomes responsive.
···
On Friday, November 14, 2014 11:05:12 AM UTC-6, pluto mars wrote:
Hi All,
I have a wx.button, that executed something that takes in total 0.2 seconds. This includes a time.sleep(0.1). I don’t want that button to be pressed again before the previous button click action has not finished yet. However, even though the total execution time seems to be only 0.2 seconds. I printed it using the time module, it seems that I can only repressed the button after like 1 second. I do have other threads running in my wx.python program. When I completely remove the time.sleep, my button seems to be very responsive again. Can anyone explain to me what the problem is and how I can solve it ?
regards,
pluto mars
Hi,
generally, a gui app (including the ones written in wxpython) doesn't
work well with time.sleep(...) as this call block the event loop and
freezes the app until completition of sleep.
see some usual approaches e.g. in:
http://wiki.wxpython.org/LongRunningTasks
(some code might be a bit older, but the main ideas are still valid)
You can e.g. use wx.CallAfter(...) to call a function after some
specified amount of time; (time.sleep is used in some examples there
to simulate blocking function calls, which are treated in the
acompanied code).
You can call Disable() on a wx.Button widget and Enable() it again,
after completing the operation.
hth,
vbr
···
2014-11-14 18:05 GMT+01:00 pluto mars <plutomars955@gmail.com>:
Hi All,
I have a wx.button, that executed something that takes in total 0.2 seconds.
This includes a time.sleep(0.1). I don't want that button to be pressed
again before the previous button click action has not finished yet. However,
even though the total execution time seems to be only 0.2 seconds. I printed
it using the time module, it seems that I can only repressed the button
after like 1 second. I do have other threads running in my wx.python
program. When I completely remove the time.sleep, my button seems to be very
responsive again. Can anyone explain to me what the problem is and how I can
solve it ?
regards,
pluto mars
Hi,
I understand this problem, in case of a long running task. E.g when using a large time.sleep(). What I don’t understand is that the action itself took only 0.2 seconds. I have only a sleep time of 0.1 seconds. Why would such a small sleep time makes the response of the wx.button be like a second ? As I said, I use a print statement and the time module to check how long the action took. And it printed 0.2 seconds. So, why would it respond instead like as if the action took a second ? Is it because the other threads were also blocked and they needed also some time to execute something after the time.sleep returned ? Does it mean that I would always need to use wx.Callafter or a thread even if I have a sleep time that is very small to keep things responsive ? Is there a special way to sleep without having this slow response of the button ? NOte that I do want the user to be able to press again after the action has finished. I undestand I can use the wx.Callafter, but I find it more work to create a thread specially for this since the sleep time is very small.
Here is how I profiled how long the action took
def buttonClicked(self,event):
startTime = time.time()
actionPerformed()
print "execution took: ", time.time()-startTime, "[s]"
Thank you in advance for answering.
pluto mars
···
On Friday, November 14, 2014 7:04:35 PM UTC+1, vbr wrote:
2014-11-14 18:05 GMT+01:00 pluto mars plutom...@gmail.com:
Hi All,
I have a wx.button, that executed something that takes in total 0.2 seconds.
This includes a time.sleep(0.1). I don’t want that button to be pressed
again before the previous button click action has not finished yet. However,
even though the total execution time seems to be only 0.2 seconds. I printed
it using the time module, it seems that I can only repressed the button
after like 1 second. I do have other threads running in my wx.python
program. When I completely remove the time.sleep, my button seems to be very
responsive again. Can anyone explain to me what the problem is and how I can
solve it ?
regards,
pluto mars
Hi,
generally, a gui app (including the ones written in wxpython) doesn’t
work well with time.sleep(…) as this call block the event loop and
freezes the app until completition of sleep.
see some usual approaches e.g. in:
http://wiki.wxpython.org/LongRunningTasks
(some code might be a bit older, but the main ideas are still valid)
You can e.g. use wx.CallAfter(…) to call a function after some
specified amount of time; (time.sleep is used in some examples there
to simulate blocking function calls, which are treated in the
acompanied code).
You can call Disable() on a wx.Button widget and Enable() it again,
after completing the operation.
hth,
vbr
Please attach a small program that demonstrates your problem, something that we can just open on our computer.
···
On Saturday, November 15, 2014 1:26:27 PM UTC-8, pluto mars wrote:
Hi,
I understand this problem, in case of a long running task. E.g when using a large time.sleep(). What I don’t understand is that the action itself took only 0.2 seconds. I have only a sleep time of 0.1 seconds. Why would such a small sleep time makes the response of the wx.button be like a second ?