Can anybody explain how to use TransferDataToWindow/TransferDataFromWindow?

Hi Thomas,

Below you will find an old, quick and dirty application I wrote once in order
to learn this stuff. It was created with infos coming from this mailing list.
Developed under wxPython 2.5.x, it just work out of the box with wxPy 2.8.0.1
and may need some cleanings.
With the hope, it helps.

Jean-Michel Fauth, Switzerland

# -*- coding: iso-8859-1 -*-

···

#--------------------------------------------------------------------
# Name: TransferFromDlg.py
# Purpose: Test
# Author: Jean-Michel Fauth, Switzerland
# Copyright: (c) 2002-2004 Jean-Michel Fauth
# Licence: GPL
#--------------------------------------------------------------------
# os dev: windows 98
# py dev: Python 2.4
# wx dev: wxPython 2.5.3.1
# Revision: 19 January 2005
#--------------------------------------------------------------------

#~ At 2005-01-18 09:16 PM +0000, you wrote:
#~ >I've been experimenting with passing information from a dialog to the main
#~ >frame.Unfortunately, I've not come to a solution so far :frowning: After I asked
#~ >on freenode, I was told two thing and neither of them worked out. Here are
#~ >they:
#~ >1.Passing a variable to the constructor and changing it before destroying
#~ >the control. This doesn't work, because the we're working with a copy of
#~ >the var... I wished I could use C++ pointers :frowning:

#~ In your calling code, create a dictionary
          #~ context = {}

#~ Pass it into your dialog class:
          #~ def __init__( self, context ):
                  #~ ...
                  #~ self.context = context

#~ Use any values in 'context' to populate your widgets.
#~ Override the TransferDataFromWindow method to copy
#~ values back from the widgets to 'context'.

#~ When ShowModal returns (in your calling code),
#~ call dlog.TransferDataFromWindow(), and voila! the
#~ values are in the dictionary.

#~ HTH.

#~ - Sam

#--------------------------------------------------------------------

import wx

#-------------------------------------------------------------------

class MyDialog(wx.Dialog):

     def __init__(self, parent, id, context):
         title = 'fenêtre de dialogue'
         pos, size = (-1, -1), (300, 300)
         wx.Dialog.__init__(self, parent, id, title, pos, size)
         self.CenterOnScreen()

         self.context = context

         self.but1 = wx.Button(self, wx.NewId(), '&ok', (10, 10), wx.DefaultSize)
         self.but1.Bind(wx.EVT_BUTTON, self.OnClick1)

         self.but2 = wx.Button(self, wx.NewId(), '&cancel', (10, 50), wx.DefaultSize)
         self.but2.Bind(wx.EVT_BUTTON, self.OnClick2)

         self.but3 = wx.Button(self, wx.NewId(), 'modify array', (130, 10), wx.DefaultSize)
         self.but3.Bind(wx.EVT_BUTTON, self.OnClick3)

         self.txt1 = wx.TextCtrl(self, wx.NewId(), 'aa bb cc', (10, 90), (100, -1))
         self.txt2 = wx.TextCtrl(self, wx.NewId(), 'mm nn oo', (10, 130), (100, -1))

         self.txt1.SetValue(self.context['txt1'])
         self.txt2.SetValue(self.context['txt2'])

         #~ self.Bind(wx.EVT_CHAR_HOOK, self.OnCharHook)

     def TransferDataFromWindow(self):
         self.context['txt1'] = self.txt1.GetValue()
         self.context['txt2'] = self.txt2.GetValue()

     def OnClick1(self, event):
         self.EndModal(wx.ID_OK)

     def OnClick2(self, event):
         self.EndModal(wx.ID_CANCEL)

     def OnClick3(self, event):
         for i in xrange(len(self.context['ary'])):
             self.context['ary'][i] += 0.123
             print self.context['ary'][i],
             print

     #~ def OnCharHook(self, event):
         #~ if event.KeyCode() == wx.WXK_ESCAPE:
             #~ self.EndModal(wx.ID_CANCEL)
         #~ else:
             #~ event.Skip()

#-------------------------------------------------------------------

class MyPanel(wx.Panel):

     def __init__(self, parent, id):
         wx.Panel.__init__(self, parent, id, wx.DefaultPosition, wx.DefaultSize)

         self.but1 = wx.Button(self, wx.NewId(), 'dialog', (10, 10), wx.DefaultSize)
         self.but1.Bind(wx.EVT_BUTTON, self.OnClick1)

         self.context = {'txt1': 'aaa', 'txt2': 'bbb', 'ary': range(5)}

     def OnClick1(self, event):
         dlg = MyDialog(self, wx.NewId(), self.context)
         r = dlg.ShowModal()
         if r == wx.ID_CANCEL:
             print 'cancel'
             print self.context
         elif r == wx.ID_OK:
             print 'ok'
             r2 = dlg.TransferDataFromWindow()
             print 'r2:', r2
             print self.context
         else:
             pass
         dlg.Destroy()

#--------------------------------------------------------------------

class MyFrame(wx.Frame):

     def __init__(self, parent, id, title, pos, size, style):
         wx.Frame.__init__(self, parent, id, title, pos, size, style)
         #~ self.CentreOnScreen()

         self.panel = MyPanel(self, wx.NewId())

         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

     def OnCloseWindow(self, event):
         self.Destroy()

#--------------------------------------------------------------------

class MyApp(wx.App):

     def OnInit(self):
         sty = wx.DEFAULT_FRAME_STYLE
         #~ sty = wx.SYSTEM_MENU | wx.CAPTION | wx.MINIMIZE_BOX | wx.CLOSE_BOX
         frame = MyFrame(None, -1, __file__, (0, 0), (300, 200), sty)
         frame.Show(True)
         self.SetTopWindow(frame)
         return True

#--------------------------------------------------------------------

def main():
     app = MyApp(False)
     app.MainLoop()

#--------------------------------------------------------------------

if __name__ == "__main__":
     main()

#eof-----------------------------------------------------------------