首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用python的wx.grid,如何合并列?

使用python的wx.grid,如何合并列?
EN

Stack Overflow用户
提问于 2017-09-13 13:42:22
回答 5查看 2.3K关注 0票数 1

这是我到目前为止创建的桌子。

这是我希望它看起来像什么,与列合并。

我想把昨天的专栏和今天的专栏分别合并成三列。因此,昨天的股价将高于股票、波动率和现金。今天也是如此。我找到了一个叫做wx.grid.SetColSize(self, int col, int width)的函数,但是它没有任何效果。有人知道怎么做吗?

这也是我的代码。

代码语言:javascript
运行
复制
import wx
import wx.grid as gridlib

class MyForm(wx.Frame):
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, parent=None, title="Strategies' Allocations")
        self.panel = wx.Panel(self)

        button_refresh = wx.Button(self.panel, id=wx.ID_ANY, label='Refresh')
        button_refresh.Bind(wx.EVT_BUTTON, self.refresh)

        self.myGrid1 = gridlib.Grid(self.panel)
        self.myGrid1.CreateGrid(2, 6)

        self.myGrid1.SetRowLabelSize(60)
        self.myGrid1.SetRowLabelValue(0, "")
        self.myGrid1.SetRowLabelValue(1, "ABRVXX")

        for i in range(6):
            self.myGrid1.SetColSize(i, 60)

        self.myGrid1.SetColLabelValue(0, "")
        self.myGrid1.SetColLabelValue(1, "Yesterday")
        self.myGrid1.SetColLabelValue(2, "")
        self.myGrid1.SetColLabelValue(3, "")
        self.myGrid1.SetColLabelValue(4, "Today")
        self.myGrid1.SetColLabelValue(5, "")

        self.myGrid1.SetCellValue(0, 0, "Equity")
        self.myGrid1.SetCellValue(0, 1, "Volatility")
        self.myGrid1.SetCellValue(0, 2, "Cash")
        self.myGrid1.SetCellValue(0, 3, "Equity")
        self.myGrid1.SetCellValue(0, 4, "Volatility")
        self.myGrid1.SetCellValue(0, 5, "Cash")

        self.myGrid1.SetColLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
        self.myGrid1.SetDefaultCellAlignment( wx.ALIGN_CENTRE, wx.ALIGN_TOP )
        # ******************************* #

        self.myGrid2 = gridlib.Grid(self.panel)
        self.myGrid2.CreateGrid(2, 6)

        for i in range(6):
            self.myGrid2.SetColSize(i, 60)

        self.myGrid2.SetColLabelValue(0, "")
        self.myGrid2.SetColLabelValue(1, "Yesterday")
        self.myGrid2.SetColLabelValue(2, "")
        self.myGrid2.SetColLabelValue(3, "")
        self.myGrid2.SetColLabelValue(4, "Today")
        self.myGrid2.SetColLabelValue(5, "")

        self.myGrid2.SetCellValue(0, 0, "Treasury")
        self.myGrid2.SetCellValue(0, 1, "Volatility")
        self.myGrid2.SetCellValue(0, 2, "Cash")
        self.myGrid2.SetCellValue(0, 3, "Treasury")
        self.myGrid2.SetCellValue(0, 4, "Volatility")
        self.myGrid2.SetCellValue(0, 5, "Cash")

        self.myGrid2.SetRowLabelSize(60)
        self.myGrid2.SetRowLabelValue(0, "")
        self.myGrid2.SetRowLabelValue(1, "ABRXIV")

        self.myGrid2.SetColLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
        self.myGrid2.SetDefaultCellAlignment( wx.ALIGN_CENTRE, wx.ALIGN_TOP )
        # ****************************** #

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.myGrid1, 1, wx.TOP|wx.ALIGN_CENTRE, 2)
        sizer.Add(self.myGrid2, 1, wx.TOP|wx.ALIGN_CENTRE, 2)
        sizer.Add(button_refresh, 1, wx.RIGHT|wx.LEFT|wx.TOP|wx.BOTTOM|wx.EXPAND|wx.ALIGN_CENTRE, 50)

        self.panel.SetSizer(sizer)
        self.panel.SetSize((500,400))
        self.SetSize((500,400))
        self.panel.Layout()

    def refresh(self, event):
        pass

if __name__ == "__main__":
    app = wx.App()
    frame = MyForm().Show()
    app.MainLoop()
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2017-09-13 16:41:24

一种快速而肮脏的方法是转储列标签SetColLabelSize(0)并将标题添加为单元格。

然后调整单元格的跨度,只针对那些带有SetCellSize()的单元格。

下面我修改了myGrid1,但没有修改myGrid2。

代码语言:javascript
运行
复制
import wx
import wx.grid as gridlib

