2012-12-24 31 views
1

我正在用樹控件創建一個程序,樹中的每個項目都有數據顯示在wxRichTextctrl中。我想出瞭如何從ctrl獲取xml數據,但我不知道如何在ctrl中顯示。就像我設置setvalue()時,它只是顯示xml。從文件加載不是一個有效的選項,因爲我有一個字典從我加載每個記錄(存儲在XML)否則我會創建temperory文件並加載它是一種令人毛骨悚然。 如果你能幫我解決一些示例代碼,我將不勝感激。WxRichTextCtrl python從文件中檢索條目

回答

1

要繞過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() 
+0

出現了問題。我試過你的方法,沒有發生錯誤,但屏幕沒有更新。我添加了一個刷新到控件,並嘗試佈局,因爲我發現從谷歌搜索。但仍然沒有。嘗試你的方法後,我試圖從空白屏幕保存緩衝區,它只包含一個正常的XML沒有任何數據。所以我猜ctrl沒有更新。這不是顯示更新問題。你能幫助 – MYNE

+0

@GeorgeCyriac我已經用另一種解決方案更新了我的答案。請讓我知道這對你有沒有用。這兩個版本都可以在winXP,py 2.7,wx 2.8/2.9上運行。 –

+0

它現在的作品!再次感謝。 – MYNE