我试图将wx.grid.Grid
集中在wxPython中。我已经尝试了ID参数1,0,-1的所有可能组合。我试图将wx.ALIGN_CENTER_HORIZONTAL
添加到许多sizer中。我已经在这几天工作,任何帮助将不胜感激。
我的问题是:"我如何集中我的wxgrid?"
这是我的代码:
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)
答案 0 :(得分:0)
从LogView.py看这一行:
self.gridPnlSizer.Add(self.grid,-1,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
首先,你认为using a proportion of -1,我认为这是未定义的。它导致Python在我的机器上崩溃,所以......那不是很好。默认值为0.
其次,您告诉sizer扩展小部件。我想如果小部件占用了所有可用空间,它肯定以一个定义为中心,但我不认为这就是你的意思。
最后,拉伸垫片是不必要的。所以用:
替换三行的块self.gridPnlSizer.Add(self.grid,flag=wx.ALIGN_CENTER_HORIZONTAL)
当我这样做时,我最终得到: