How to pass data from a frame when it closes

I am trying to have a frame which is given an existing time. It
remebers this, and has a couple of spin controls and a simple menu
(Use/Discard). If the user slects "Use", it returns the values from
the spin controls; if the user selects "discard", it returns the old
value that it was passed. I have used wxFormBuilder to build the
frame, and then over-ridden the events. What I want is something like
the following code suggests. I may make the frame more complex and
have more data to pass back in the future. Is there an easy way to do
this? Something like the modal dialog?

""" My code, this frame is created from a parent, to which the values
must be returned"""

import Periodic

class PeriodSetter(Periodic.PeriodFrame):

    def __init__( self, parent, a_Interval):
        """ Uses PeriodFrame (generated by wxFormBuilder) to get a new
interval
            a_Interval is in minutes"""
        print "initializing" # debug
        self.oldInterval=a_Interval # remember
        self.hours=self.oldInterval/60
        self.minutes=self.oldInterval%60
        Periodic.PeriodFrame.__init__( self, parent )
        self.Show
        self.m_spinCtrlHours.SetRange(0,23)
        self.m_spinCtrlMins.SetRange(0,59)
        self.m_spinCtrlHours.SetValue(self.hours)
        self.m_spinCtrlMins.SetValue(self.minutes)

    def HoursSpinCtrl( self, event ):
        event.Skip()

    def HoursSpinText( self, event ):
        event.Skip()

    def MinsSpinCtrl( self, event ):
        event.Skip()

    def MinsSpinText( self, event ):
        event.Skip()

    def AcceptClick( self, event ):
        return ((60*self.hours)+self.minutes) # Notional only-
        self.Destroy()

    def DiscardClick( self, event ):
        return(self.oldInterval) # Notional only
        self.Destroy()

# -*- coding: utf-8 -*-

···

###########################################################################
## Python code generated with wxFormBuilder (version Apr 10 2012)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
#import wx.xrc

###########################################################################
## Class PeriodFrame
###########################################################################

class PeriodFrame ( wx.Frame ):

  def __init__( self, parent ):
    wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Set
Periodic Email Interval", pos = wx.DefaultPosition, size =
wx.Size( 297,98 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

    self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

    bSizer3 = wx.BoxSizer( wx.HORIZONTAL )

    self.m_spinCtrlHours = wx.SpinCtrl( self, wx.ID_ANY, wx.EmptyString,
wx.DefaultPosition, wx.DefaultSize, wx.SP_ARROW_KEYS|wx.SP_WRAP, 0,
10, 0 )
    bSizer3.Add( self.m_spinCtrlHours, 0, wx.ALL, 5 )

    self.m_staticText1 = wx.StaticText( self, wx.ID_ANY, u"h",
wx.DefaultPosition, wx.DefaultSize, 0 )
    self.m_staticText1.Wrap( -1 )
    bSizer3.Add( self.m_staticText1, 0, wx.ALL, 5 )

    self.m_spinCtrlMins = wx.SpinCtrl( self, wx.ID_ANY, wx.EmptyString,
wx.DefaultPosition, wx.DefaultSize, wx.SP_ARROW_KEYS|wx.SP_WRAP, 0,
10, 0 )
    bSizer3.Add( self.m_spinCtrlMins, 0, wx.ALL, 5 )

    self.m_staticText2 = wx.StaticText( self, wx.ID_ANY, u"m",
wx.DefaultPosition, wx.DefaultSize, 0 )
    self.m_staticText2.Wrap( -1 )
    bSizer3.Add( self.m_staticText2, 0, wx.ALL, 5 )

    self.SetSizer( bSizer3 )
    self.Layout()
    self.m_menubar1 = wx.MenuBar( 0 )
    self.m_menu3 = wx.Menu()
    self.m_mniAccept = wx.MenuItem( self.m_menu3, wx.ID_ANY, u"Accept
and quit", wx.EmptyString, wx.ITEM_NORMAL )
    self.m_menu3.AppendItem( self.m_mniAccept )

    self.m_mniDiscard = wx.MenuItem( self.m_menu3, wx.ID_ANY, u"Discard
and quit", wx.EmptyString, wx.ITEM_NORMAL )
    self.m_menu3.AppendItem( self.m_mniDiscard )

    self.m_menubar1.Append( self.m_menu3, u"Use/Discard" )

    self.SetMenuBar( self.m_menubar1 )

    self.Centre( wx.BOTH )

    # Connect Events
    self.m_spinCtrlHours.Bind( wx.EVT_SPINCTRL, self.HoursSpinCtrl )
    self.m_spinCtrlHours.Bind( wx.EVT_TEXT, self.HoursSpinText )
    self.m_spinCtrlMins.Bind( wx.EVT_SPINCTRL, self.MinsSpinCtrl )
    self.m_spinCtrlMins.Bind( wx.EVT_TEXT, self.MinsSpinText )
    self.Bind( wx.EVT_MENU, self.AcceptClick, id =
self.m_mniAccept.GetId() )
    self.Bind( wx.EVT_MENU, self.DiscardClick, id =
self.m_mniDiscard.GetId() )

  def __del__( self ):
    pass

  # Virtual event handlers, overide them in your derived class
  def HoursSpinCtrl( self, event ):
    event.Skip()

  def HoursSpinText( self, event ):
    event.Skip()

  def MinsSpinCtrl( self, event ):
    event.Skip()

  def MinsSpinText( self, event ):
    event.Skip()

  def AcceptClick( self, event ):
    event.Skip()

  def DiscardClick( self, event ):
    event.Skip()

Hi,

I prefer to use pubsub for this sort of thing. In fact, I wrote a tutorial on this very topic: http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/

Hope that helps!

  • Mike

frame, and then over-ridden the events. What I want is something like
the following code suggests.

Generally you'd want to post a more pared-down code sample; this
includes a lot of stuff that's irrelevant to your concern.

have more data to pass back in the future. Is there an easy way to do
this?

One easy way...from within your child frame:

parentFrame = childFrame.GetParent()
#Do stuff and thereby acquire new value here called newValue
parentFrame.value_you_want_passed_to_it = newValue

Che

Totally brilliant! Works beautifully! Thanks.

···

On Jun 6, 11:39 am, C M <cmpyt...@gmail.com> wrote:

> frame, and then over-ridden the events. What I want is something like
> the following code suggests.

Generally you'd want to post a more pared-down code sample; this
includes a lot of stuff that's irrelevant to your concern.

> have more data to pass back in the future. Is there an easy way to do
> this?

One easy way...from within your child frame:

parentFrame = childFrame.GetParent()
#Do stuff and thereby acquire new value here called newValue
parentFrame.value_you_want_passed_to_it = newValue

Che