wxPython 网格中的自动换行和换行符

5 投票
2 回答
3940 浏览
提问于 2025-04-16 16:53

我想实现一个网格,里面的单元格需要有以下的表现:

  1. 如果单元格里的文字太长,应该自动换行。

  2. 单元格里的换行符(\n)也应该被处理。

也就是说,跟像MS Excel、OO Calc这样的表格编辑器一样,当你为单元格开启“自动换行”选项时的表现是一样的。

我尝试这样做:

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

但是这个代码的效果并不如我所愿——在单元格编辑器里换行符处理得很好,但在单元格渲染器里却被忽略了。如果我去掉self.SetDefaultRenderer(wx.grid.GridCellAutoWrapStringRenderer()),那么换行符在编辑器和渲染器里都能正常处理,但显然渲染器里的自动换行就不管用了。

有没有人知道该怎么解决这个问题?

2 个回答

0

对于列标题,我成功地插入了一个换行符(\n)。

self.m_grid1.SetColLabelValue(8, "Reference \n Level")
4

我通过写一个自定义的渲染器解决了这个问题:

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

撰写回答