问题描述
我正在使用 rtf 控件来显示文本.我无法理解 rtf 控制对象结构.如何将文本放入 rtf 缓冲区?
I am using a rtf control to display text. I cannot understand the rtf control object structure. How do you get text into the rtf buffer?
我已经访问了缓冲区并添加了一个 xml 处理程序.我从那里去哪里?
I have accessed the buffer and added an xml handler. Where do I go from there?
import wx
import wx.richtext as rt
class MainFrame(wx.Frame):
"""Create MainFrame class."""
def __init__(self):
"""Initialise the class."""
wx.Frame.__init__(self, None, -1, 'Demonstrate wxPython Rich Text')
self.panel = MainPanel(self)
self.refresh_rtf(self.raw_xml())
self.Centre()
def refresh_rtf(self, xml):
handler = wx.richtext.RichTextXMLHandler()
rtf_buffer = self.rtf_control.GetBuffer()
rtf_buffer.AddHandler(handler)
#handler.ImportXML(rtf_buffer, self.rtf_control)
#self.rtf_control.Refresh()
def raw_xml(self):
xml = ('<?xml version="1.0" encoding="UTF-8"?>'
'<richtext version="1.0.0.0" xmlns="http://www.wxwidgets.org">'
'<paragraphlayout textcolor="#4C4C4C" fontsize="11"'
'fontstyle="90"'
'fontweight="90" fontunderlined="0"'
'fontface="Ubuntu" alignment="1"'
'parspacingafter="10" parspacingbefore="0"'
'linespacing="10">'
'<paragraph>'
'<text>"What do we want: "</text>'
'<text textcolor="#FF0000">all</text>'
'</paragraph>'
'</paragraphlayout>'
'</richtext>')
return xml
class MainPanel(wx.Panel):
"""Create a panel class to contain screen widgets."""
def __init__(self, frame):
"""Initialise the class."""
wx.Panel.__init__(self, frame)
rtf_sizer = self._create_rtf_control(frame)
main_sizer = wx.BoxSizer(wx.HORIZONTAL)
main_sizer.Add(rtf_sizer, flag=wx.ALL, border=10)
self.SetSizerAndFit(main_sizer)
def _create_rtf_control(self, frame):
rtf_style = wx.VSCROLL|wx.HSCROLL|wx.TE_READONLY|wx.BORDER_SIMPLE
frame.rtf_control = wx.richtext.RichTextCtrl(self,
size=(400, 200),
style=rtf_style)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(frame.rtf_control)
return sizer
if __name__ == '__main__':
"""Run the application."""
screen_app = wx.App()
main_frame = MainFrame()
main_frame.Show(True)
screen_app.MainLoop()
*****编辑*****
*****EDIT*****
作为参考,我在这里添加了实现我想要的结果的 Python 2 代码
For reference I have added here the Python 2 code that achieved the result I want
def refresh_rtf(self, xml):
if xml != '':
out = StringIO()
handler = wx.richtext.RichTextXMLHandler()
rtf_buffer = self.rtf_control.GetBuffer()
rtf_buffer.AddHandler(handler)
out.write(str(xml))
out.seek(0)
handler.LoadStream(rtf_buffer, out)
self.rtf_control.Refresh()
Lorem ipsum dolor 坐 amet,consectetur adipiscing 精英.整数 ne odio.Praesent 自由人.sed cursus ante dapibus 直径.塞德尼西.Nulla quis sem 在 nibh elementum imperdiet.Duis sagittis ipsum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum.
推荐答案
我已经放弃了这项工作.我确定这是可能的,但似乎没有人能够指导我
I have given this up as a bad job. I'm sure it's possible, but no one seems to be able to guide me
相反,我使用
html.HtmlWindow
修改后的代码,功能完美,是:
The revised code, which functions perfectly, is:
import wx
import wx.html as html
class MainFrame(wx.Frame):
"""Create MainFrame class."""
def __init__(self):
"""Initialise the class."""
wx.Frame.__init__(self, None, -1, 'Demonstrate wxPython Html')
self.panel = MainPanel(self)
self.Centre()
self.html_display.SetPage(self.raw_html())
@staticmethod
def raw_html():
html = ('<p><font color="#4C4C4C", size=2>What do we want: '
'<font color="#FF0000">all</font>'
'</p>')
return html
class MainPanel(wx.Panel):
"""Create a panel class to contain screen widgets."""
def __init__(self, frame):
"""Initialise the class."""
wx.Panel.__init__(self, frame)
html_sizer = self._create_html_control(frame)
main_sizer = wx.BoxSizer(wx.HORIZONTAL)
main_sizer.Add(html_sizer, flag=wx.ALL, border=10)
self.SetSizerAndFit(main_sizer)
def _create_html_control(self, frame):
txt_style = wx.VSCROLL|wx.HSCROLL|wx.BORDER_SIMPLE
frame.html_display = html.HtmlWindow(self, -1,
size=(400, 200),
style=txt_style)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(frame.html_display)
return sizer
if __name__ == '__main__':
"""Run the application."""
screen_app = wx.App()
main_frame = MainFrame()
main_frame.Show(True)
screen_app.MainLoop()
转换原始数据需要一些工作,但不要太多
A bit of work to convert the raw data, but not too much
这篇关于使用 wxPython 富文本控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!