问题描述
我正在使用wxpython开发一个中等复杂的文本编辑程序,但遇到一个奇怪的问题。我不能在段落的最后一行插入换行符。
下面是python3 wxPython 4.1.0中的最小工作示例代码,可让您在某些段落中插入换行符。
请注意,即使是代码中第2段中的换行符也不会显示在GUI中。 而且,如果您转到最后一行并按Shift + Enter(通常会插入换行符),则此操作将无效。它适用于第二行和第一行,但不适用于最后一行。 在第一个链接内发生相同的问题。但是奇怪的是,您可以在第二个链接中插入一个换行符。
我怀疑我在样式上做错了,但是我找不到。 有谁有想法吗? 谢谢。
import wx
import wx.richtext as rt
class RichTextFrame(wx.Frame):
def __init__(self,*args,**kw):
wx.Frame.__init__(self,**kw)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.rtc = rt.RichTextCtrl(self,style=wx.VSCROLL | wx.HSCROLL | wx.NO_BORDER)
self.sizer.Add(self.rtc,1,flag=wx.EXPAND)
self.SetSizer(self.sizer)
self._stylesheet = rt.RichTextStyleSheet()
self._stylesheet.SetName('Stylesheet')
self._create_styles()
self._insert_sample_text()
def _create_styles(self) -> None:
"""
Create styles for rich text control.
:return: None
"""
# normal style
stl_paragraph: rt.RichTextAttr = self.rtc.GetDefaultStyleEx()
stl_paragraph.SetParagraphSpacingBefore(10)
stl_paragraph.SetParagraphSpacingAfter(10)
style_paragraph: rt.RichTextParagraphStyleDeFinition = rt.RichTextParagraphStyleDeFinition('par')
style_paragraph.SetStyle(stl_paragraph)
style_paragraph.SetNextStyle('par')
self._stylesheet.AddParagraphStyle(style_paragraph)
self.rtc.ApplyStyle(style_paragraph)
self.rtc.SetDefaultStyle(stl_paragraph)
# Link style
stl_link = rt.RichTextAttr()
stl_link.SetFlags(wx.TEXT_ATTR_URL)
stl_link.SetFontUnderlined(True)
stl_link.SetTextColour(wx.BLUE)
style_link: rt.RichTextCharacterStyleDeFinition = rt.RichTextCharacterStyleDeFinition('url')
style_link.SetStyle(stl_link)
self._stylesheet.AddCharacterStyle(style_link)
def _insert_link(self,text: str,link_id: str) -> None:
"""
Insert a link into text at current position.
:param text: The visible text.
:param link_id: The ID of the link
:return: None
"""
self.rtc.BeginStyle(self._stylesheet.FindCharacterStyle('url').GetStyle())
self.rtc.BeginURL(link_id)
self.rtc.WriteText(text)
self.rtc.EndURL()
self.rtc.EndStyle()
def _insert_sample_text(self) -> None:
"""
Insert sample text.
:return: None
"""
self.rtc.ApplyStyle(self._stylesheet.FindParagraphStyle('par'))
self.rtc.BeginParagraphStyle('par')
self.rtc.WriteText('paragraph 1,sample of a longer text for testing url creation ')
self.rtc.LineBreak()
self.rtc.WriteText('paragraph 1,more sample of a longer text for testing url creation ')
self._insert_link('www.google.com','link from code')
self.rtc.EndParagraphStyle()
self.rtc.Newline()
self.rtc.ApplyStyle(self._stylesheet.FindParagraphStyle('par'))
self.rtc.BeginParagraphStyle('par')
self._insert_link('www.google.com','42')
self.rtc.WriteText(' paragraph 2 adding some text after a link')
self.rtc.LineBreak()
self.rtc.WriteText(' some more text after a line break in code')
self.rtc.EndParagraphStyle()
self.rtc.Newline()
class MyApp(wx.App):
"""
Main class for running the gui
"""
def __init__(self):
wx.App.__init__(self)
self.frame = None
def OnInit(self):
self.frame = RichTextFrame(None,-1,"RichTextCtrl",size=(900,500),style=wx.DEFAULT_FRAME_STYLE)
self.SetTopWindow(self.frame)
self.frame.Show()
return True
if __name__ == "__main__":
app = MyApp()
app.MainLoop()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)