【问题标题】:Superscript in wxPython RichTextCtrlwxPython RichTextCtrl 中的上标
【发布时间】:2018-11-05 14:06:33
【问题描述】:

我正在尝试让 wxPython RichTextCtrl 显示上标。 我在

看到了一些 wxWidgets 代码

http://wxwidgets.10942.n7.nabble.com/rich-text-and-font-attributes-td23557.html

并且还看到了

的文档

https://wxpython.org/Phoenix/docs/html/wx.TextAttr.html#wx.TextAttr.SetTextEffects

到目前为止,我得到了这个,但它不起作用

attr = wx.richtext.RichTextAttr()
attr.SetTextEffects (wx.TEXT_ATTR_EFFECT_SUPERSCRIPT)
attr.SetTextEffectFlags (wx.TEXT_ATTR_EFFECTS)
#attr.SetTextEffectFlags (wx.TEXT_ATTR_EFFECT_SUPERSCRIPT)
attr.SetFlags (wx.TEXT_ATTR_EFFECTS)
self.myRichTextCtrl.SetStyle (currentPos, currentPos+len(value1)-1, attr)
self.myRichTextCtrl.WriteText (myString)

我知道有一个 fancytext 小部件,但此时切换到 fancytext 是不切实际的。

任何帮助将不胜感激!

【问题讨论】:

    标签: wxpython subscript superscript richtextctrl


    【解决方案1】:

    使用SetStyle,您可以将属性应用于尚未编写的文本位置。

    有一个选项SetBasicStyle 和SetDefaultStyle 允许您设置整个文档或从现在开始的属性。

    这是一个工作示例。

    import wx
    import wx.richtext as rt
    class MainFrame(wx.Frame):
    
        def __init__(self):
            wx.Frame.__init__(self, None, title='Test RichText Superscript')
            self.panel = wx.Panel(self)
    
            self.rtc1 = rt.RichTextCtrl(self.panel,pos=(10,10),size=(350,90),style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER|wx.FONTFAMILY_DEFAULT|wx.TEXT_ATTR_FONT_FACE)
            self.rtc2 = rt.RichTextCtrl(self.panel,pos=(10,110),size=(350,90),style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER|wx.FONTFAMILY_DEFAULT|wx.TEXT_ATTR_FONT_FACE)
    
            self.Show()
    
            attr_super = wx.richtext.RichTextAttr()
            attr_super.SetTextEffects(wx.TEXT_ATTR_EFFECT_SUPERSCRIPT)
            attr_super.SetFlags(wx.TEXT_ATTR_EFFECTS)
            attr_super.SetTextEffectFlags(wx.TEXT_ATTR_EFFECT_SUPERSCRIPT)
            self.rtc1.WriteText("Is this super?")
            self.rtc1.SetStyle (7, 13, attr_super)
    
            attr_sub = wx.richtext.RichTextAttr()
            attr_sub.SetTextEffects(wx.TEXT_ATTR_EFFECT_SUBSCRIPT)
            attr_sub.SetFlags(wx.TEXT_ATTR_EFFECTS)
            attr_sub.SetTextEffectFlags(wx.TEXT_ATTR_EFFECT_SUBSCRIPT)
            self.rtc1.AppendText ("\nIs this sub?")
            self.rtc1.SetStyle (23, 26, attr_sub)
            self.rtc1.AppendText ("\nIs this normal?")
    
            self.rtc2.WriteText("Is this super?")
            self.rtc2.SetDefaultStyle(attr_super)
            self.rtc2.WriteText("\nIs this super?")
    
    
    
    if __name__ == '__main__':
        app = wx.App()
        frame = MainFrame()
        app.MainLoop()
    

    【讨论】:

    • 太棒了!非常感谢!
    猜你喜欢
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多