]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-13/list_report_etc.py
   2 import sys
, glob
, random
 
   5 class DemoFrame(wx
.Frame
): 
   7         wx
.Frame
.__init
__(self
, None, -1, 
   8                           "Other wx.ListCtrl Stuff", 
  16     def MakeListCtrl(self
, otherflags
=0): 
  17         # if we already have a listctrl then get rid of it 
  22             otherflags |
= wx
.LC_EDIT_LABELS
 
  24         # load some images into an image list 
  25         il 
= wx
.ImageList(16,16, True) 
  26         for name 
in glob
.glob("smicon??.png"): 
  27             bmp 
= wx
.Bitmap(name
, wx
.BITMAP_TYPE_PNG
) 
  30         # create the list control 
  31         self
.list = wx
.ListCtrl(self
, -1, style
=wx
.LC_REPORT|otherflags
) 
  33         # assign the image list to it 
  34         self
.list.AssignImageList(il
, wx
.IMAGE_LIST_SMALL
) 
  37         for col
, text 
in enumerate(data
.columns
): 
  38             self
.list.InsertColumn(col
, text
) 
  41         for row
, item 
in enumerate(data
.rows
): 
  42             index 
= self
.list.InsertStringItem(sys
.maxint
, item
[0]) 
  43             for col
, text 
in enumerate(item
[1:]): 
  44                 self
.list.SetStringItem(index
, col
+1, text
) 
  46             # give each item a random image 
  47             img 
= random
.randint(0, il_max
) 
  48             self
.list.SetItemImage(index
, img
, img
) 
  50             # set the data value for each item to be its position in 
  52             self
.list.SetItemData(index
, row
) 
  55         # set the width of the columns in various ways 
  56         self
.list.SetColumnWidth(0, 120) 
  57         self
.list.SetColumnWidth(1, wx
.LIST_AUTOSIZE
) 
  58         self
.list.SetColumnWidth(2, wx
.LIST_AUTOSIZE
) 
  59         self
.list.SetColumnWidth(3, wx
.LIST_AUTOSIZE_USEHEADER
) 
  61         # bind some interesting events 
  62         self
.Bind(wx
.EVT_LIST_ITEM_SELECTED
, self
.OnItemSelected
, self
.list) 
  63         self
.Bind(wx
.EVT_LIST_ITEM_DESELECTED
, self
.OnItemDeselected
, self
.list) 
  64         self
.Bind(wx
.EVT_LIST_ITEM_ACTIVATED
, self
.OnItemActivated
, self
.list) 
  66         # in case we are recreating the list tickle the frame a bit so 
  67         # it will redo the layout 
  74         item 
= menu
.Append(-1, "E&xit\tAlt-X") 
  75         self
.Bind(wx
.EVT_MENU
, self
.OnExit
, item
) 
  76         mbar
.Append(menu
, "&File") 
  79         item 
= menu
.Append(-1, "Sort ascending") 
  80         self
.Bind(wx
.EVT_MENU
, self
.OnSortAscending
, item
)         
  81         item 
= menu
.Append(-1, "Sort descending") 
  82         self
.Bind(wx
.EVT_MENU
, self
.OnSortDescending
, item
) 
  83         item 
= menu
.Append(-1, "Sort by submitter") 
  84         self
.Bind(wx
.EVT_MENU
, self
.OnSortBySubmitter
, item
) 
  86         menu
.AppendSeparator() 
  87         item 
= menu
.Append(-1, "Show selected") 
  88         self
.Bind(wx
.EVT_MENU
, self
.OnShowSelected
, item
)         
  89         item 
= menu
.Append(-1, "Select all") 
  90         self
.Bind(wx
.EVT_MENU
, self
.OnSelectAll
, item
) 
  91         item 
= menu
.Append(-1, "Select none") 
  92         self
.Bind(wx
.EVT_MENU
, self
.OnSelectNone
, item
) 
  94         menu
.AppendSeparator() 
  95         item 
= menu
.Append(-1, "Set item text colour") 
  96         self
.Bind(wx
.EVT_MENU
, self
.OnSetTextColour
, item
) 
  97         item 
= menu
.Append(-1, "Set item background colour") 
  98         self
.Bind(wx
.EVT_MENU
, self
.OnSetBGColour
, item
) 
 100         menu
.AppendSeparator() 
 101         item 
= menu
.Append(-1, "Enable item editing", kind
=wx
.ITEM_CHECK
) 
 102         self
.Bind(wx
.EVT_MENU
, self
.OnEnableEditing
, item
) 
 103         item 
= menu
.Append(-1, "Edit current item") 
 104         self
.Bind(wx
.EVT_MENU
, self
.OnEditItem
, item
) 
 105         mbar
.Append(menu
, "&Demo") 
 107         self
.SetMenuBar(mbar
) 
 111     def OnExit(self
, evt
): 
 115     def OnItemSelected(self
, evt
): 
 117         print "Item selected:", item
.GetText() 
 119     def OnItemDeselected(self
, evt
): 
 121         print "Item deselected:", item
.GetText() 
 123     def OnItemActivated(self
, evt
):  
 125         print "Item activated:", item
.GetText() 
 127     def OnSortAscending(self
, evt
): 
 128         # recreate the listctrl with a sort style 
 129         self
.MakeListCtrl(wx
.LC_SORT_ASCENDING
) 
 131     def OnSortDescending(self
, evt
): 
 132         # recreate the listctrl with a sort style 
 133         self
.MakeListCtrl(wx
.LC_SORT_DESCENDING
) 
 135     def OnSortBySubmitter(self
, evt
): 
 136         def compare_func(row1
, row2
): 
 137             # compare the values in the 4th col of the data 
 138             val1 
= data
.rows
[row1
][3] 
 139             val2 
= data
.rows
[row2
][3] 
 140             if val1 
< val2
: return -1 
 141             if val1 
> val2
: return 1 
 144         self
.list.SortItems(compare_func
) 
 148     def OnShowSelected(self
, evt
): 
 149         print "These items are selected:" 
 150         index 
= self
.list.GetFirstSelected() 
 155             item 
= self
.list.GetItem(index
) 
 156             print "\t%s" % item
.GetText() 
 157             index 
= self
.list.GetNextSelected(index
) 
 159     def OnSelectAll(self
, evt
): 
 160         for index 
in range(self
.list.GetItemCount()): 
 161             self
.list.Select(index
, True) 
 163     def OnSelectNone(self
, evt
): 
 164         index 
= self
.list.GetFirstSelected() 
 166             self
.list.Select(index
, False) 
 167             index 
= self
.list.GetNextSelected(index
) 
 170     def OnSetTextColour(self
, evt
): 
 171         dlg 
= wx
.ColourDialog(self
) 
 172         if dlg
.ShowModal() == wx
.ID_OK
: 
 173             colour 
= dlg
.GetColourData().GetColour() 
 174             index 
= self
.list.GetFirstSelected() 
 176                 self
.list.SetItemTextColour(index
, colour
) 
 177                 index 
= self
.list.GetNextSelected(index
) 
 180     def OnSetBGColour(self
, evt
): 
 181         dlg 
= wx
.ColourDialog(self
) 
 182         if dlg
.ShowModal() == wx
.ID_OK
: 
 183             colour 
= dlg
.GetColourData().GetColour() 
 184             index 
= self
.list.GetFirstSelected() 
 186                 self
.list.SetItemBackgroundColour(index
, colour
) 
 187                 index 
= self
.list.GetNextSelected(index
) 
 191     def OnEnableEditing(self
, evt
): 
 192         self
.editable 
= evt
.IsChecked() 
 195     def OnEditItem(self
, evt
): 
 196         index 
= self
.list.GetFirstSelected() 
 198             self
.list.EditLabel(index
) 
 201 class DemoApp(wx
.App
): 
 204         self
.SetTopWindow(frame
) 
 205         print "Program output appears here..." 
 209 app 
= DemoApp(redirect
=True)