ListCtrl help

Need some help with getting the data I type in my Dialog to be
outputted in the ListCtrl once the OK button is clicked. I'm not sure
how to go about doing this any help would be great.

import wx

class Phonebook(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, "PhoneBook v1.0", size=
(500,600))

        menubar = wx.MenuBar()

        first = wx.Menu()

        first.Append(101, "New Contact...","Add a New Contact")
        self.Bind(wx.EVT_MENU, self.OnAdd, id=101)

        first.Append(102, "New Group...", "Creates New Group")
        self.Bind(wx.EVT_MENU, self.OnGroup, id=102)

        first.Append(103, "Delete Contact...", "Delete a Contact")
        self.Bind(wx.EVT_MENU, self.OnDel, id=103)

        first.Append(104, "Delete Group...", "Delete a Group")
        self.Bind(wx.EVT_MENU, self.OnDelGroup, id=104)

        first.AppendSeparator()

        first.Append(105, "Exit", "Exit Phonebook")
        self.Bind(wx.EVT_MENU, self.OnExit, id=105)

        menubar.Append(first, "File")

        #End File Menu

        second = wx.Menu()

        second.Append(106, "Search...", "Search for Contact")
        self.Bind(wx.EVT_MENU, self.OnDelGroup, id=106)

        second.AppendSeparator()

        second.Append(107, "Show All", "View All Contacts")
        self.Bind(wx.EVT_MENU, self.OnDelGroup, id=107)

        second.AppendSeparator()

        second.Append(108, "Import", "Import an Address Book")
        self.Bind(wx.EVT_MENU, self.OnDelGroup, id=108)

        menubar.Append(second, "Edit")

        #End Edit Menu

        third = wx.Menu()

        third.Append(109, "Contact", "Contact Us")
        self.Bind(wx.EVT_MENU, self.OnDelGroup, id=109)

        third.AppendSeparator()

        third.Append(110, "About", "About Phonebook")
        self.Bind(wx.EVT_MENU, self.OnAbout, id=110)

        menubar.Append(third, "Help")

        self.SetMenuBar(menubar)

        #End Help Menu

        #StartListCtrl

        self.lc = wx.ListCtrl(self,id, size=(-1, 120),
        style=wx.LC_REPORT|wx.SUNKEN_BORDER|wx.LC_HRULES)

        self.loadList()

        #EndListCtrl

    def loadList(self):
        self.lc.InsertColumn(0, "First Name")
        self.lc.SetColumnWidth(0, 150)
        self.lc.InsertColumn(1, "Last Name")
        self.lc.SetColumnWidth(1, 150)
        self.lc.InsertColumn(2, "Phone Number")
        self.lc.SetColumnWidth(2, 188)

        #EndListCtrl

        status = self.CreateStatusBar()

    def OnAdd(self,event):
        dlg=MyDialog(self,-1)
        dlg.ShowModal()
        dlg.Destroy()

    def OnGroup(self,event):
        self.Close()

    def OnDel(self,event):
        self.Close()

    def OnDelGroup(self,event):
        self.Close()

    def OnExit(self, event):
        self.Close()

    def OnAbout(self, event):
        wx.MessageBox("Phonebook v1.0\n\nMade by: tweak", "About",
wx.OK)

class MyDialog(wx.Dialog):
    def __init__(self, parent, id):
        wx.Dialog.__init__(self, parent, id)
        grid = wx.GridBagSizer(5, 5)

        grid.Add(wx.StaticText(self, -1, 'First Name'),(0,0))
        grid.Add(wx.StaticText(self, -1, 'Last Name'),(1,0))
        grid.Add(wx.StaticText(self, -1, 'Phone number'),(2,0))
        self.name = wx.TextCtrl(self, -1)
        self.lname = wx.TextCtrl(self, -1)
        self.number = wx.TextCtrl(self, -1)
        grid.Add(self.name,(0,1))
        grid.Add(self.lname,(1,1))
        grid.Add(self.number,(2,1))

        self.ok = wx.Button(self, wx.ID_OK, 'OK')
        self.cancel = wx.Button(self, wx.ID_CANCEL, 'Cancel')
        grid.Add(self.ok,(3,0))
        grid.Add(self.cancel,(3,2))

        self.SetSizer(grid)
        self.Fit()

if __name__=='__main__':
    app = wx.PySimpleApp()
    frame = Phonebook(parent=None, id=-1)
    frame.Show()
    app.MainLoop()

def OnAdd(self,event):

dlg=MyDialog(self,-1)

result = dlg.ShowModal()

if result == wx.ID_OK:

pos = self.lc.GetItemCount()

self.lc.InsertStringItem(pos,dlg.name.GetValue())

self.lc.SetStringItem(pos,1,dlg.lname.GetValue())

self.lc.SetStringItem(pos,2,dlg.number.GetValue())

dlg.Destroy()

The basic pattern for using dialogs goes something like this:

  dlg = SomeDialogClass(self)
  dlg.SetWidgetValues(foo, bar)
  if dlg.ShowModal() == wx.ID_OK:
    foo, bar = dlg.GetWidgetValues()
    self.DoSomething(foo, bar)
  dlg.Destroy()

For adding items to the listctrl you can use InsertStringItem to make a new item and set the text for the first column, and SetStringItem to set the text values for the remaining columns.

