Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set wxpython Grid top headers?

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()

enter image description here

But I'd like to set custom column headers:

enter image description here

How to do this?

like image 698
alwbtc Avatar asked Dec 11 '22 11:12

alwbtc


1 Answers

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.

like image 61
mico Avatar answered Dec 23 '22 17:12

mico