wx.listctrl 创建出如下格式(format)的的清单:
报告视图 (report view)
清单视图 (list view)
图标视图 (icon view)
专题教程:wxpython中文教程
例子,键入(key in)各州名以及各自首府, 并将它们添加到清单部件,使用报告视图.
#!/usr/bin/python
#coding=utf-8
#capitals.py
import wx
class MyDialog(wx.Dialog):
def __init__(self, parent, id, title):
wx.Dialog.__init__(self, parent, id, title, size=(600, 500),
style=wx.DEFAULT_DIALOG_STYLE)
hbox = wx.BoxSizer(wx.HORIZONTAL)
vbox1 = wx.BoxSizer(wx.VERTICAL)
vbox2 = wx.BoxSizer(wx.VERTICAL)
vbox3 = wx.GridSizer(2, 2, 0, 0)
vbox4 = wx.BoxSizer(wx.VERTICAL)
pnl1 = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
pnl2 = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
self.lc = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
self.lc.InsertColumn(0, '国 家')
self.lc.InsertColumn(1, '首 都')
self.lc.SetColumnWidth(0, 140)
self.lc.SetColumnWidth(1, 153)
vbox1.Add(pnl1, 1, wx.EXPAND | wx.ALL, 3)
vbox1.Add(pnl2, 1, wx.EXPAND | wx.ALL, 3)
vbox2.Add(self.lc, 1, wx.EXPAND | wx.ALL, 3)
self.tc1 = wx.TextCtrl(pnl1, -1)
self.tc2 = wx.TextCtrl(pnl1, -1)
vbox3.AddMany([(wx.StaticText(pnl1, -1, '国 家'), 0,
wx.ALIGN_CENTER),
(self.tc1, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL),
(wx.StaticText(pnl1, -1, '首 都'), 0,
wx.ALIGN_CENTER_HORIZONTAL),
(self.tc2, 0)])
pnl1.SetSizer(vbox3)
vbox4.Add(wx.Button(pnl2, 10, '添 加'), 0, wx.ALIGN_CENTER|wx.TOP,
45)
vbox4.Add(wx.Button(pnl2, 11, '移 除'), 0, wx.ALIGN_CENTER|wx.TOP,
15)
vbox4.Add(wx.Button(pnl2, 12, '清 空'), 0, wx.ALIGN_CENTER|wx.TOP,
15)
vbox4.Add(wx.Button(pnl2, 13, '关 闭'), 0, wx.ALIGN_CENTER|wx.TOP,
15)
pnl2.SetSizer(vbox4)
self.Bind(wx.EVT_BUTTON, self.OnAdd, id=10)
self.Bind(wx.EVT_BUTTON, self.OnRemove, id=11)
self.Bind(wx.EVT_BUTTON, self.OnClear, id=12)
self.Bind(wx.EVT_BUTTON, self.OnClose, id=13)
hbox.Add(vbox1, 1, wx.EXPAND)
hbox.Add(vbox2, 1, wx.EXPAND)
self.SetSizer(hbox)
def OnAdd(self, event):
if not self.tc1.GetValue() or not self.tc2.GetValue():
return
num_items = self.lc.GetItemCount()
self.lc.InsertStringItem(num_items, self.tc1.GetValue())
self.lc.SetStringItem(num_items, 1, self.tc2.GetValue())
self.tc1.Clear()
self.tc2.Clear()
def OnRemove(self, event):
index = self.lc.GetFocusedItem()
self.lc.DeleteItem(index)
def OnClose(self, event):
self.Close()
def OnClear(self, event):
self.lc.DeleteAllItems()
class MyApp(wx.App):
def OnInit(self):
dlg = MyDialog(None, -1, 'capitals.py')
dlg.ShowModal()
dlg.Destroy()
return True
app = MyApp(0)
app.MainLoop()
如图: