我正在尝试将 awx.grid.Grid
在 wxPython 中居中。我已经尝试了 ID 参数 1,0,-1 的所有可能组合。我试图添加wx.ALIGN_CENTER_HORIZONTAL
到许多尺寸器。我已经为此工作了几天,任何帮助将不胜感激。
我的问题是:“如何使我的 wxgrid 居中?”
这是我的代码:
主文件
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()
日志视图.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)
网格表.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)