how to execute code in MainLoop ?

hello,

I want to do a simulation in a graphical wx-application,
and the result of that simulation is continuously shown in the graphical window.

I suppose I've to put that simulation code in to the MainLoop,
how do I have to do that ?

thanks,
Stef Mientki

Stef Mientki wrote:

hello,

I want to do a simulation in a graphical wx-application,
and the result of that simulation is continuously shown in the graphical window.

I suppose I've to put that simulation code in to the MainLoop,
how do I have to do that ?

There is an example of replacing the MainLoop in samples/mainloop, but I would bet that you don't need to do that. How fast is "continuously shown"? If it's more than 20 or 30 frames per second then you are wasting your time because the human eye can't distinguish more than that. (15 fps for old farts like me.) At that rate using a timer or shifting your simulation code to idle events might be good enough, or running it in another thread and then just updating the display in the main thread from a timer would be another approach. See LongRunningTasks - wxPyWiki

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

Stef Mientki wrote:

hello,

I want to do a simulation in a graphical wx-application,
and the result of that simulation is continuously shown in the graphical window.

I suppose I've to put that simulation code in to the MainLoop,
how do I have to do that ?

There is an example of replacing the MainLoop in samples/mainloop, but I would bet that you don't need to do that.

thanks Robin, that's what I'm looking for.

How fast is "continuously shown"? If it's more than 20 or 30 frames per second then you are wasting your time because the human eye can't distinguish more than that. (15 fps for old farts like me.) At that rate using a timer or shifting your simulation code to idle events might be good enough, or running it in another thread and then just updating the display in the main thread from a timer would be another approach. See LongRunningTasks - wxPyWiki

You're right, 2 times a second is fast enough for me (I might even be older ;-).
But my calculations (certainly if my model gets detailed), are very very slow in Python,
so I need all the time I can get for calculations.
I assume that a separate thread will perform better,
but as I've just started with Python, I'll try to leave threads for later on.

cheers,
Stef Mientki

I've been doing some OpenGL stuff in Python and wxPython, and have been
able to get around 60 frames/second without serious issue in Windows
(using a wx.Timer, adjusting sleep times depending on the current sleep
time and the actual framerate). If I don't use wx.Timers, I can get
much higher rates, but that says as much about timer resolution in
Windows than anything else.

- Josiah

···

Robin Dunn <robin@alldunn.com> wrote:

Stef Mientki wrote:
> hello,
>
> I want to do a simulation in a graphical wx-application,
> and the result of that simulation is continuously shown in the graphical
> window.
>
> I suppose I've to put that simulation code in to the MainLoop,
> how do I have to do that ?

There is an example of replacing the MainLoop in samples/mainloop, but I
would bet that you don't need to do that. How fast is "continuously
shown"? If it's more than 20 or 30 frames per second then you are
wasting your time because the human eye can't distinguish more than
that. (15 fps for old farts like me.) At that rate using a timer or
shifting your simulation code to idle events might be good enough, or
running it in another thread and then just updating the display in the
main thread from a timer would be another approach. See
LongRunningTasks - wxPyWiki

Stef Mientki wrote:

You're right, 2 times a second is fast enough for me (I might even be older ;-).
But my calculations (certainly if my model gets detailed), are very very slow in Python,
so I need all the time I can get for calculations.

If you can do your calculations one time step at a time, It'd be easy to have a loop like:

while TerminationCritera:
     Model.CalcNextTimestep
     Visualization.Draw()

and there you go.

You might want a wx.App.Yield() in there too, so that someone could push a "Stop" button or something.

By the way, I don't what you want to visualize, but there is a good chance that wx.lib.floatcanvas could help with that part.

-Chris

···

--
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

Christopher Barker wrote:

Stef Mientki wrote:

You're right, 2 times a second is fast enough for me (I might even be older ;-).
But my calculations (certainly if my model gets detailed), are very very slow in Python,
so I need all the time I can get for calculations.

If you can do your calculations one time step at a time, It'd be easy to have a loop like:

while TerminationCritera:
    Model.CalcNextTimestep
    Visualization.Draw()

Yes, I think that's the way I'll go

and there you go.

You might want a wx.App.Yield() in there too, so that someone could push a "Stop" button or something.

I read it's very dangeruous

By the way, I don't what you want to visualize, but there is a good chance that wx.lib.floatcanvas could help with that part.

At first I didn't want to visualize anything,
but as this my first change to use a Python GUI
(until now I always do my GUI in Delphi, which is much easier ;-),
I'm thinking of: LEDs turning on and off, LCD-displays etc.
Besides that I want to use a editor component, where the actual executed code line is highlighted.

I've chozen OGL, after looking at the wxPython demos (really beautiful demo !!),
and for as far as I can judge it's quit similar to FLOATCANVAS.
Anyway thanks for the tip.

cheers,
Stef Mientki

-Chris

Kamer van Koophandel - handelsregister 41055629 / Netherlands Chamber of Commerce - trade register 41055629

Christopher Barker wrote:

You might want a wx.App.Yield() in there too, so that someone could push a "Stop" button or something.

Better yet, call:

wx.YieldIfNeeded()

or

wx.GetApp().Yield(True)

(they are the same thing)

-CHB

···

--
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