python grid wxpython newline wrapping

Ajuste automático y nuevas líneas en la grilla wxPython



grid newline (1)

Resolvió este problema escribiendo un renderizador personalizado:

from wx.lib import wordwrap import wx.grid class CutomGridCellAutoWrapStringRenderer(wx.grid.PyGridCellRenderer): def __init__(self): wx.grid.PyGridCellRenderer.__init__(self) def Draw(self, grid, attr, dc, rect, row, col, isSelected): text = grid.GetCellValue(row, col) dc.SetFont( attr.GetFont() ) text = wordwrap.wordwrap(text, grid.GetColSize(col), dc, breakLongWords = False) hAlign, vAlign = attr.GetAlignment() if isSelected: bg = grid.GetSelectionBackground() fg = grid.GetSelectionForeground() else: bg = attr.GetBackgroundColour() fg = attr.GetTextColour() dc.SetTextBackground(bg) dc.SetTextForeground(fg) dc.SetBrush(wx.Brush(bg, wx.SOLID)) dc.SetPen(wx.TRANSPARENT_PEN) dc.DrawRectangleRect(rect) grid.DrawTextRectangle(dc, text, rect, hAlign, vAlign) def GetBestSize(self, grid, attr, dc, row, col): text = grid.GetCellValue(row, col) dc.SetFont(attr.GetFont()) text = wordwrap.wordwrap(text, grid.GetColSize(col), dc, breakLongWords = False) w, h, lineHeight = dc.GetMultiLineTextExtent(text) return wx.Size(w, h) def Clone(self): return CutomGridCellAutoWrapStringRenderer()

Quiero implementar una grilla con las celdas que tienen el siguiente comportamiento:

  1. el texto de la celda debe estar envuelto si no se ajusta a la celda

  2. las líneas nuevas (/ n) en el texto de la celda también se deben procesar

es decir, el mismo comportamiento que en los editores de tablas como MS Excel, OO Calc, etc. cuando habilita la opción ''wrap words'' para celdas.

Estoy tratando de hacer esto de la siguiente manera:

import wx import wx.grid 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.SetDefaultRenderer(wx.grid.GridCellAutoWrapStringRenderer()) 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) #Proceed CTRL+ENTER as newline in the cell editor 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() if __name__ == "__main__": app = wx.PySimpleApp() f = MyFrame() f.Center() f.Show() app.MainLoop()

Pero este código no funciona como se esperaba: líneas nuevas procesadas correctamente en el editor de la celda, pero ignoradas en el procesador de la celda. Si self.SetDefaultRenderer(wx.grid.GridCellAutoWrapStringRenderer()) , las líneas nuevas se procesan correctamente tanto en el editor como en el procesador, pero obviamente el ajuste automático en el renderizador no funciona.

¿Alguien sabe cómo resolver esto?