1

I am trying to center a wx.grid.Grid in wxPython. I have tried every possible combination of ID parameter 1,0,-1. I have tried to add wx.ALIGN_CENTER_HORIZONTAL to many sizers. I have been working on this for a few days now, any help would be greatly appreciated.

My question is: "How do I center my wxgrid?"

Here is my Code:

main.py

import wx
from LogView import LogView

class MyFrame(wx.Frame):
    """ We simply derive a new class of Frame. """
    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self, parent, ID, title=title, size=(850,725))

        self.main = wx.Panel(self)

        self.nb = wx.Notebook(self.main, 1)
        self.logView = LogView(parent=self.nb, ID=-1)
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)

        self.nb.AddPage(self.logView, "Log")

        # Create sizers
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)

        # Adding Objects to mainSizer
        self.mainSizer.AddSpacer(10)
        self.mainSizer.Add(self.nb, 1, wx.ALL|wx.EXPAND)

        # Set main sizer
        self.main.SetAutoLayout(True)
        self.main.SetSizer(self.mainSizer)
        self.mainSizer.Fit(self.main)
        self.Layout()
        self.Centre(wx.BOTH)
        self.Show()

app = wx.App(False)
frame = MyFrame(None, -1, 'TFM UI')
app.MainLoop()

LogView.py

import wx
from GridTable import GridTable
class LogView(wx.Panel):
    def __init__(self, parent, ID):
        wx.Panel.__init__(self, parent, ID)
        self.sizer = wx.BoxSizer(wx.VERTICAL)

        self.gridPnl = wx.Panel(self)
        self.gridPnlSizer = wx.BoxSizer(wx.VERTICAL)

        self.grid = GridTable(self.gridPnl)

        self.gridPnlSizer.AddStretchSpacer()
        self.gridPnlSizer.Add(self.grid,-1,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
        self.gridPnlSizer.AddStretchSpacer()

        self.gridPnl.SetAutoLayout(True)
        self.gridPnl.SetSizer(self.gridPnlSizer)
        self.gridPnlSizer.Fit(self.gridPnl)

        self.sizer.AddSpacer(25)
        self.sizer.Add(self.gridPnl,-1,wx.EXPAND)


        self.SetAutoLayout(True)
        self.SetSizer(self.sizer)
        self.sizer.Fit(self)

GridTable.py

import wx
import wx.grid

class GridTable(wx.grid.Grid):
    def __init__(self, parent):
        #colDisplay should be a list

        wx.grid.Grid.__init__(self, parent)
        self.CreateGrid(100,3)
        for i in xrange(3):
            if i==0:
                self.SetColLabelValue(i, "Col"+str(i))
                self.SetColSize(i,85)
            else:
                self.SetColLabelValue(i, "Col"+str(i))
                self.SetColSize(i,150)


        for i in xrange(100):
            #clear grid
            self.SetRowLabelValue(i, str(i+1))
            for j in xrange(3):
                self.SetCellValue(i,j,"")
                if i==0:
                    self.SetCellBackgroundColour(i, j, wx.LIGHT_GREY)
                elif i%2==0:
                    self.SetCellBackgroundColour(i, j, wx.LIGHT_GREY)
                else:
                    self.SetCellTextColour(i, j, wx.BLACK)
                    self.SetCellBackgroundColour(i, j, wx.WHITE)

        for i in xrange(100):
            if i==0:
                for j in xrange(3):
                    if j == 0:
                        self.SetRowLabelValue(i, str(i+1))
                        self.SetCellValue(i,j,str("Col"+str(j)))
                        self.SetCellBackgroundColour(i, j, wx.LIGHT_GREY)
                    else:
                        self.SetCellValue(i,j,str("Col"+str(j)))
                        self.SetCellBackgroundColour(i, j, wx.LIGHT_GREY)
            elif i%2==0:
                for j in xrange(3):
                    if j == 0:
                        self.SetRowLabelValue(i, str(i+1))
                        self.SetCellValue(i,j,str("Col"+str(j)))
                        self.SetCellBackgroundColour(i, j, wx.LIGHT_GREY)
                    else:
                        self.SetCellValue(i,j,str("Col"+str(j)))
                        self.SetCellBackgroundColour(i, j, wx.LIGHT_GREY)
            else:
                for j in xrange(3):
                    if j == 0:
                        self.SetRowLabelValue(i, str(i+1))
                        self.SetCellValue(i,j,str("Col"+str(j)))
                        self.SetCellTextColour(i, j, wx.BLACK)
                        self.SetCellBackgroundColour(i, j, wx.WHITE)
                    else:
                        self.SetCellValue(i,j,str("Col"+str(j)))
                        self.SetCellTextColour(i, j, wx.BLACK)
                        self.SetCellBackgroundColour(i, j, wx.WHITE)
Jay Kominek
  • 8,674
  • 1
  • 34
  • 51
user908759
  • 1,355
  • 8
  • 26
  • 48
  • From running your code I don't understand what are you trying to achieve. How should the vertical centering look like? Or you meant horizontal? – Anna Jun 01 '15 at 02:00
  • Please take a look at my edit. I meant to say center horizontally... wx.ALIGN_CENTER_HORIZONTAL. I want the self.grid to be centered horizontally on the self.gridPnl. Thank you for your reply – user908759 Jun 02 '15 at 04:03

2 Answers2

0

Looking at this line from LogView.py:

self.gridPnlSizer.Add(self.grid,-1,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)

For starters, you're using a proportion of -1, which I think is undefined. It causes Python to crash on my machine, so... that's not great. The default is 0.

Second, you're telling the sizer to expand the widget. I guess if the widget takes up all the available space, it's certainly centered by one definition, but I don't think that's what you mean.

Finally, the stretch spacers are unnecessary. So replace that block of three lines with:

self.gridPnlSizer.Add(self.grid,flag=wx.ALIGN_CENTER_HORIZONTAL)

When I do that, I end up with:

enter image description here

Jay Kominek
  • 8,674
  • 1
  • 34
  • 51
  • The reason why I added the expand is to remove the horizontal scroll bar at the bottom of the grid. Your code centered the grid but added the horizontal scroll bar at the bottom of the grid – user908759 Jun 07 '15 at 17:41
  • Oh, so you also need the code to do some other stuff that you didn't mention? Great. – Jay Kominek Jun 07 '15 at 18:12
  • I also need the rows to remain numbered, but I didn't mention that either. Thanks for your help. – user908759 Jun 08 '15 at 03:15
0

self.my_grid.SetRowLabelSize(0) # hides the row # & horizontal scrollbar

You could use this and then add a column with your own row numbers.