mega noob to gui stuff

Hi guys...

I've been doing python for about a year now and have never needed a
gui until now... I've been reading through tutorials on here but don't
fully understand where the main code goes? lol

I made a frame, but im confused as how to slot in my existing script
and update the text box with new values rather than print to screen...
heres pretty much all my script is :

mins=0

while True:

time.sleep(60)
mins+=1
print str(mins) + ' minutes have passed...'

(well the actual script is about 700 lines but thats the basic idea)

I really can't understand where the app.MainLoop() goes off too...
sorry that this is such a noob question just can't seem to find the
answer!!

Cheers,

hayate wrote:

I've been doing python for about a year now and have never needed a
gui until now... I've been reading through tutorials on here but don't
fully understand where the main code goes? lol

I made a frame, but im confused as how to slot in my existing script
and update the text box with new values rather than print to screen...
...
I really can't understand where the app.MainLoop() goes off too...
sorry that this is such a noob question just can't seem to find the
answer!!

Yes, this is really the big philosophical leap you have to make when you
switch to a GUI. Instead of YOU being in control, you give over control
to the windowing system. Everything you do is then in response to
events. You can't use time.sleep, because while you are sleeping, your
thread is tied up so you can't process events. So, instead of that, you
use a wx.Timer. That fires an event periodically, and the code you have
above would go in the EVT_TIMER handler.

So, you create all your frames and windows and controls, but none of
that actually causes anything to get done. It just queues up window
messages that need to be handled. The work doesn't get done until you
call app.MainLoop, at which point you start handling all of the window
messages that have accrued. app.MainLoop will not return until the main
window closes and the application is supposed to exit.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Have you looked at this page?:

http://wiki.wxpython.org/Getting%20Started

There is a “Hello, World” example there, and then larger examples.

Basically, all wxPython scripts will have essentially the same thing at the bottom, which is lines 4-7 of the “Hello, World” example. Your code that does various manipulations of the data will be in other functions in the script. So, for example, you can:

  1. Have a button on the GUI and have it bound to an “event handler” function.

  2. When the user pressed the button, the event handler function is called.

  3. That function can then grab the text from a text box (using textbox.GetValue() ) and do something based on it.

  4. Etc.

Does this help get you on the right track?

Che

···

On Tue, Nov 22, 2011 at 4:09 PM, hayate hayate.mugen.tenshin@gmail.com wrote:

Hi guys…

I’ve been doing python for about a year now and have never needed a

gui until now… I’ve been reading through tutorials on here but don’t

fully understand where the main code goes? lol

Hi Both,

Thanks alot guys I had looked at the hello world example but hadn't
grasped the "the big philosophical leap" as Tim put it... Instead of
me being in control my code is run based on events that happen within
the gui whether its button clicks or timers! Seems so simple now :slight_smile:

···

On Nov 22, 11:13 pm, C M <cmpyt...@gmail.com> wrote:

On Tue, Nov 22, 2011 at 4:09 PM, hayate <hayate.mugen.tens...@gmail.com>wrote:

> Hi guys...

> I've been doing python for about a year now and have never needed a
> gui until now... I've been reading through tutorials on here but don't
> fully understand where the main code goes? lol

Have you looked at this page?:

Getting Started - wxPyWiki

There is a "Hello, World" example there, and then larger examples.

Basically, all wxPython scripts will have essentially the same thing at the
bottom, which is lines 4-7 of the "Hello, World" example. Your code that
does various manipulations of the data will be in other functions in the
script. So, for example, you can:

1) Have a button on the GUI and have it bound to an "event handler"
function.
2) When the user pressed the button, the event handler function is called.
3) That function can then grab the text from a text box (using
textbox.GetValue() ) and do something based on it.
4) Etc.

Does this help get you on the right track?

Che

A couple of quirks to keep in mind when switching to an event driven
system verse procedureal programming (like traditional (now ancient) C
programming.

PreFirst tip:
Getting help here at Google Groups wxpython-users is very useful. But
learn how to make SampleApp, a name wxpyhon-users give to the minimum
app needed to ask your question or show your problem.

First
app.MainLoop()

is almost akin to C's main()
main()
{
    while 1;
        get event from event queue
        send event to handler (ie call the handler function with event
as a parameter)
}

Second
You subclass wx class like the wx.Frame or wx.App to customize them;
but don't forget to call super() in your HayateFrame(wx.Frame). Note
that if you subclass wx.App you will need to override OnInit() AND
return True. Optionally you -might- override __init__, but you don't
have too.

Third
Don't forget to Show your windows inst = HayateFrame(); inst.Show()

Fourth
wxPython GUI expects to be in the main thread. Network IO, FileIO,
complex calculations, etc, can be in other threads.

Fifth
Never assume your events will be dispatched and processed in the order
you expect.

Sixth
Consider creating your GUI code seperate from your data handling; see
OOP patterns like MVC, MVP, Observer pattern.

You may be an expert programmer; buy many new people may be drawn to
this post when seeing your topic title. So I kinda also gave tips for
those new GUI coders too.

If I may add another related one: you should never run GUI operations
from any other thread. It's the wrong way to do things so sometimes
it'll work (usually on your dev machine) and sometimes it'll crash
horribly (usually on the end-users machine). What you want to do is
either send events to the GUI thread that cause it to do the updates
you need (via the event system or pubsub for example), or use
wx.CallLater to call the GUI related methods (which ensures they run
on the correct thread). Note in both cases that you can't necessarily
assume exactly when the update will occur or in what order.

···

On Nov 24, 8:01 am, Dev Player <devpla...@gmail.com> wrote:

Fourth
wxPython GUI expects to be in the main thread. Network IO, FileIO,
complex calculations, etc, can be in other threads.

I don’t think pubsub is thread-safe though. I have used it by wrapping it in wx.CallAfter or wx.CallLater, which are thread-safe though.

···

Mike Driscoll

Blog: http://blog.pythonlibrary.org