[Tutor] passing variables between frames?
Kent Johnson
kent37 at tds.net
Tue May 24 00:32:50 CEST 2005
Jeff Peery wrote:
> Hello, I am having trouble with passing variables between frames (Frame1
> and Dialog1). I'm using wxpython to write an application. when I run my
> application the parent frame appears and within the parent frame I can
> assign values to variables. I then have a button that launches a child
> dialog. I want to access the values from the parent frame in the
> dialog. for example say I had a listbox in the parent frame and I
> choose the first entry 'a' from the list. I then launch the child
> dialog and I want to put the value of the selection from the list box
> into a text field - when I lauch the child dialog a text field appears
> with the value '0'. From within Frame1 I can use the getselection()
> function to get the listbox selection. although in the child dialog I
> tried to use Frame1.listBox.getselection() to get the selection from
> Frame1. this doesn't work. not sure what to do here? any ideas? thanks.
It's hard to know specifically what is broken without seeing code or error messages. My guess is
that the variable Frame1 is not in scope in the dialog box code. But I would suggest a different
approach...
- Make the dialog box code independent of the values of variables in Frame1. Generally for me this
means packaging up the dialog box in a function or class that is passed the values it needs to use
and returns some result to the user. A very simple example is a dialog box that presents an error
message; you could make a function showError(msg) that doesn't know anything about its caller.
- In the event handler for the button in Frame1, gather the values needed by the dialog, launch it,
get the result and handle it.
This style keeps your dialog code independent of the rest of the program. An immediate benefit is
that you can write a simple test driver that exercises the dialog without having to create any
infrastructure. (This is the way I create most dialog boxes. I run it standalone until I am happy
with its appearance and function, then I integrate it into the rest of the app.) Another long-term
benefit is the dialogs are reusable; you may not see a need for this but in the long run some of
them will probably have more than one use.
HTH,
Kent
More information about the Tutor
mailing list