如何在StyledTextCtrl中创建查找对话框?

时间:2018-08-16 15:07:09

标签: python wxpython

关于此主题,存在一个现有问题:Find text dialog with wxpython,但在TextCtrl中。因此,我将TextCtrl更改为StyledTextCtrl,并对其进行了测试。但是我遇到了这个错误:

in wxStyledTextCtrl::SetStyle(): not implemented

如何使SetStyle成为选择,以便您单击即可?这是我的代码:

import wx
import wx.stc as stc

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.tc = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE | wx.TE_WORDWRAP)
        self.bt_find = wx.Button(self, -1, "find")

        self.Bind(wx.EVT_BUTTON, self.on_button, self.bt_find)
        self.Bind(wx.EVT_FIND, self.on_find)

        self.pos = 0
        self.size = 0
        #
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tc, 1, wx.EXPAND, 0)
        sizer.Add(self.bt_find, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        self.SetSizer(sizer)
        sizer.Fit(self)
        self.Layout()

    def on_button(self, event):
        self.txt = self.tc.GetValue()
        self.data = wx.FindReplaceData()   # initializes and holds search parameters
        self.dlg = wx.FindReplaceDialog(self.tc, self.data, 'Find')
        self.dlg.Show()

    def on_find(self, event):
        fstring = self.data.GetFindString()          # also from event.GetFindString()
        self.pos = self.txt.find(fstring, self.pos)
        self.size = len(fstring)
        self.tc.SetStyle(self.pos, self.pos+self.size, wx.TextAttr("red", "black"))


if __name__ == "__main__":

    app = wx.PySimpleApp(0)
    frame_1 = MyFrame(None, wx.ID_ANY, "")
    frame_1.Show()
    app.MainLoop()

1 个答案:

答案 0 :(得分:1)

StyledTextCtrl看起来像是一个非常复杂的野兽,我只能假设您将不得不认真阅读http://www.scintilla.org/ enter image description here
我评论中的链接指向使用scintilla函数而不是SetStyling
这是我使用的方法:

AddSelection

api