Well I'm trying to make a phone book program and I need some help what I want to do is add a ListCtrl to my frame so I can see the names and numbers of people in my phone book but I am unsure how to do this. Here is my code so far.

import wx
import anydbm
import  wx.lib.mixins.listctrl  as  listmix
import sys

class Phonebook(wx.Frame):
    
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, "PhoneBook v1.0", size=(500,600))
        
        db = anydbm.open("phonebook.db", "c")
        
        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
        
        status = self.CreateStatusBar()
        
    def OnAdd(self,event):
        dlg=MyDialog(self,-1)
        dlg.ShowModal()
        dlg.Destroy()
        
    def OnGroup(self,event):
        group = raw_input('Enter Group Name:')
        del[group]
        
    def OnDel(self,event):
        delete = raw_input('Enter Contact Name:')
        del[delete]
        
    def OnDelGroup(self,event):
        delete = raw_input('Enter Group To Delete:')
        
    def OnExit(self, event):
        self.Close()
        
    def OnAbout(self, event):
        wx.MessageBox("Phonebook v1.0\n\nMade by: tweak\n\nrevised.org", "About", wx.OK)
        
        listctrldata = {
        1 : ("Hey!", "You can edit", "me!"),
        2 : ("Try changing the contents", "by", "clicking"),
        3 : ("in", "a", "cell")
        }
        
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, 'Name'),(0,0))
        grid.Add(wx.StaticText(self, -1, 'Phone number'),(1,0))
        self.name = wx.TextCtrl(self, -1)
        self.number = wx.TextCtrl(self, -1)
        grid.Add(self.name,(0,1))
        grid.Add(self.number,(1,1))
        
        self.ok = wx.Button(self, wx.ID_OK, 'OK')
        self.cancel = wx.Button(self, wx.ID_CANCEL, 'Cancel')
        grid.Add(self.ok,(2,0))
        grid.Add(self.cancel,(2,2))
        
        self.SetSizer(grid)
        self.Fit()
        
        
if __name__=='__main__':
    app = wx.PySimpleApp()
    frame = Phonebook(parent=None, id=-1)
    frame.Show()
    app.MainLoop()

I tried something like this but it did not work. didnt show up on my frame at all so im completely lost.

class MyListCtrl(wx.ListCtrl):
    def __init__(self, paent, id, pos=wx.DefaultPosition,
                  size=wx.DefaultSize, style=0):
        wx.ListCtrl.__init__(self, parent, id. pos, size, style)
        
        self.Populate()
    def Populate(self):
        self.InsertColumn(0, "First Name")
        self.InsertColumn(1, "Last Name")
        self.InsertColumn(2, "Phone Number")

There's a typo in MyListCtrl should be parent not paent. You should also have a comma (,) not a period (.) between id and pos. Additionally, how are you adding the ListCtrl to your window? You should be using a sizer.

Refer to this example by ZZucker of a ListCtrl. It will give you some insight into a ListCtrl, it's events and placing it with a sizer.

Hope that helps

This article has been dead for over six months. Start a new discussion instead.