0

所以我可以自动将项目添加到列表中。我的问题现在围绕给定以下代码更新条目..当我选择该项目并尝试通过更新按钮更新它时,我得到的就是这个

Traceback (most recent call last):
File "C:\Users\Owner\Desktop\sad pROG\customer_app.py", line 97, in update_entry
entry = self.entrydict[self.list.GetItemText( long(self.index))]
KeyError: u'C0004'

用 GetItem 替换 te getItemText() 会产生此错误

Traceback (most recent call last):
  File "C:\Users\Owner\Desktop\sad pROG\customer_app.py", line 97, in update_entry
    entry = self.entrydict[self.list.GetItem( long(self.index))]
KeyError: <wx._controls.ListItem; proxy of <Swig Object of type 'wxListItem *' at    0x25b7d60> >





from wx import *
import Customer as c
class CustomerMain ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Customers", pos = wx.DefaultPosition, size = wx.Size( 517,486 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.Size( -1,-1 ), wx.DefaultSize )

        bSizer3 = wx.BoxSizer( wx.VERTICAL )


        # Create a customer
        self.cust = c.Customer()



        self.list = wx.ListCtrl( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize,wx.LC_REPORT )
        self.list.InsertColumn(0,"ID")
        self.list.InsertColumn(1,"First Name")
        self.list.InsertColumn(2,"Last Name")
        self.list.Bind(EVT_LIST_ITEM_SELECTED,self.GetSelectedItems)
        self.list.SetMinSize( wx.Size( 500,300 ) )
        self.list.SetMaxSize( wx.Size( 1000,800 ) )
        self.refresh_list(None)

        bSizer3.Add( self.list, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )

        bSizer4 = wx.BoxSizer( wx.HORIZONTAL )
        self.m_button20 = wx.Button( self, wx.ID_ANY, u"Add Customer", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_button20.Bind(wx.EVT_BUTTON, self.add_entry)
        bSizer4.Add( self.m_button20, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )

        self.m_button21 = wx.Button( self, wx.ID_ANY, u"Update Record", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_button21.Bind(wx.EVT_BUTTON, self.update_entry)
        bSizer4.Add( self.m_button21, 1, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )





        bSizer3.Add( bSizer4, 1, wx.EXPAND, 5 )

        self.SetSizer( bSizer3 )
        self.Layout()

        self.Centre( wx.BOTH )
        self.Show()

    def refresh_list(self, event):
        """Place the names of each CustomerEntry into the list"""

        index = 0
        self.entrydict = {}
        self.list.DeleteAllItems()
        for entry in self.cust.list_entries():
            self.list.InsertStringItem(index,entry.Cust_ID)
            self.list.SetStringItem(index,1,entry.F_Name)
            self.list.SetStringItem(index,2,entry.L_Name)
            self.entrydict[index] = entry
            index += 1


    def GetSelectedItems(self, event):
        """    Gets the selected items for the list control.
        Selection is returned as a list of selected indices,
        low to high.
        """
        selection = []
        self.index = self.list.GetFirstSelected()
        selection.append(self.index)
        while len(selection) != self.list.GetSelectedItemCount():
            self.index = self.list.GetNextSelected(self.index)
            selection.append(self.index)

        return self.index


    def add_entry(self, event):
       """Add a new entry to the Address cust"""
       # start out with blank, generic CustomerEntry
       entry = c.CustomerEntry("","","","","")
       self.endiag = CustomerDetail(self,entry)
       self.endiag.ShowModal()           



       self.cust.add_entry(entry.Cust_ID,
                            entry.F_Name,
                            entry.L_Name,
                            entry.Address,
                            entry.Cust_Stat )

       self.refresh_list(None)

    def update_entry(self, event):
       entry = self.entrydict[self.list.GetItemText( long(self.index))]
       self.endiag2 = CustomerDetail(self , entry)
       self.endiag2.ShowModal()

       self.cust.update_entry(entry.Cust_ID,
                            entry.F_Name,
                            entry.L_Name,
                            entry.Address,
                            entry.Cust_Stat )

       self.refresh_list(None)

def __del__( self ):
    pass


 class CustomerDetail ( wx.Dialog ):

    def __init__( self, parent, entry ):
            wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 690,500 ), style = wx.DEFAULT_DIALOG_STYLE )

            self.entry = entry
            self.SetSizeHintsSz( wx.Size( 500,500 ), wx.DefaultSize )

            bSizer6 = wx.BoxSizer( wx.VERTICAL )

            gSizer6 = wx.GridSizer( 2, 2, 0, 0 )

            self.m_staticText5 = wx.StaticText( self, wx.ID_ANY, u"ID", wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_staticText5.Wrap( -1 )
            gSizer6.Add( self.m_staticText5, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )

            self.Cust_ID = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
            self.Cust_ID.SetMinSize( wx.Size( 200,15 ) )
            self.Cust_ID.SetMaxSize( wx.Size( 500,15 ) )

            gSizer6.Add( self.Cust_ID, 1, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ALL|wx.EXPAND, 5 )

            self.m_staticText6 = wx.StaticText( self, wx.ID_ANY, u"First Name", wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_staticText6.Wrap( -1 )
            gSizer6.Add( self.m_staticText6, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )

            self.F_Name = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
            self.F_Name.SetMinSize( wx.Size( 200,15 ) )
            self.F_Name.SetMaxSize( wx.Size( 500,15 ) )

            gSizer6.Add( self.F_Name, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND, 5 )

            self.m_staticText7 = wx.StaticText( self, wx.ID_ANY, u"Last Name", wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_staticText7.Wrap( -1 )
            gSizer6.Add( self.m_staticText7, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )

            self.L_Name = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
            self.L_Name.SetMinSize( wx.Size( 200,15 ) )
            self.L_Name.SetMaxSize( wx.Size( 500,15 ) )

            gSizer6.Add( self.L_Name, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND|wx.ALIGN_CENTER_VERTICAL, 5 )

            self.m_staticText8 = wx.StaticText( self, wx.ID_ANY, u"Address", wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_staticText8.Wrap( -1 )
            gSizer6.Add( self.m_staticText8, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )

            self.Address = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
            self.Address.SetMinSize( wx.Size( 200,15 ) )
            self.Address.SetMaxSize( wx.Size( 500,15 ) )

            gSizer6.Add( self.Address, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 5 )

            self.m_staticText12 = wx.StaticText( self, wx.ID_ANY, u"Status", wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_staticText12.Wrap( -1 )
            gSizer6.Add( self.m_staticText12, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )


            stat_choices = ["Active","Inactive","Archived"]

            self.Cust_Stat = wx.ComboBox( self, wx.ID_ANY, u"", wx.DefaultPosition, wx.DefaultSize, stat_choices, 0 )
            self.Cust_Stat.SetMinSize( wx.Size( 200,15 ) )
            self.Cust_Stat.SetMaxSize( wx.Size( 300,15 ) )
            self.Cust_Stat.Bind(wx.EVT_COMBOBOX, self.OnSelect)
            gSizer6.Add( self.Cust_Stat, 0, wx.ALL, 5 )

            bSizer6.Add( gSizer6, 1, wx.EXPAND, 5 )

            bSizer13 = wx.BoxSizer( wx.HORIZONTAL )

            self.m_button24 = wx.Button( self, wx.ID_ANY, u"ok", wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_button24.Bind(wx.EVT_BUTTON, self.ok)

            bSizer13.Add( self.m_button24, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )

            self.m_button25 = wx.Button( self, wx.ID_ANY, u"cancel", wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_button25.Bind(wx.EVT_BUTTON, self.closeDial)

            bSizer13.Add( self.m_button25, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )

            bSizer6.Add( bSizer13, 1, wx.EXPAND, 5 )

            self.SetSizer( bSizer6 )
            self.Layout()

            self.Centre( wx.BOTH )

    def OnSelect (self, event):
        self.Cust_Stat.GetValue()



    def closeDial(self,event):

        self.Destroy()


    def ok(self, event):
        self.entry.set_details( str(self.Cust_ID.GetValue()),
                            str(self.F_Name.GetValue()),
                            str(self.L_Name.GetValue()),
                            str(self.Address.GetValue()),
                            str(self.Cust_Stat.GetValue()) )
        self.Destroy()


        def __del__( self ):
             pass                
def main():


ex = wx.App()
    CustomerMain(None)
    ex.MainLoop()    


if __name__ == '__main__':
    main()

不知道这个错误到底意味着什么。有任何想法吗?

4

2 回答 2

0

每当请求 dict() 对象(使用格式 a = adict[key])并且键不在字典中时,Python 都会引发 KeyError。

关于 Keyerror 的文档在这里。

所以最有可能的self.list.GetItemText( long(self.index))是,你的self.entrydict. 你能用print()语句检查吗?

于 2013-05-17T07:13:15.843 回答
0

1.获取ListCtrl中选中项的列表,里面有一个函数getListCtrlSelectionwx.lib.mixins.listctrl所以你的GetSelectedItems方法可以改写为

def GetSelectedItems(self, event):
    from wx.lib.mixins.listctrl import getListCtrlSelection
    self.index = getListCtrlSelection(self.list)
    return self.index

2.self.index是一个索引列表,ListCtrl.GetItemText需要一个索引作为输入,因此要获取所选项目的列表,您可以使用列表推导:

items = [self.list.GetItemText(i) for i in self.index]

然后,您可以self.entrydict根据所获得的内容从中获取条目self.list

entries = [self.entrydict[item] for item in items]
于 2013-05-17T07:43:20.113 回答