要繞過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
的內容以特定格式:
stream = StringIO()
handler = wx.richtext.RichTextHTMLHandler()
handler.SaveStream(myRichTextCtrl.GetBuffer(), stream)
print stream.getvalue()
或者,也可以直接通過緩衝器加載流。請注意,相應的處理必須已經存在來解釋數據:
# 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()
出現了問題。我試過你的方法,沒有發生錯誤,但屏幕沒有更新。我添加了一個刷新到控件,並嘗試佈局,因爲我發現從谷歌搜索。但仍然沒有。嘗試你的方法後,我試圖從空白屏幕保存緩衝區,它只包含一個正常的XML沒有任何數據。所以我猜ctrl沒有更新。這不是顯示更新問題。你能幫助 – MYNE
@GeorgeCyriac我已經用另一種解決方案更新了我的答案。請讓我知道這對你有沒有用。這兩個版本都可以在winXP,py 2.7,wx 2.8/2.9上運行。 –
它現在的作品!再次感謝。 – MYNE