如何设置wxpython Grid顶部标题?

时间:2013-09-12 19:57:54

标签: python header grid wxpython

当我使用wxpython创建网格时,我将顶部列标题设为“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

但我想设置自定义列标题:

enter image description here

怎么做?

1 个答案:

答案 0 :(得分:11)

WxPython wiki告诉每个行和列如何具有可设置的标签(标题),这些标签可以包含任何文本以及它自己的其他布局。

列标题由命令

设置
SetColLabelValue(int col, const wxString& value)

将列号和所需的标签值作为参数。

This tutorial进一步表明,您案例中的完整示例代码如下:

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

当然,如果该日期在飞行中初始化,那么您必须获取它,例如从datetime对象中将这些行添加到您的代码中(取自this post):

# Add to imports
import datetime  

# fetch date and you can give this, not hard coded string value.
datetime.datetime.today()

要对您的图书重复多年/次,请查看以下内容:

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

代码来源here,而不是打印,相应地设置标签。此示例打印两个时间实例之间的每个日期。您的项目需要dateutil