WxRichTextCtrl python从fi检索条目

2024-09-29 21:30:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个带有树控件的程序,树中的每个项目都有数据,这些数据显示在wxRichTextctrl中。我知道了如何从ctrl获取xml数据,但不知道如何在ctrl中显示。就像我设置setvalue()时一样,它只是按原样显示xml。从文件加载不是一个有效的选项,因为我有一个字典,我从中加载每个记录(存储在xml中),否则我将创建一个临时文件并加载它,这有点令人毛骨悚然。 如果您能帮我做一些示例代码,我将不胜感激。在


Tags: 数据项目代码程序示例字典选项记录
1条回答
网友
1楼 · 发布于 2024-09-29 21:30:44

要绕过RichTextCtrl.LoadFile(),必须创建一个基于RichTextFileHandler的类,并使用其LoadStream()方法直接写入RichTextCtrl缓冲区。在

  • RichTextPlainTextHandler
  • RichTextHTMLHandler
  • RichTextXMLHandler

例如:

from cStringIO import StringIO

# initialize a string stream with XML data
stream = StringIO( myXmlString )
# create an XML handler
handler = wx.richtext.RichTextXMLHandler()
# load the stream into the control's buffer
handler.LoadStream( myRichTextCtrl.GetBuffer(), stream )
# refresh the control
myRichTextCtrl.Refresh()

以及以特定格式获取RichTextCtrl的内容:

^{pr2}$

或者,可以直接通过缓冲区加载流。请注意,必须已经存在适当的处理程序来解释数据:

# add the handler (where you create the control)
myRichTextCtrl.GetBuffer().AddHandler(wx.richtext.RichTextXMLHandler())
stream = StringIO( myXmlString )
buffer = self.myRichTextCtrl.GetBuffer()
# you have to specify the type of data to load and the control
# must already have an instance of the handler to parse it
buffer.LoadStream(stream, wx.richtext.RICHTEXT_TYPE_XML)
myRichTextCtrl.Refresh()

相关问题 更多 >

    热门问题