how to send data from dialog to main frame

There are two ways to do this.
1. get the data from the dialog controls after it completes.
dlg = mydlg()
result = dlg.ShowModal()
if result == wx.ID_OK:
     value = dlg.control.GetValue()
     # etc.
dlg.Destroy() # MUST get values before destroying dlg

2. In the dlg, hook the OK button:
     btn = wx.Button(self, wx.ID_OK)
     btn.Bind(wx.EVT_BUTTON, self.OnOk)
     ...
def OnOk(self, evt):
     self.value1 = self.text.GetValue()
     # etc.
     wx.Dialog.ProcessEvent(self, evt)

Then when you use it:
dlg = mydlg()
result = dlg.ShowModal()
dlg.Destroy() # needn't get values before destroying dlg
if result == wx.ID_OK:
     value = dlg.value1
     # etc.

···

At 09:47 PM 11/29/2007, you wrote:

I'm confused about something very basic. Although I can use a wxMessageDialog to allow a yes or no answer which has results on the main frame, I wanted to try creating my own dialog instead, and am not sure how to do this and have it send anything (data or events) back to the main frame.

The dialog is a class in its own module. When needed in the main frame I import it and use dot notation to create an instance of the dialog and then ShowModal() to show it. There are buttons which when pressed are intended to change things back on the main frame. But how do I do that? If I import the main frame in the dialog module's code and then make an instance of the main frame, it's a *new* instance, and not the originally running instance when I started the app. I wind up with two main frames running and haven't delivered the data.

The demo for wxDialog mentions basically what I want to do: "The example is very simple; in real world situations, a dialog that had input fields such as this would no doubt be required to deliver those values back to the calling function. The Dialog class supports data retrieval in this manner." I just don't understand how the class supports this.

Any help is appreciated.

Thank you. This works, but I would like to understand why it works. Questions…

  1. If I change wx.ID_OK (in both the dialog’s and frame’s code) to wx.ID_CANCEL it still works, but if I use wx.ID_NO or wx.ID_DELETE it doesn’t. Why is that?

1b. the line result = dlog.ShowModal
() I understand, but I don’t understand how the act of showing the modal dialog can == wx.ID_OK, because I’m not sure what wx.ID_OK represents.

  1. What is the purpose here of wx.Dialog.ProcessEvent(self, evt)?

Thanks again.

···

On Nov 30, 2007 1:24 AM, Phil Mayes phil@philmayes.com wrote:

At 09:47 PM 11/29/2007, you wrote:

I’m confused about something very basic. Although I can use a
wxMessageDialog to allow a yes or no answer which has results on the main

frame, I wanted to try creating my own dialog instead, and am not sure how
to do this and have it send anything (data or events) back to the main frame.

The dialog is a class in its own module. When needed in the main frame I

import it and use dot notation to create an instance of the dialog and
then ShowModal() to show it. There are buttons which when pressed are
intended to change things back on the main frame. But how do I do

that? If I import the main frame in the dialog module’s code and then
make an instance of the main frame, it’s a new instance, and not the
originally running instance when I started the app. I wind up with two

main frames running and haven’t delivered the data.

The demo for wxDialog mentions basically what I want to do: "The example
is very simple; in real world situations, a dialog that had input fields

such as this would no doubt be required to deliver those values back to
the calling function. The Dialog class supports data retrieval in this
manner." I just don’t understand how the class supports this.

Any help is appreciated.

There are two ways to do this.

  1. get the data from the dialog controls after it completes.
    dlg = mydlg()
    result = dlg.ShowModal()
    if result == wx.ID_OK:

    value = dlg.control.GetValue()

    etc.

dlg.Destroy() # MUST get values before destroying dlg

  1. In the dlg, hook the OK button:
    btn = wx.Button(self, wx.ID_OK)
    btn.Bind(wx.EVT_BUTTON
    , self.OnOk)

    def OnOk(self, evt):
    self.value1 = self.text.GetValue()

    etc.

    wx.Dialog.ProcessEvent(self, evt)

Then when you use it:
dlg = mydlg()
result = dlg.ShowModal()

dlg.Destroy() # needn’t get values before destroying dlg
if result == wx.ID_OK:
value = dlg.value1
# etc.

C M wrote:

Thank you. This works, but I would like to understand why it works. Questions...

1. If I change wx.ID_OK (in both the dialog's and frame's code) to wx.ID_CANCEL it still works, but if I use wx.ID_NO or wx.ID_DELETE it doesn't. Why is that?

wx.Dialog has default button handlers for buttons with an ID of wx.ID_OK and wx.ID_CANCEL. If you have buttons with other IDs then you need to add handlers for them yourself.

1b. the line result = dlog.ShowModal () I understand, but I don't understand how the act of showing the modal dialog can == wx.ID_OK, because I'm not sure what wx.ID_OK represents.

The return value of ShowModal is whatever value was passed to EndModal. So the default handler for the ok button calls (among other things) self.EndModal(wx.ID_OK) and the cancel button handler calls self.EndModal(wx.ID_CANCEL). You can call EndModal from your own handlers with some other value if you want to do so.

3. What is the purpose here of wx.Dialog.ProcessEvent(self, evt)?

It is resending the event. What is probably intended is that the default event handler is still called even though it is caught by this handler. A better way to do that is to simply call evt.Skip().

BTW, you'll probably want to read the section in the book on Validators, as that describes another way to transfer data to/from a dialog that you might find to be simpler because once it is set up it is automatic.

···

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

Thank you for the explanations, it is helpful.

···

On Nov 30, 2007 6:33 PM, Robin Dunn robin@alldunn.com wrote:

C M wrote:

Thank you. This works, but I would like to understand why it works.
Questions…

  1. If I change wx.ID_OK (in both the dialog’s and frame’s code) to

wx.ID_CANCEL it still works, but if I use wx.ID_NO or wx.ID_DELETE it
doesn’t. Why is that?

wx.Dialog has default button handlers for buttons with an ID of wx.ID_OK
and wx.ID_CANCEL. If you have buttons with other IDs then you need to

add handlers for them yourself.

1b. the line result = dlog.ShowModal () I understand, but I don’t
understand how the act of showing the modal dialog can == wx.ID_OK
,
because I’m not sure what wx.ID_OK represents.

The return value of ShowModal is whatever value was passed to EndModal.
So the default handler for the ok button calls (among other things)

self.EndModal(wx.ID_OK) and the cancel button handler calls
self.EndModal(wx.ID_CANCEL). You can call EndModal from your own
handlers with some other value if you want to do so.

  1. What is the purpose here of wx.Dialog.ProcessEvent(self, evt)?

It is resending the event. What is probably intended is that the
default event handler is still called even though it is caught by this

handler. A better way to do that is to simply call evt.Skip().

BTW, you’ll probably want to read the section in the book on Validators,
as that describes another way to transfer data to/from a dialog that you

might find to be simpler because once it is set up it is automatic.


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