Load after initialising

I've got a few things I need to fetch out of a database, the information only
needs to be pulled out once. The data is needed when the user presses some
buttons.

I would just put this fetching in the init of the frame, but I'd rather not delay
the startup of the frame, and likewise I could put it in the button event but I'd
rather not slow down the event from the button with database queries.

At the moment I've got it to load only on the first time the user presses the
button, after that its stored and on the next button press the data isn't fetched
again.

Is there a way I could fetch the data straight after the frame initialises? So
nothing is slowed down?

I expect its quite simple to do but I'm quite simple too :slight_smile:

Cheers!
Paul

Bind to a EVT_IDLE in __init__

http://www.wxpython.org/docs/api/wx.IdleEvent-class.html

then unbind when you are done fetching the data.

The buttons utilizing the data could be disabled until the data is ready just in case (enable them when you unbind from EVT_IDLE).

Toni

···

On Wed, 09 Nov 2011 13:23:37 +0100, Paul <poalman@gmail.com> wrote:

I've got a few things I need to fetch out of a database, the information only
needs to be pulled out once. The data is needed when the user presses some
buttons.

I would just put this fetching in the init of the frame, but I'd rather not delay
the startup of the frame, and likewise I could put it in the button event but I'd
rather not slow down the event from the button with database queries.

At the moment I've got it to load only on the first time the user presses the
button, after that its stored and on the next button press the data isn't fetched
again.

Is there a way I could fetch the data straight after the frame initialises? So
nothing is slowed down?

I expect its quite simple to do but I'm quite simple too :slight_smile:

Is there a way I could fetch the data straight after the frame
initialises? So
nothing is slowed down?

I expect its quite simple to do but I'm quite simple too :slight_smile:

Bind to a EVT_IDLE in __init__

wxPython API Documentation — wxPython Phoenix 4.2.2 documentation

then unbind when you are done fetching the data.

another option is wx.CallAfter() -- put the call in at the end of your __init__. I think this will accomplish the same thing as EVT_IDLE, but it's a bit simpler.

>> So nothing is slowed down?

well, "nothing" is a tall order. If the DB lookup does take a while, you could put it in a separate thread, then have it call a method on your frame (with wx.CallAfter) when it's done -- that would keep it from locking up the GUI at all.

The buttons utilizing the data could be disabled until the data is ready
just in case (enable them when you unbind from EVT_IDLE).

or when the above referenced method is called.

However, I dislike disabled buttons -- often the user is not sure why it's disabled. So I'd be inclined to have a check for the existence of the data when the button is pressed, and suitable message returned to the user if it's not there yet.

-Chris

···

On 11/9/11 4:38 AM, Toni Ru�a wrote:

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

another option is wx.CallAfter() -- put the call in at the end of your
__init__. I think this will accomplish the same thing as EVT_IDLE, but
it's a bit simpler.

>> So nothing is slowed down?

well, "nothing" is a tall order. If the DB lookup does take a while, you
could put it in a separate thread, then have it call a method on your
frame (with wx.CallAfter) when it's done -- that would keep it from
locking up the GUI at all.

> The buttons utilizing the data could be disabled until the data is ready
> just in case (enable them when you unbind from EVT_IDLE).

or when the above referenced method is called.

However, I dislike disabled buttons -- often the user is not sure why
it's disabled. So I'd be inclined to have a check for the existence of
the data when the button is pressed, and suitable message returned to
the user if it's not there yet.

I'm not keen on disabling the buttons either. If the button is pressed before
the data has been retrieved I guess the best solution would be to join the
thread and wait for it to finish as you can't do anything until that data comes
back. I'm not too great with threads, would I simple saved a reference to the
thread and call join? would this return to the code once the thread finishes,
and what if the thread has finished running already?

···

-Chris