4

以下代码将单元格设置为只读,但如何在 wx.grid 中将整个行/列(例如第 3 列)设置为只读?

import wx.grid as gridlib

myGrid = gridlib.Grid(panel)
myGrid.SetReadOnly(3, 3, True)
4

2 回答 2

5

您必须使用 GridCellAttr 来执行此操作。这是一个简单的例子:

import wx
import wx.grid as gridlib

########################################################################
class MyForm(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, parent=None, title="A Simple Grid")
        panel = wx.Panel(self)

        myGrid = gridlib.Grid(panel)
        myGrid.CreateGrid(12, 8)

        # get the cell attribute for the top left row
        editor = myGrid.GetCellEditor(0,0)
        attr = gridlib.GridCellAttr()
        attr.SetReadOnly(True)
        myGrid.SetRowAttr(0, attr)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(myGrid, 1, wx.EXPAND)
        panel.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm().Show()
    app.MainLoop()

此代码将使第一行只读。

于 2013-09-12T13:34:23.230 回答
2

未经测试,但你不能做这样的事情:

rows = myGrid.GetNumberRows()
cols = myGrid.GetNumberCols()

for r, c in zip(enumerate(rows), enumerate(cols)):
        myGrid.SetReadOnly(r, c, True)

或者,甚至更好:

myGrid.EnableEditing(False)

于 2013-09-12T12:47:58.140 回答