Multiline Text Input in Grid Cell?

How can I input multiline of text strings into a grid cell? I'm not talking about auto-wrapping, I'd like to be able to insert blank lines in text. Tried both
SetCellRenderer to wx.grid.GridCellAutoWrapStringRenderer
()
&
SetCellEditor to wx.grid.GridCellTextEditor()
but didn't work. When I press "ENTER" key in a cell, the next cell is activated instead of starting a new line in the same cell. Do I need to nest textCtrl in a cell? Or Can I do that?

Thank you.

···

  • wcc

wccppp schrieb:

How can I input multiline of text strings into a grid cell?

Hi wcc!

Maybe with:
SHIFT+ENTER or
CTRL+ENTER

Regards,
Gerold
:slight_smile:

···

--
________________________________________________________________________
Gerold Penz - bcom - Programmierung
     gerold.penz@tirol.utanet.at | http://gerold.bcom.at | http://sw3.at
Ehrliche, herzliche Begeisterung ist einer der
     wirksamsten Erfolgsfaktoren. Dale Carnegie

Thanks Gerold. I tried CTRL+ENTER, SHIFT+ENTER, ALT+ENTER. Not the answer.

···


wcc

wccppp schrieb:

Thanks Gerold. I tried CTRL+ENTER, SHIFT+ENTER, ALT+ENTER. Not the answer.

Hi wcc!

The combination of CTRL+ENTER and ``wx.grid.GridCellAutoWrapStringEditor`` does it.

   #!/usr/bin/env python
   # -*- coding: iso-8859-1 -*-

   import wx
   import wx.grid

   wx.SetDefaultPyEncoding("iso-8859-1")

   class MyFrame(wx.Frame):

       def __init__(self, parent = None, id = -1, title = "Multiline"):

           wx.Frame.__init__(self, parent, id, title)

           panel = wx.Panel(self)

           vbox = wx.BoxSizer(wx.VERTICAL)
           panel.SetSizer(vbox)

           grid = wx.grid.Grid(panel, style = wx.WANTS_CHARS | wx.BORDER)
           vbox.Add(grid, 1, wx.EXPAND | wx.ALL, 5)

           grid.CreateGrid(10, 10)
           grid.SetDefaultEditor(wx.grid.GridCellAutoWrapStringEditor())

           grid.SetCellValue(0, 0, "Line1\nLine2\nLine3")
           grid.SetRowSize(0, 100)

           # Use CTRL+ENTER to write line breaks manually

   def main():

       app = wx.PySimpleApp()
       f = MyFrame()
       f.Center()
       f.Show()
       app.MainLoop()

   if __name__ == "__main__":
       main()

Regards,
Gerold
:slight_smile:

···

--
________________________________________________________________________
Gerold Penz - bcom - Programmierung
     gerold.penz@tirol.utanet.at | http://gerold.bcom.at | http://sw3.at
Ehrliche, herzliche Begeisterung ist einer der
     wirksamsten Erfolgsfaktoren. Dale Carnegie

Gerold,

Thanks a lot for your code. It works!! Now I need to figure out why I did wrong.

···


wcc

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()

···

wccppp schrieb:

I still could not figure out why my code does not work. When press CTRL+ENTER, the program just activate the next cell.

Hi wccppp!?

(sorry for my english)

I found out, that it could have to do with the additional buttons. That´s a problem, because every programm needs buttons.

Under Windows, I found a (terrible) workaround for it. I hope it help´s you.

   #!/usr/bin/env python
   # -*- coding: iso-8859-1 -*-

   import wx
   import wx.grid

   wx.SetDefaultPyEncoding("iso-8859-1")

   class MyGrid(wx.grid.Grid):

       def __init__(self, parent = None, style = wx.WANTS_CHARS):

           wx.grid.Grid.__init__(self, parent, -1, style = style)

           self.CreateGrid(10, 10)

           self.editor = wx.grid.GridCellAutoWrapStringEditor()
           self.SetDefaultEditor(self.editor)

           self.SetCellValue(0, 0, "Line1\nLine2\nLine3")
           self.SetRowSize(0, 100)

   class MyFrame(wx.Frame):

       def __init__(self, parent = None, title = "Multiline"):

           wx.Frame.__init__(self, parent, -1, title)

           self.Bind(wx.EVT_CHAR_HOOK, self.on_frame_char_hook)

           panel = wx.Panel(self)

           vbox = wx.BoxSizer(wx.VERTICAL)
           panel.SetSizer(vbox)

           grid = MyGrid(panel)
           vbox.Add(grid, 1, wx.EXPAND | wx.ALL, 5)
           self.grid = grid

           btn_exit = wx.Button(panel, -1, "Exit")
           vbox.Add(btn_exit, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 10)

       def on_frame_char_hook(self, event):

           if event.CmdDown() and event.GetKeyCode() == wx.WXK_RETURN:
               if self.grid.editor.IsCreated():
                   self.grid.editor.StartingKey(event)
               else:
                   event.Skip
           else:
               event.Skip()

   def main():

       app = wx.PySimpleApp()
       f = MyFrame()
       f.Center()
       f.Show()
       app.MainLoop()

   if __name__ == "__main__":
       main()

Regards,
Gerold
:slight_smile:

···

--
________________________________________________________________________
Gerold Penz - bcom - Programmierung
     gerold.penz@tirol.utanet.at | http://gerold.bcom.at | http://sw3.at
Ehrliche, herzliche Begeisterung ist einer der
     wirksamsten Erfolgsfaktoren. Dale Carnegie

Hi Gerold,

Thanks a lot for your continous help. With my entry level wxPython skill, I won’t be able to tell whether a workaround is terrible or terrific, as long as it works, it is all good for now.

Thanks again.

···


wcc