使用grid.setcell对齐在python中

2024-04-19 00:08:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我在wxpython中有一个网格,我想在单元格的中心显示所有文本。在

据我所知这是可行的

grid.SetCellAlignment(0, 0, wx.ALIGN_CENTRE, wx.ALIGN_CENTRE)

但正如您所见,这是单元的局部属性。我怎么能对整个网格这么做呢?在


Tags: 文本网格属性wxpython局部中心grid单元
3条回答

尝试使用StaticText类来对齐文本,而不是使用SetCellAlignment。在

您应该从创建继承类wx.grid.grid,并添加名为SetGridAlignment的方法。如下所示:

import wx
import wx.grid as wxgrid

class Grid(wxgrid.Grid):
    def __init__(self,*args,**kwargs):
        wxgrid.Grid.__init__(self,*args,**kwargs)

    def SetGridAlignment(self,halign,valign):
        """
        This method aligns all cells of the grid.

        halign :   Horizontal alignment
        valign :   Vertical alignment
        """
        nrows=self.GetNumberRows()
        ncols=self.GetNumberCols()
        for ii in range(nrows):
            for jj in range(ncols):
                self.SetCellAlignment(ii,jj,halign,valign)


if __name__=='__main__':
    app = wx.App()
    frame = wx.Frame(None, -1, u"Simple Grid", size=(300,200))
    gr = Grid(frame, -1)
    gr.CreateGrid(10,2)
    gr.SetGridAlignment(wx.ALIGN_CENTRE,wx.ALIGN_CENTRE)
    frame.Show()
    app.MainLoop()
grid.SetDefaultCellAlignment(wx.ALIGN_CENTRE, wx.ALIGN_CENTRE)

相关问题 更多 >