【问题标题】:Setting properties for table and cell in RichTextctrl在 RichTextctrl 中设置表格和单元格的属性
【发布时间】:2017-07-19 14:25:46
【问题描述】:

我可以使用 RichTextctrl WriteTable 方法在 RichTextctrl 上写一个表格。

https://wxpython.org/Phoenix/docs/html/wx.richtext.RichTextCtrl.html#wx.richtext.RichTextCtrl.WriteTable

def AddTable( self, event ):
    self.table=self.rtc.WriteTable(3,4)

图片:Table_Picture

但是,无法设置表格和单元格属性,也无法识别 RichTextctrl 中选定的单元格或表格。任何关于这方面的例子都是有帮助的。在此先感谢。

wxPython 4.0.0a1(Alpha 版)

【问题讨论】:

    标签: python-2.7 wxpython


    【解决方案1】:

    有同样的问题;但是一些示例代码 你必须让你的函数返回表对象 self.table 有

    import wx
    import wx.richtext as rtc
    

    假设你在课堂上这样做

    columncount = self.table.GetColumnCount()
    rowcount = self.table.GetRowCount()
    

    在此处获取特定单元格 2,3

    self.myCell = self.table.GetCell(row=2, col=2)
    

    现在要写入该单元格 -> 必须是字符串

    self.myCell.AddParagraph("new stuff goest here")
    

    并从表格单元格中获取文本;为您想要的文本行编制索引

    self.myCellText = self.myCell.GetParagraphText(0)
    

    【讨论】:

      【解决方案2】:

      def create_table(self):

      xlist = []
      xlist.append({'desc': 'blabla blabla', 'amt': 5, 'prc': 2.50})    
      xlist.append({'desc': 'blabla blabla', 'amt': 5, 'prc': 2.50})    
      xlist.append({'desc': 'blabla blabla', 'amt': 5, 'prc': 2.50})    
      xlist.append({'desc': 'blabla blabla', 'amt': 5, 'prc': 2.50})    
      xlist.append({'desc': 'blabla blabla', 'amt': 5, 'prc': 2.50})    
      xlist.append({'desc': 'blabla blabla', 'amt': 5, 'prc': 2.50})    
      table = self.ctrl.WriteTable(len(xlist), 3)    
      for pos, cont in enumerate(xlist):    
          # Description
          cell = table.GetCell(row=pos, col=0)     
          prg = cell.GetParagraphAtLine(0)    
          prg.InsertText(1, str(cont['desc']))    
          prg.SetAttributes(self.set_alignment(prg, "left"))    
          cell.SetBasicStyle(self.set_colwidth(cell, 460))    
          cell.UpdateRanges()    
          # Amount
          cell = table.GetCell(row=pos, col=1)
          prg = cell.GetParagraphAtLine(0)
          prg.InsertText(1, str(cont['amt']))
          prg.SetAttributes(self.set_alignment(prg, "right"))
          cell.SetBasicStyle(self.set_colwidth(cell, 40))
          cell.UpdateRanges()
          # Price
          cell = table.GetCell(row=pos, col=2)
          prg = cell.GetParagraphAtLine(0)
          prg.InsertText(1, str(cont['prc']))
          prg.SetAttributes(self.set_alignment(prg, "right"))
          cell.SetBasicStyle(self.set_colwidth(cell, 60))
          cell.UpdateRanges()
      

      def set_colwidth(self, cell, width):

      dim_atr = rt.TextAttrDimension()
      dim_atr.SetValue(width)
      dim_atr.SetUnits(rt.TEXT_ATTR_UNITS_PIXELS)
      size_atr = rt.TextAttrSize()
      size_atr.SetWidth(dim_atr)
      box_atr = rt.TextBoxAttr()
      box_atr.SetSize(size_atr)
      cell_atr = cell.GetBasicStyle()
      cell_atr.SetTextBoxAttr(box_atr)
      return cell_atr
      

      def set_alignment(self, prg, ori):

      attr = prg.GetAttributes()
      if ori == "left":
          attr.SetAlignment(wx.TEXT_ALIGNMENT_LEFT)
          attr.SetLeftIndent(16)
      elif ori == "right":
          attr.SetAlignment(wx.TEXT_ALIGNMENT_RIGHT)
          attr.SetRightIndent(16)
      return attr
      

      【讨论】:

        猜你喜欢
        • 2010-09-19
        • 2013-08-15
        • 1970-01-01
        • 1970-01-01
        • 2013-01-30
        • 1970-01-01
        • 1970-01-01
        • 2021-11-29
        • 1970-01-01
        相关资源
        最近更新 更多