如何在wx.grid(wxpython)中将整个行/列设置为只读?

时间:2013-09-12 10:54:24

标签: python wxpython row readonly

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

import wx.grid as gridlib

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

2 个答案:

答案 0 :(得分:4)

您必须使用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()

此代码将第一行设为只读。

答案 1 :(得分:2)

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

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

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

或者,甚至更好:

myGrid.EnableEditing(False)