• <small id='FnNDP'></small><noframes id='FnNDP'>

    1. <tfoot id='FnNDP'></tfoot>

          <bdo id='FnNDP'></bdo><ul id='FnNDP'></ul>
        <legend id='FnNDP'><style id='FnNDP'><dir id='FnNDP'><q id='FnNDP'></q></dir></style></legend>
      1. <i id='FnNDP'><tr id='FnNDP'><dt id='FnNDP'><q id='FnNDP'><span id='FnNDP'><b id='FnNDP'><form id='FnNDP'><ins id='FnNDP'></ins><ul id='FnNDP'></ul><sub id='FnNDP'></sub></form><legend id='FnNDP'></legend><bdo id='FnNDP'><pre id='FnNDP'><center id='FnNDP'></center></pre></bdo></b><th id='FnNDP'></th></span></q></dt></tr></i><div id='FnNDP'><tfoot id='FnNDP'></tfoot><dl id='FnNDP'><fieldset id='FnNDP'></fieldset></dl></div>

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

        Auto wrap and newlines in wxPython grid(wxPython网格中的自动换行和换行)

        <legend id='KISPy'><style id='KISPy'><dir id='KISPy'><q id='KISPy'></q></dir></style></legend>

        <small id='KISPy'></small><noframes id='KISPy'>

            <tbody id='KISPy'></tbody>

          1. <tfoot id='KISPy'></tfoot>
              <bdo id='KISPy'></bdo><ul id='KISPy'></ul>
              <i id='KISPy'><tr id='KISPy'><dt id='KISPy'><q id='KISPy'><span id='KISPy'><b id='KISPy'><form id='KISPy'><ins id='KISPy'></ins><ul id='KISPy'></ul><sub id='KISPy'></sub></form><legend id='KISPy'></legend><bdo id='KISPy'><pre id='KISPy'><center id='KISPy'></center></pre></bdo></b><th id='KISPy'></th></span></q></dt></tr></i><div id='KISPy'><tfoot id='KISPy'></tfoot><dl id='KISPy'><fieldset id='KISPy'></fieldset></dl></div>

                1. 本文介绍了wxPython网格中的自动换行和换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想用具有以下行为的单元格实现一个网格:

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

                  1. 如果单元格文本不适合单元格,则应将其换行

                  1. 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网格中的自动换行和换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                    <bdo id='zryV0'></bdo><ul id='zryV0'></ul>
                  • <tfoot id='zryV0'></tfoot>

                  • <small id='zryV0'></small><noframes id='zryV0'>

                  • <legend id='zryV0'><style id='zryV0'><dir id='zryV0'><q id='zryV0'></q></dir></style></legend>

                      <i id='zryV0'><tr id='zryV0'><dt id='zryV0'><q id='zryV0'><span id='zryV0'><b id='zryV0'><form id='zryV0'><ins id='zryV0'></ins><ul id='zryV0'></ul><sub id='zryV0'></sub></form><legend id='zryV0'></legend><bdo id='zryV0'><pre id='zryV0'><center id='zryV0'></center></pre></bdo></b><th id='zryV0'></th></span></q></dt></tr></i><div id='zryV0'><tfoot id='zryV0'></tfoot><dl id='zryV0'><fieldset id='zryV0'></fieldset></dl></div>

                        <tbody id='zryV0'></tbody>