Data transfer problem - Solved

I am relatively new to wxPython and having a problem transferring an integer from the main window to a child dialog.
The situation: developing contact (phone-book) app for my own use.
I use a wxListCtrl to list the contacts (persons only) which is connected to an event handler. When a contact is selected (listCtrl is configured to single selection).
The event handler activates an “Edit” button. This Edit button when pressed gets the primary key of the selected contact and calls a dialog (I named it personInput) and transfers the primary key to this dialog to permit getting and loading the data into this dialog box. The code is:

def onEdit(self, event):
        ''' 1. get the selected person or company   
            2. open either the personInput or the companyInput dialog
        '''
        
        if self.lbPersons.GetSelection()>0:
            selItem = self.lbPersons.GetString(self.lbPersons.GetSelection())
            for Pers in self.personKeys:
                if selItem == Pers[1]:
                    vKey =(Pers[0])
            
        inputPersDlg = PI.personInput(self)
        inputPersDlg.getAction(vKey)
        inputPersDlg. ShowModal()
        inputPersDlg.Destroy()

The dialog has the function self.getAction:

self.result=self.getAction(*args)
        print(self.result)

    def getAction(self, event):
        return event

My problem is the integer is not transferred but a wx.Window object is transferred. the print displays:

<main.mainWindow object at 0x7fbeb5f3b130>

It is clear that my transfer code is incorrect but I have not yet found a correction.
Hope someone can help me.

Are you really using a wx.ListCtrl?

The ‘lb’ prefix to lbPersons and the use of GetSelection() and GetString() methods suggests you are actually using a wx.ListBox.

Edit: if so, you could do something like this:

import wx

NAMES = ("Tom", "Dick", "Harry")

class MyDialog(wx.Dialog):
    def __init__(self, *args, **kwds):
        wx.Dialog.__init__(self, *args, **kwds)
        self.SetSize((400, 200))
        self.SetTitle("Edit Name")
        main_sizer = wx.BoxSizer(wx.VERTICAL)
        top_sizer = wx.BoxSizer(wx.HORIZONTAL)
        main_sizer.Add(top_sizer, 1, wx.EXPAND, 0)
        self.text_ctrl = wx.TextCtrl(self, wx.ID_ANY, "")
        top_sizer.Add(self.text_ctrl, 1, wx.ALIGN_CENTER_VERTICAL, 0)
        bottom_sizer = wx.StdDialogButtonSizer()
        main_sizer.Add(bottom_sizer, 0, wx.ALIGN_RIGHT | wx.ALL, 4)
        self.button_OK = wx.Button(self, wx.ID_OK, "")
        self.button_OK.SetDefault()
        bottom_sizer.AddButton(self.button_OK)
        self.button_CANCEL = wx.Button(self, wx.ID_CANCEL, "")
        bottom_sizer.AddButton(self.button_CANCEL)
        bottom_sizer.Realize()
        self.SetSizer(main_sizer)
        self.SetAffirmativeId(self.button_OK.GetId())
        self.SetEscapeId(self.button_CANCEL.GetId())
        self.Layout()

    def getName(self):
        return self.text_ctrl.GetValue()

    def setName(self, name):
        self.text_ctrl.SetValue(name)


class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.SetSize((400, 300))
        self.SetTitle("Contacts")
        self.panel = wx.Panel(self, wx.ID_ANY)
        main_sizer = wx.BoxSizer(wx.VERTICAL)
        self.list_box = wx.ListBox(self.panel, wx.ID_ANY, choices=[])
        main_sizer.Add(self.list_box, 1, wx.EXPAND, 0)
        bottom_sizer = wx.BoxSizer(wx.HORIZONTAL)
        main_sizer.Add(bottom_sizer, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM | wx.TOP, 6)
        self.close_button = wx.Button(self.panel, wx.ID_ANY, "Close")
        bottom_sizer.Add(self.close_button, 0, wx.RIGHT, 36)
        self.edit_button = wx.Button(self.panel, wx.ID_ANY, "Edit")
        bottom_sizer.Add(self.edit_button, 0, wx.RIGHT, 24)
        self.panel.SetSizer(main_sizer)
        self.Layout()

        self.list_box.SetItems(NAMES)
        self.Bind(wx.EVT_BUTTON, self.OnEdit, self.edit_button)
        self.Bind(wx.EVT_BUTTON, self.OnClose, self.close_button)

    def OnClose(self, _event):
        self.Destroy()

    def OnEdit(self, _event):
        index = self.list_box.GetSelection()

        if index != wx.NOT_FOUND:
            name = self.list_box.GetString(index)
            dialog = MyDialog(self)
            dialog.setName(name)
            result = dialog.ShowModal()
            new_name = dialog.getName()
            if result == wx.ID_OK:
                self.list_box.SetString(index, new_name)
                result = "OK"
            else:
                result = "Cancel"
            print(result, new_name)
            dialog.Destroy()


if __name__ == "__main__":
    app = wx.App()
    frame = MyFrame(None)
    frame.Show()
    app.MainLoop()

You are right I made an error in identifying the widget, it is a ListBox not a ListCtrl. I had started out using a listCtrl but changed to the more simple ListBox. Sorry!.

All the dialogs are standalone files, not integrated into the main frame file. This application will have when completed 5 standalone dialogs for data insertion, update, filtration and display. This process in more detail: Main window containing a listbox of person names and another listbox for company names below it. when the form opens all the persons and companies are automatically loaded the their respective listboxes. The buttons are all disabled except the Quit and the New buttons. When I click on a person’s name all his/her communication devises appear in a ListCtrl (real one this time) to the right of the person listbox and to the right of that the related company name (if one exists) and Notes, if any.
The Main Window has limited data (name, comm devices and company name). If I want more detailed information such as addresses I use a dialog.
My problem is transferring the Primary key of the selection to the appropriate dialog box. The integer number is clearly identified in my main window as an integer (using a print function) but changes to an object when I call the dialog from inside the main window code. I am probably using the wrong code but my research to date has not given direction towards the right formulation of the code.
I could for example insert a TextCtrl in the dialog and transfer the integer into that TextCtrl. I’ve done that in a other app and it works fine. I thought that a simple function would do the trick. I still think that’s the way to go but I’m not doing it right.

I should know by now not to try and guess what is happening without seeing any actual working code. Apologies for wasting your time.

No apologies necessary. My time was not wasted. Just the fact that I got an answer is positive. Studying your code is also a learning process. I don’t use forums very often and trying to balance the amount of information transmitted and the forum space allowed is difficult. I’ve uploaded the code. Still a work in progress

Happy New Year! :grin:

Continued to experiment and found a solution:
It seems that I was on the right track but failed in the application.
In the receiving dialog I created a function getAction() but I was calling it in the dialog’s init function which I can’t do since I already called it from my parent form. All the code to execute the SQL query and subsequent widget must start from inside this function called by the parent frame.
code in dialog: (gave function a name change)

def editAction(self,event):
        self.persToEdit = SQL.getEditPerson(event)
        print(self.persToEdit)
        # form loading functions will be called from here