设法修复了我的代码。现在的事情是我正在尝试调用一个事件以使用 getIndex() 来提取当前选定的 listctrl 项的索引。我知道我需要为事件传递一个变量,但代码一直抱怨我必须声明它..或者如果我实际上在 evt_itemselected 绑定中使用事件..它说我没有这样的变量命名事件....可以'似乎找不到任何此类情况的示例...可以请赐教吗?
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.SelItem())
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 SelItem (self, event):
"""""Extracts the index of the currently selected listctrl item"""
currentItem = self.list.GetIndex()
return currentItem
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.GetItemData(self.selItem)]
self.endiag2 = CustomerDetail(self , entry)
self.endiag2.Show()
self.refresh_list(entry)
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()
如果名称== '主要': main()