在RichTextctrl中设置表和单元格的属性

时间:2017-07-19 14:25:47

标签: python-2.7 wxpython

我可以使用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版)

2 个答案:

答案 0 :(得分:0)

有同样的问题;但是一些示例代码 您将必须让您的函数返回表对象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)

答案 1 :(得分:0)

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