所以我得到了自动添加项目到列表的东西。我的问题现在围绕更新条目旋转,给定代码如下...当我选择项目并尝试更新它通过更新按钮更新按钮所有我得到的是
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'
替换te getItemText(),GetItem会产生此错误
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()
Dunno世界上的错误意味着什么。任何想法?
答案 0 :(得分:0)
每当请求dict()对象时,Python就会引发KeyError(使用 格式a = adict [key]),密钥不在字典中。
所以最有可能的是,self.list.GetItemText( long(self.index))
是您self.entrydict
中的关键。你能用print()
陈述检查吗?
答案 1 :(得分:0)
1.要获取ListCtrl中所选项目的列表,getListCtrlSelection
中有一个函数wx.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]