【问题标题】:wxPython - How can I display a html formatted string in wx.RichTextCtrlwxPython - 如何在 wx.RichTextCtrl 中显示 html 格式的字符串
【发布时间】:2010-06-08 09:44:32
【问题描述】:

我正在尝试在 Richtext Ctrl 中显示一些字符串(html 格式)。在我的代码中,我尝试以这种方式使用它(self.txtmain 是 RichTextCtrl):

out = StringIO()
htmlhandler = rt.RichTextHTMLHandler()
buffer = self.txtmain.GetBuffer()
buffer.AddHandler(htmlhandler)
out.write(string)
out.seek(0)
htmlhandler.LoadStream(buffer, out)
self.txtmain.Refresh()

未发出错误,但 RichTextCtrl 窗口未更新。 我在这里错过了什么?

【问题讨论】:

  • 检查htmlhandler.LoadStream(buffer, out)的结果,false表示失败(不知道为什么,还在努力)

标签: wxpython richtextctrl


【解决方案1】:

查看“wx.Layout()”,更新窗口/小部件。

在某些情况下,我在添加项目后使用“wx.Layout()”重绘整个窗口

例如,当我隐藏一个小部件并在同一位置显示另一个小部件时...

在这种情况下,self.Layout(),在self.txtmain.Refresh()之后..

out = StringIO()
htmlhandler = rt.RichTextHTMLHandler()
buffer = self.txtmain.GetBuffer()
buffer.AddHandler(htmlhandler)
out.write(string)
out.seek(0)
htmlhandler.LoadStream(buffer, out)
self.txtmain.Refresh()
self.Layout()

但是,我不确定它是否适用于您的情况...

并且要从 StringIO() 中检索内容,必须使用 getvalue()

htmlhandler.LoadStream(buffer, out)

  htmlhandler.LoadStream(buffer, out.getvalue())

【讨论】:

  • 已更改为 out.getvalue() 但我现在有“TypeError: Expected wx.InputStream or Python file-like object。”
猜你喜欢
  • 1970-01-01
  • 2011-01-05
  • 2021-11-28
  • 2023-02-15
  • 2019-08-06
  • 1970-01-01
  • 2020-03-13
  • 2019-03-04
  • 1970-01-01
相关资源
最近更新 更多