Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # google groups: wxpython-users
- # topic replied to: Curious why this alignment has to be set up this way
- # http://www.blog.pythonlibrary.org/2011/01/04/wxpython-wx-listctrl-tips-and-tricks/
- import wx
- # you could use collections.OrderedDict
- data = {
- # map
- # sort_key, count, greek
- 5:('one', 'alpha'),
- 6:('two', 'beta'),
- 7:('three', 'gamma'),
- 8:('four', 'delta'), }
- class MainPanel(wx.Panel):
- __doc__ = wx.Panel.__doc__
- # Panel(parent, id=ID_ANY, pos=DefaultPosition, size=DefaultSize, style=TAB_TRAVERSAL, name=PanelNameStr)
- def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.TAB_TRAVERSAL, name='MainPanel'):
- #wx.Panel.__init__(self, parent, id, pos, size, style, name)
- super().__init__(parent, id, pos, size, style, name)
- # Add three buttons
- # Button(parent, id=ID_ANY, label=EmptyString, pos=DefaultPosition, size=DefaultSize, style=0, validator=DefaultValidator, name=ButtonNameStr)
- ok = wx.Button(self, id=wx.ID_OK, name='ok')
- cancel = wx.Button(self, id=wx.ID_CANCEL, name='cancel')
- exitb = wx.Button(self, id=wx.ID_EXIT, name='exit')
- # Put three buttons into a BoxSizer
- hsizer = wx.BoxSizer(wx.HORIZONTAL)
- hsizer.Add(ok, 0, wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL, 5)
- hsizer.Add(cancel, 0, wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL, 5)
- hsizer.Add(exitb, 0, wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL, 5)
- # ListCtrl(parent, id=ID_ANY, pos=DefaultPosition, size=DefaultSize, style=LC_ICON, validator=DefaultValidator, name=ListCtrlNameStr)
- self.lb = wx.ListCtrl(self, style = wx.LC_REPORT | wx.BORDER_SUNKEN | wx.LC_SINGLE_SEL, name='mylist')
- self.lb.InsertColumn(0, 'Count')
- self.lb.InsertColumn(1, 'Greek')
- # Fill the ListCtrl
- self.fillListCtrl()
- # Layout buttons and ListCtrl
- vsizer = wx.BoxSizer(wx.VERTICAL)
- vsizer.Add(hsizer, 0, wx.ALIGN_RIGHT | wx.ALL, 0)
- vsizer.Add(self.lb, 1, wx.EXPAND | wx.ALL, 5)
- self.SetSizer(vsizer)
- self.setListCtrlColumnWidths()
- # Event Bindings
- self.Bind(wx.EVT_SIZE, self.onSize)
- self.Bind(wx.EVT_BUTTON, self.onButton)
- self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.onItemSelected)
- def fillListCtrl(self, start=0, end=None):
- """NotImplemented start and end"""
- self.lb.DeleteAllItems()
- index = 0
- for key in sorted(data):
- label = data[key][0]
- greek = data[key][1]
- item = self.lb.InsertItem(index, label)
- self.lb.SetItem(index, 1, greek)
- # optional for sorting
- self.lb.SetItemData(index, key)
- index += 1
- def setListCtrlColumnWidths(self):
- """Set the listctrl column zero's width to the remaining
- width after column one's usage. Magic number 5 removed from
- width to prevent horizontal scrollbar."""
- second_column_width = self.lb.GetColumnWidth(1)
- first_column_width = self.lb.GetSize()[0]-5-second_column_width
- self.lb.SetColumnWidth(0, first_column_width)
- def SetStatusText(self, text='', number=0):
- top_parent = self.GetTopLevelParent()
- if top_parent.GetStatusBar():
- top_parent.SetStatusText(text, number)
- def onItemSelected(self, event):
- index = event.GetIndex()
- key = event.GetData()
- label = event.GetLabel()
- column_1_text = data[key][1]
- # OR
- column_1_item = self.lb.GetItem(index, 1)
- column_1_text = column_1_item.GetText()
- text = "index:{} sort_key:{} count:{} greek:{}".format(
- index, key, label, column_1_text)
- self.SetStatusText(text)
- def onSize(self, event):
- """If you like column zero of the listctrl to expand
- instead of the last column."""
- self.setListCtrlColumnWidths()
- event.Skip() # needed in this case
- def onButton(self, event):
- widget = event.GetEventObject()
- name = widget.GetName()
- self.SetStatusText(name + ' clicked')
- class MainFrame(wx.Frame):
- __doc__ = wx.Frame.__doc__
- # Frame(parent, id=ID_ANY, title=EmptyString, pos=DefaultPosition, size=DefaultSize, style=DEFAULT_FRAME_STYLE, name=FrameNameStr)
- def __init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE, name='MainFrame'):
- #wx.Panel.__init__(self, parent, id, pos, size, style, name)
- super().__init__(parent, id, title, pos, size, style, name)
- sizer = wx.BoxSizer(wx.VERTICAL)
- sizer.Add(MainPanel(self), 1, wx.EXPAND | wx.ALL, 0)
- self.SetSizer(sizer)
- self.CreateStatusBar()
- self.Show()
- if __name__ == "__main__":
- app = wx.App()
- frame = MainFrame(None, title='BoxSizer options')
- app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment