2017-09-13 17 views
0

これまでに作成した表は次のとおりです。ここでpythonのwx.gridを使って、どのようにして列をマージできますか?

enter image description here

列がマージされて、私は、それは次のようになりたいものです。

enter image description here

私は3つの列の各列全体に昨日と今日を統合したいと思います。だから昨日は株式、ボラティリティ、現金を上回るだろう。今日も同様です。 wx.grid.SetColSize(self, int col, int width)という1つの機能が見つかりましたが、何の効果もありませんでした。誰もこれを行う方法を知っていますか?

ここに私のコードもあります。

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

ないあなたが何を意味するか確認してください。グリッド内のデータを使って探しているものをいくつかスクリーンショットで表示できますか? – Igor

+0

@Igor私は何を望むかを示すスクリーンショットを追加しました。 –

答えて

2

列ラベルSetColLabelSize(0)をダンプして見出しをセルとして追加するのが速く汚れた方法です。
次に、これらのセルのセルスパンをSetCellSize()に調整します。
以下、myGrid1は変更しましたが、myGrid2は変更していません。

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

enter image description here

+0

私がこれをやろうとしていることは、仕事を終わらせるためです。助けてくれてありがとう。 –

+0

@AlexF両方の答えを編集した後、調整済みの画像を追加しました。それでもプライバシー問題がある場合は、自由に編集し直してください。 –

+0

これは素晴らしいです。まだそれを必要とする人のために非常に役立つが、プライバシーを維持する。収容していただきありがとうございます。 –

1

これは特に簡単になるだろうされていませんが、私はあなたがwxGridCellAttrProviderからクラスの導出を定義し、「何もしない」を返すようにそのGetColumnHeaderRenderer()メソッドをオーバーライドすることで、あなたが望むものをしたいの列のwxGridColumnHeaderRendererを実現すべきだと思います他のものに対しては標準レンダラー(基本クラスGetColumnHeaderRenderer()によって返されます)を使用します。次に、テーブルオブジェクトのカスタムアトリビュートプロバイダオブジェクトでSetAttrProvider()を呼び出すだけです。

2

wxPythonのデモでGridLabelRendererサンプルを見てみましょう。 wx.lib.mixins.gridlabelrendererのクラスに基づいて、グリッドの行と列のカスタムラベルを描画する例です。これらのクラスを使用すると、必要に応じてラベルを描画するのがかなり簡単になります。適切なDrawメソッドをオーバーライドするだけです。

0

@RobinDunnで提案されているように、GridLabelRendererを使用したバージョン(種類の)があります。これは最初に見た目ほど複雑ではありません。
しかしそれには2つの癖があります。
rect.leftrect.rightを保存するのに、lrを使用することに注意してください。
rect.leftを調整すると、right.rightも同様に調整されるため、このようにしています。
rectにはサイズがあるため、1つの要素を調整するともう一方が補正されるため、これが原因であると想定します。
ブランクの列見出しを宣言する必要があります。そうでない場合は、標準の「A、B、C、D .....」の順序に戻ります。

import wx 
import wx.grid as grid 
import wx.lib.mixins.gridlabelrenderer as glr 
#---------------------------------------------------------------------- 
class MyGrid(grid.Grid, glr.GridWithLabelRenderersMixin): 
    def __init__(self, *args, **kw): 
     grid.Grid.__init__(self, *args, **kw) 
     glr.GridWithLabelRenderersMixin.__init__(self) 

class TextLabelRenderer(glr.GridLabelRenderer): 
    def __init__(self, text, colspan,bgcolour=None): 
     self.text = text 
     self.colspan = colspan 
     if bgcolour is not None: 
      self.bgcolour = bgcolour 
     else: 
      self.bgcolour = "white" 

    def Draw(self, grid, dc, rect, col): 
     if self.colspan == 0: 
      rect.SetSize((0,0)) 
     if self.colspan > 1: 
      add_cols = self.colspan - 1 
      l = rect.left 
      r = rect.right + ((rect.Size.x -1) * add_cols) 
      rect.left = l 
      rect.right = r 
     dc.SetBrush(wx.Brush(self.bgcolour)) 
     dc.SetPen(wx.TRANSPARENT_PEN) 
     dc.DrawRectangleRect(rect) 
     hAlign, vAlign = grid.GetColLabelAlignment() 
     text = self.text 
     if self.colspan != 0: 
      self.DrawBorder(grid, dc, rect) 
     self.DrawText(grid, dc, rect, text, hAlign, vAlign) 

class TestPanel(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, parent=None, size=(800,300)) 
     ROWS = 6 
     COLS = 11 
     g = MyGrid(self, size=(100,100)) 
     g.CreateGrid(ROWS, COLS) 
     g.SetColLabelRenderer(0, TextLabelRenderer('Contract',1,"lightblue")) 
     g.SetColLabelRenderer(1, TextLabelRenderer('Yesterday',3,"lightgreen")) 
     g.SetColLabelRenderer(2, TextLabelRenderer('',0)) 
     g.SetColLabelRenderer(3, TextLabelRenderer('',0)) 
     g.SetColLabelRenderer(4, TextLabelRenderer('Today',4,"green")) 
     g.SetColLabelRenderer(5, TextLabelRenderer('',0)) 
     g.SetColLabelRenderer(6, TextLabelRenderer('',0)) 
     g.SetColLabelRenderer(7, TextLabelRenderer('',0)) 
     g.SetColLabelRenderer(8, TextLabelRenderer('Other',1,"gold")) 
     # g.SetColLabelRenderer(9, TextLabelRenderer('',0)) 
     g.SetRowLabelSize(0) 
     g.SetCellValue(0, 1, "Equity") 
     g.SetCellValue(0, 2, "Volatility") 
     g.SetCellValue(0, 3, "Cash") 
     g.SetCellValue(0, 4, "Equity") 
     g.SetCellValue(0, 5, "Volatility") 
     g.SetCellValue(0, 6, "Cash") 
     g.SetCellValue(1, 0, "2") 
     g.SetCellValue(1, 1, "1500") 
     g.SetCellValue(1, 2, "23") 
     g.SetCellValue(1, 3, "2300") 
     g.SetCellValue(1, 4, "1400") 
     g.SetCellValue(1, 5, "26") 
     g.SetCellValue(1, 6, "2400") 
     g.SetColLabelAlignment(wx.ALIGN_CENTRE, wx.ALIGN_CENTRE) 
     g.SetDefaultCellAlignment(wx.ALIGN_CENTRE, wx.ALIGN_TOP) 

     g1 = MyGrid(self, size=(100,100)) 
     g1.CreateGrid(ROWS, COLS) 
     g1.SetColLabelRenderer(0, TextLabelRenderer('Contract',1,"lightblue")) 
     g1.SetColLabelRenderer(1, TextLabelRenderer('Yesterday',3,"lightgreen")) 
     g1.SetColLabelRenderer(2, TextLabelRenderer('',0)) 
     g1.SetColLabelRenderer(3, TextLabelRenderer('',0)) 
     g1.SetColLabelRenderer(4, TextLabelRenderer('Today',3,"green")) 
     g1.SetColLabelRenderer(5, TextLabelRenderer('',0)) 
     g1.SetColLabelRenderer(6, TextLabelRenderer('',0)) 
     g1.SetColLabelRenderer(7, TextLabelRenderer('Other',2,"gold")) 
     g1.SetColLabelRenderer(8, TextLabelRenderer('',0)) 
     g1.SetRowLabelSize(0) 
     g1.SetCellValue(0, 1, "Equity") 
     g1.SetCellValue(0, 2, "Volatility") 
     g1.SetCellValue(0, 3, "Cash") 
     g1.SetCellValue(0, 4, "Equity") 
     g1.SetCellValue(0, 5, "Volatility") 
     g1.SetCellValue(0, 6, "Cash") 
     g1.SetCellValue(1, 0, "2") 
     g1.SetCellValue(1, 1, "500") 
     g1.SetCellValue(1, 2, "23") 
     g1.SetCellValue(1, 3, "12300") 
     g1.SetCellValue(1, 4, "11400") 
     g1.SetCellValue(1, 5, "26") 
     g1.SetCellValue(1, 6, "12400") 
     g1.SetColLabelAlignment(wx.ALIGN_CENTRE, wx.ALIGN_CENTRE) 
     g1.SetDefaultCellAlignment(wx.ALIGN_CENTRE, wx.ALIGN_TOP) 
     self.Sizer = wx.BoxSizer(wx.VERTICAL) 
     self.Sizer.Add(g, 1, wx.EXPAND) 
     self.Sizer.Add(g1, 1, wx.EXPAND) 
     self.Show() 
#---------------------------------------------------------------------- 
if __name__ == "__main__": 
    app = wx.App() 
    frame = TestPanel() 
    app.MainLoop() 

enter image description here

関連する問題