···

On 9/28/09 12:18 AM, tweak wrote:

Need some help with getting the data I type in my Dialog to be
outputted in the ListCtrl once the OK button is clicked. I'm not sure
how to go about doing this any help would be great.

--
Robin Dunn
Software Craftsman

Thanks this worked great! But would you also know of any tutorial that
would explain what all this is doing so I actually understand what is
going on?

···

On Sep 28, 2:18 pm, Mike Conley <mconl...@gmail.com> wrote:

def OnAdd(self,event):
dlg=MyDialog(self,-1)
result = dlg.ShowModal()
if result == wx.ID_OK:
pos = self.lc.GetItemCount()
self.lc.InsertStringItem(pos,dlg.name.GetValue())
self.lc.SetStringItem(pos,1,dlg.lname.GetValue())
self.lc.SetStringItem(pos,2,dlg.number.GetValue())
dlg.Destroy()

Thanks this worked great! But would you also know of any tutorial that
would explain what all this is doing so I actually understand what is
going on?

Well, let's break this down...

> def OnAdd(self,event):
> dlg=MyDialog(self,-1)
> result = dlg.ShowModal()
> if result == wx.ID_OK:
> pos = self.lc.GetItemCount()
> self.lc.InsertStringItem(pos,dlg.name.GetValue())
> self.lc.SetStringItem(pos,1,dlg.lname.GetValue())
> self.lc.SetStringItem(pos,2,dlg.number.GetValue())
> dlg.Destroy()

# create a dialog instance
dlg=MyDialog(self,-1)

# show the instance and set a variable
# to hold what button was used to dismiss
# the dialog
result = dlg.ShowModal()

# check if the user pressed the OK button
# if he did, grab info from the dialog's fields
if result == wx.ID_OK:

# this gets the number of items in the listctrl
# note that since most things python are zero
# based, this also tells you where to insert
# your next item
pos = self.lc.GetItemCount()

# use InsertStringItem to add a new row
# to the list control and give the first
# column the text from dlg.name.GetValue()
self.lc.InsertStringItem(pos,dlg.name.GetValue())

# this will set the 2nd column to the text
# found in dlg.lname.GetValue()
# this is where that zero-based stuff comes in
# note the 2nd parameter is "1", but maps to
# column 2
self.lc.SetStringItem(pos,1,dlg.lname.GetValue())

# this is basically the same as above, but
# it's for the 3rd column (note the "2")
self.lc.SetStringItem(pos,2,dlg.number.GetValue())

# finally, we destroy the dialog
dlg.Destroy()

Note: If you just hide the dialog, you'll probably forget to destroy
it later and it will hang out in the background and cause your main
program to basically become a zombie when you close it. Dialogs aren't
met to be kept around, so just get rid of them.

I hope that answered your question.

···

On Sep 30, 5:45 am, tweak <sprinkles.ch...@gmail.com> wrote:

On Sep 28, 2:18 pm, Mike Conley <mconl...@gmail.com> wrote:

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Thanks a lot, that answered everything good explanation.

···

On Sep 30, 9:53 am, Mike Driscoll <kyoso...@gmail.com> wrote:

On Sep 30, 5:45 am, tweak <sprinkles.ch...@gmail.com> wrote:

> Thanks this worked great! But would you also know of any tutorial that
> would explain what all this is doing so I actually understand what is
> going on?

Well, let's break this down...

> On Sep 28, 2:18 pm, Mike Conley <mconl...@gmail.com> wrote:

> > def OnAdd(self,event):
> > dlg=MyDialog(self,-1)
> > result = dlg.ShowModal()
> > if result == wx.ID_OK:
> > pos = self.lc.GetItemCount()
> > self.lc.InsertStringItem(pos,dlg.name.GetValue())
> > self.lc.SetStringItem(pos,1,dlg.lname.GetValue())
> > self.lc.SetStringItem(pos,2,dlg.number.GetValue())
> > dlg.Destroy()

# create a dialog instance
dlg=MyDialog(self,-1)

# show the instance and set a variable
# to hold what button was used to dismiss
# the dialog
result = dlg.ShowModal()

# check if the user pressed the OK button
# if he did, grab info from the dialog's fields
if result == wx.ID_OK:

# this gets the number of items in the listctrl
# note that since most things python are zero
# based, this also tells you where to insert
# your next item
pos = self.lc.GetItemCount()

# use InsertStringItem to add a new row
# to the list control and give the first
# column the text from dlg.name.GetValue()
self.lc.InsertStringItem(pos,dlg.name.GetValue())

# this will set the 2nd column to the text
# found in dlg.lname.GetValue()
# this is where that zero-based stuff comes in
# note the 2nd parameter is "1", but maps to
# column 2
self.lc.SetStringItem(pos,1,dlg.lname.GetValue())

# this is basically the same as above, but
# it's for the 3rd column (note the "2")
self.lc.SetStringItem(pos,2,dlg.number.GetValue())

# finally, we destroy the dialog
dlg.Destroy()

Note: If you just hide the dialog, you'll probably forget to destroy
it later and it will hang out in the background and cause your main
program to basically become a zombie when you close it. Dialogs aren't
met to be kept around, so just get rid of them.

I hope that answered your question.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org