Auto wrap and newlines in wxPython grid(wxPython网格中的自动换行和换行)
问题描述
我想用具有以下行为的单元格实现一个网格:
I want to implement a grid with the cells that have the following behaviour:
如果单元格文本不适合单元格,则应将其换行
cell text should be wrapped if it doesn't fit to the cell
单元格文本中的换行符 ( ) 也应该被处理
newlines ( ) in the cell text should be processed as well
即当您为单元格启用换行"选项时,其行为与 MS Excel、OO Calc 等表格编辑器中的行为相同.
i.e. the same behaviour as in table editors like MS Excel, OO Calc, etc. when you enable the 'wrap words' option for cells.
我正在尝试这样做:
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
Line2
Line3")
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()) 则换行符在编辑器和渲染器中都正确处理,但显然在渲染器中自动换行不起作用.
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.
有人知道怎么解决吗?
推荐答案
通过编写自定义渲染器解决了这个问题:
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()
这篇关于wxPython网格中的自动换行和换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:wxPython网格中的自动换行和换行
基础教程推荐
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
