[Tutor] help with global variables?

Jeff Peery jeffpeery at yahoo.com
Fri Jul 9 01:19:44 CEST 2004


one more question... I think I have a problem with my globals, I have some constants that I use throughout my application; however they do not work, I receive error messages that the "global is not defined", it is defined, which makes me think I defined it in the wrong place. where do you put globals? you say the "file level," could you be more specific? I have one script per dialog/frame, do I need to redefine the globals for each frame and dialog, shouldn't I be able to place them in the application file and have the globals available for each frame and dialog? 
 
thanks.
 
Jeff

Alan Gauld <alan.gauld at blueyonder.co.uk> wrote:

> Hello, I have an application I am writing with wxpython and the
> boa constructor.

Dunno anything about Boa so that might invalidate my reply...

> I am unsure how to "talk between frames and dialogs".
> Global variables don't seem to be passed into child dialog
> and other frames from the parent frame,

Global variables exist at the file level and should be visible within
any function within that file. However global variables are almnost
certainly the wrong answer! :-)

> I am unsure as to how to get a value from a control in the parent
> dialog from the child dialog.

Just to be clear, we are talking about GUI control here not
class inheritance or widget containment?
ie You have a dialog whereby you hit a button and a new "child"
dialog opens?

>
> myValue = self.textCtrl2.GetValue()
>
> but what if the above line is in a different dialog or frame
> than the text field?

If its in a different object then one of two situations arises:

1) If you know the new dialog needs to know it you pass a refernce
to the parent dialog into the child dialog constructor when you
call it. The child dialog can then call

parent.textCtrl2.GetValue()

OR

2) If the parent needs to know the value from the child dialog you
return it as a result. Without knowing more about the design of your
application its hard to be definite.

But one thing you should never be doing is mixing up GUI widget
code and application logic... So if its an application object that
needs to know then pass a reference to the app object to the dialog
or a reference to the dialog to the app (latter is best for reuse).

HTH

Alan G.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20040708/619d590d/attachment.html


More information about the Tutor mailing list