[Python] wxpython生成的一个简单数据表格操作界面 →→→→→进入此内容的聊天室

来自 , 2019-04-10, 写在 Python, 查看 108 次.
URL http://www.code666.cn/view/91a4d5c9
  1. import wx, wx.grid
  2.  
  3. class GridData(wx.grid.PyGridTableBase):
  4.     _cols = "a b c".split()
  5.     _data = [
  6.         "1 2 3".split(),
  7.         "4 5 6".split(),
  8.         "7 8 9".split()
  9.     ]
  10.     _highlighted = set()
  11.  
  12.     def GetColLabelValue(self, col):
  13.         return self._cols[col]
  14.  
  15.     def GetNumberRows(self):
  16.         return len(self._data)
  17.  
  18.     def GetNumberCols(self):
  19.         return len(self._cols)
  20.  
  21.     def GetValue(self, row, col):
  22.         return self._data[row][col]
  23.  
  24.     def SetValue(self, row, col, val):
  25.         self._data[row][col] = val
  26.  
  27.     def GetAttr(self, row, col, kind):
  28.         attr = wx.grid.GridCellAttr()
  29.         attr.SetBackgroundColour(wx.GREEN if row in self._highlighted else wx.WHITE)
  30.         return attr
  31.  
  32.     def set_value(self, row, col, val):
  33.         self._highlighted.add(row)
  34.         self.SetValue(row, col, val)
  35.  
  36. class Test(wx.Frame):
  37.     def __init__(self):
  38.         wx.Frame.__init__(self, None)
  39.  
  40.         self.data = GridData()
  41.         self.grid = wx.grid.Grid(self)
  42.         self.grid.SetTable(self.data)
  43.  
  44.         btn = wx.Button(self, label="set a2 to x")
  45.         btn.Bind(wx.EVT_BUTTON, self.OnTest)
  46.  
  47.         self.Sizer = wx.BoxSizer(wx.VERTICAL)
  48.         self.Sizer.Add(self.grid, 1, wx.EXPAND)
  49.         self.Sizer.Add(btn, 0, wx.EXPAND)
  50.  
  51.     def OnTest(self, event):
  52.         self.data.set_value(1, 0, "x")
  53.         self.grid.Refresh()
  54.  
  55.  
  56. app = wx.PySimpleApp()
  57. app.TopWindow = Test()
  58. app.TopWindow.Show()
  59. app.MainLoop()
  60. #//python/2327

回复 "wxpython生成的一个简单数据表格操作界面"

这儿你可以回复上面这条便签

captcha