Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4569662
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:15:05+00:00 2026-05-21T19:15:05+00:00

I want to implement a grid with the cells that have the following behaviour:

  • 0

I want to implement a grid with the cells that have the following behaviour:

  1. cell text should be wrapped if it doesn’t fit to the cell

  2. newlines (\n) in the cell text should be processed as well

i.e. the same behaviour as in table editors like MS Excel, OO Calc, etc. when you enable the ‘wrap words’ option for cells.

I’m trying to do this as follows:

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

But this code doesn’t work as expected – newlines processed correctly in the cell editor, but ignored in the cell renderer. If I remove the self.SetDefaultRenderer(wx.grid.GridCellAutoWrapStringRenderer()) then newlines processed correctly both in the editor and renderer, but obviously auto wrapping in the renderer doesn’t work.

Does anybody know how to solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T19:15:06+00:00Added an answer on May 21, 2026 at 7:15 pm

    Solved this problem by writing a custom renderer:

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have stack of images in a grid and i want to implement a
I have a text string in my grid in follwoing format Pawan/tiwari/idof22/vf i want
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want to implement file uploading with behaviour which is commonly seen in various
I want to implement a basic search/replace translation table in C; that is, it
I want to implement treegrid using local proxy. Is it possible? I have tried
I need to implement a grid in asp.net that behaves almost exactly like MS
I'm creating a grid based game in Java and I want to implement game
I want to implement a data structure of a grid of triangles shaped like
I have a window that contains a grid with two columns. the first column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.