class MyForm(wx.Frame):
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, parent=None, title="Strategies' Allocations")
        self.panel = wx.Panel(self)

        button_refresh = wx.Button(self.panel, id=wx.ID_ANY, label='Refresh')
        button_refresh.Bind(wx.EVT_BUTTON, self.refresh)

        self.myGrid1 = gridlib.Grid(self.panel)
        self.myGrid1.CreateGrid(3, 6)

        self.myGrid1.SetRowLabelSize(80)
        self.myGrid1.SetRowLabelValue(0, "")
        self.myGrid1.SetRowLabelValue(1, "")
        self.myGrid1.SetRowLabelValue(2, "2")

        for i in range(6):
            self.myGrid1.SetColSize(i, 60)

#        self.myGrid1.SetColLabelValue(0, "")
#        self.myGrid1.SetColLabelValue(1, "Yesterday")
#        self.myGrid1.SetColLabelValue(2, "")
#        self.myGrid1.SetColLabelValue(3, "")
#        self.myGrid1.SetColLabelValue(4, "Today")
#        self.myGrid1.SetColLabelValue(5, "")

        self.myGrid1.SetColLabelSize(0)
        self.myGrid1.SetCellSize(0, 0, 1, 3)
        self.myGrid1.SetCellValue(0, 0, "Yesterday")
        self.myGrid1.SetCellSize(0, 3, 1, 3)
        self.myGrid1.SetCellValue(0, 3, "Today")

        self.myGrid1.SetCellValue(1, 0, "Equity")
        self.myGrid1.SetCellValue(1, 1, "Volatility")
        self.myGrid1.SetCellValue(1, 2, "Cash")
        self.myGrid1.SetCellValue(1, 3, "Equity")
        self.myGrid1.SetCellValue(1, 4, "Volatility")
        self.myGrid1.SetCellValue(1, 5, "Cash")

        self.myGrid1.SetColLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
        self.myGrid1.SetDefaultCellAlignment( wx.ALIGN_CENTRE, wx.ALIGN_TOP )
        # ******************************* #

        self.myGrid2 = gridlib.Grid(self.panel)
        self.myGrid2.CreateGrid(2, 6)

        for i in range(6):
            self.myGrid2.SetColSize(i, 60)

        self.myGrid2.SetColLabelValue(0, "")
        self.myGrid2.SetColLabelValue(1, "Yesterday")
        self.myGrid2.SetColLabelValue(2, "")
        self.myGrid2.SetColLabelValue(3, "")
        self.myGrid2.SetColLabelValue(4, "Today")
        self.myGrid2.SetColLabelValue(5, "")

        self.myGrid2.SetCellValue(0, 0, "Treasury")
        self.myGrid2.SetCellValue(0, 1, "Volatility")
        self.myGrid2.SetCellValue(0, 2, "Cash")
        self.myGrid2.SetCellValue(0, 3, "Treasury")
        self.myGrid2.SetCellValue(0, 4, "Volatility")
        self.myGrid2.SetCellValue(0, 5, "Cash")

        self.myGrid2.SetRowLabelSize(60)
        self.myGrid2.SetRowLabelValue(0, "")
        self.myGrid2.SetRowLabelValue(1, "2")

        self.myGrid2.SetColLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
        self.myGrid2.SetDefaultCellAlignment( wx.ALIGN_CENTRE, wx.ALIGN_TOP )
        # ****************************** #

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.myGrid1, 1, wx.TOP|wx.ALIGN_CENTRE, 2)
        sizer.Add(self.myGrid2, 1, wx.TOP|wx.ALIGN_CENTRE, 2)
        sizer.Add(button_refresh, 1, wx.RIGHT|wx.LEFT|wx.TOP|wx.BOTTOM|wx.EXPAND|wx.ALIGN_CENTRE, 50)

        self.panel.SetSizer(sizer)
        self.panel.SetSize((500,400))
        self.SetSize((500,400))
        self.panel.Layout()

    def refresh(self, event):
        pass

if __name__ == "__main__":
    app = wx.App()
    frame = MyForm().Show()
    app.MainLoop()

票数 2
EN

Stack Overflow用户

发布于 2017-09-13 14:57:37

这并不是特别简单,但我认为您应该通过定义从wxGridCellAttrProvider派生的类并重写它的GetColumnHeaderRenderer()方法来实现您想要合并的列的“不做什么”wxGridColumnHeaderRenderer,以及其他列的标准呈现器(由基类GetColumnHeaderRenderer()返回)。然后,只需使用表对象上的自定义属性提供程序对象调用SetAttrProvider()

票数 2
EN

Stack Overflow用户

发布于 2017-09-13 17:17:14

看看GridLabelRenderer示例中的wxPython示例。它是基于wx.lib.mixins.gridlabelrenderer中的类为网格行和列绘制自定义标签的示例。有了这些类,就可以很容易地绘制任何您想要的标签。您只需重写适当的绘制方法即可。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46199205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档