I still could not figure out why my code does not work. When press CTRL+ENTER, the program just activate the next cell. Please see code shown below. Also, how do I know the background color of the frame? I’d like to use it as the background color for the first column, instead of using wx.LIGHT_GREY. Thank you.
import wx
import wx.grid
import wx.lib.colourdb as wxcolor
class DocVarGridResult(object):
def init(self):
self.value = None
class DocVarGrid(wx.grid.Grid
):
def init(self, parent, lst):
cntRow = len(lst)
cntCol = len(lst[0])
wx.grid.Grid.init(self, parent, -1)
self.CreateGrid(cntRow,cntCol)
self.SetDefaultEditor(wx.grid.GridCellAutoWrapStringEditor())
for row in range(cntRow):
for col in range(cntCol):
self.SetCellValue(row, col, lst[row][col])
for row in range(cntRow):
self.SetCellAlignment(row, 0, wx.ALIGN_LEFT, wx.ALIGN_CENTER)
self.SetCellAlignment(row, 1, wx.ALIGN_LEFT, wx.ALIGN_CENTER)
self.SetCellBackgroundColour(row, 0, wx.LIGHT_GREY)
if row in [1,22]:
self.SetRowSize(row, 60)
else:
self.SetRowSize(row, 20)
self.SetColLabelSize(0)
self.SetRowLabelSize(0)
self.SetGridLineColour(wx.BLACK)
self.SetColSize(1, 500)
self.SetColSize(0,120)
self.SetColSize
(1,500)
def GetGridValues (self):
cntRow = self.GetNumberRows()
cntCol = self.GetNumberCols()
lst = [[]]*cntRow
for row in range(cntRow):
for col in range(cntCol):
lst[row].append(self.GetCellValue(row,col))
return lst
class DocVarGridFrame(wx.Frame):
def init(self, lst, data, title = “”, msg = “”):
wx.Frame.init(self, None, -1, title, pos = (200, 100), size = (900,600))
panel = wx.Panel(self)
self.data = data
msgLbl = wx.StaticText(panel, -1, msg)
topSizer = wx.BoxSizer(wx.HORIZONTAL)
topSizer.Add (msgLbl,0)
self.okBtn = wx.Button(panel, -1, "&Ok")
self.cancelBtn = wx.Button(panel, -1, "&Cancel")
btnSizer = wx.BoxSizer(wx.VERTICAL)
btnSizer.Add(self.okBtn, 0)
btnSizer.Add((10,10), 0)
btnSizer.Add(self.cancelBtn, 0)
self.Bind(wx.EVT_BUTTON, self.OnOkClick, self.okBtn)
self.Bind(wx.EVT_BUTTON , self.OnCancelClick, self.cancelBtn)
self.grid = DocVarGrid(panel, lst)
botSizer = wx.BoxSizer(wx.HORIZONTAL)
botSizer.Add (self.grid, 1, wx.EXPAND | wx.ALL, 10)
botSizer.Add(btnSizer, 0, wx.EXPAND | wx.ALL, 10)
mainSizer = wx.BoxSizer (wx.VERTICAL)
mainSizer.Add(topSizer, 0, wx.EXPAND | wx.ALL
, 10)
mainSizer.Add(botSizer, 1, wx.EXPAND | wx.ALL, 10)
panel.SetSizer(mainSizer)
mainSizer.Fit (self)
mainSizer.SetSizeHints(self)
self.SetClientSize
(panel.GetSize()+(16,60))
def OnOkClick(self, event):
self.data.value = self.grid.GetGridValues()
self.Destroy()
def OnCancelClick(self, event):
self.data.value = None
self.Destroy()
def WordDocVar():
title = “Document Variables”
lst = [[“Transmittal Number”, “”],
[“Address”, “”],
[“Addressee Name”, “”],
["Addressee Title", ""],
["Sender Name", ""],
["Sender Title", ""],
["Sender Company", ""],
["Date", ""],
["Subject", ""],
["Project Number", ""],
["Quantity 1", ""],
["Description 1", ""],
["Quantity 2", ""],
["Description 2", ""],
["Quantity 3", ""],
["Description 3", ""],
["Quantity 4", ""],
["Description 4", ""],
["Quantity 5", ""],
["Description 5", ""],
["Quantity 6", ""],
["Description 6", ""],
["Message", ""],
["Copy To", ""]]
msg = "Edit document variables in grid"
app = wx.PySimpleApp()
data = DocVarGridResult()
DocVarGridFrame(lst, data, title, msg).Show()
app.MainLoop()
return data.value
if name == “main”:
print WordDocVar()
···