When I create a grid with wxpython, I get top column headers as "A", "B", "C"...
import wx
import wx.grid as gridlib
########################################################################
class MyForm(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, parent=None, title="A Simple Grid")
panel = wx.Panel(self)
myGrid = gridlib.Grid(panel)
myGrid.CreateGrid(12, 8)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(myGrid, 1, wx.EXPAND)
panel.SetSizer(sizer)
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyForm().Show()
app.MainLoop()
But I'd like to set custom column headers:
How to do this?
WxPython wiki tells, how each row and column have their settable label (header), that can have what ever text and what ever other layout of its own.
The column header is set by command
SetColLabelValue(int col, const wxString& value)
that takes column number and the desired label value as arguments.
This tutorial further shows, that the complete example code in your case would be like this:
import wx
import wx.grid as gridlib
########################################################################
class MyForm(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, parent=None, title="A Simple Grid")
panel = wx.Panel(self)
myGrid = gridlib.Grid(panel)
myGrid.CreateGrid(12, 8)
myGrid.SetColLabelValue(0, "ID")
myGrid.SetColLabelValue(1, "Name")
myGrid.SetColLabelValue(2, "Lastname")
myGrid.SetColLabelValue(3, "01-Jan-13")
# etc. etc.
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(myGrid, 1, wx.EXPAND)
panel.SetSizer(sizer)
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyForm().Show()
app.MainLoop()
Of course, if that date is initialized on fly, then you have to fetch it e.g. from a datetime
object adding these lines to your code (taken from this post):
# Add to imports
import datetime
# fetch date and you can give this, not hard coded string value.
datetime.datetime.today()
To iterate years / times on your titles, please find the following as useful:
from dateutil import rrule
from datetime import datetime, timedelta
now = datetime.now()
tenYearsLater = now + timedelta(years=10)
for dt in rrule.rrule(rrule.DAILY, dtstart=now, until=tenYearsLater):
print dt
Code source here, instead of print, set the labels correspondently. This sample printed every date between two time instances. You need dateutil
in your project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With