如何使用RichTextCtrl事件?

问题描述 投票:0回答:1

我不太清楚如何使用RichTextCtrl事件。

我想把输入到RichText中的文本转换成另一个字符串。

我想我可以用以下方法来实现这个目的 EVT_RICHTEXT_CHARACTER 但当我输入

self.textField.Bind(wx.EVT_RICHTEXT_CHARACTER,self.textEdit)

其中self.textField是RichTextCtrl,我得到一个错误信息说。

Cannot find reference 'EVT_RICHTEXT_CHARACTER' in '__init__.py | __init__.py | imported module wx'

我是否需要导入其他东西才能工作?如果需要,是什么?我不太明白wxPython文档中是怎么说的。

python wxpython
1个回答
1
投票

我怀疑你只是将事件错误地分配给了 wx 当它应该是其他东西的时候。下面,因为我导入的是 wx.richtext 作为 rt 该活动将在 rtrt.EVT_RICHTEXT_CHARACTER

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.FONTFAMILY_DEFAULT|wx.TEXT_ATTR_FONT_FACE)
        self.rtc1.Bind(rt.EVT_RICHTEXT_CHARACTER,self.textEdit)
        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("\nThis is super?")

    def textEdit(self, event):
        char = event.GetCharacter()
        self.rtc2.AppendText(char)

if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame()
    app.MainLoop()
© www.soinside.com 2019 - 2024. All rights reserved.