如何使“查找”对话框使用选择而不是SetStyles?

时间:2018-11-10 20:49:38

标签: python python-3.x wxpython

如何使文本编辑器的“查找”功能使用选择而不是SetStyles?它在此question中干扰了Lexer和self.tc.SetLexer(stc.STC_LEX_NULL)。我不想使用SetStyles,因为我觉得将来它会干扰其他SetStyles,所以我想使用选择,我该怎么做?

PS:我正在使用Windows

链接到发生的事情:imgur.com/a/oeLImVQ

代码:

import wx
import wx.stc as stc
import keyword

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.bt_css = wx.Button(self, -1, "CSS")

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

        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)
        sizer.Add(self.bt_css, 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
        dlg = wx.FindReplaceDialog(self.tc, self.data, 'Find')
        dlg.Show()

    def on_find(self, event):
        self.tc.StartStyling(pos=0, mask=0xFF)
        self.tc.SetStyling(length=len(self.txt), style=0)
        fstring = event.GetFindString()
        self.size = len(fstring)
        while True:
            self.pos = self.txt.find(fstring, self.pos)
            if self.pos < 0:
                break
            self.tc.StyleSetSpec(1, "fore:#FF0000,back:#000000")
            self.tc.StartStyling(pos=self.pos, mask=0xFF)
            self.tc.SetStyling(length=self.size, style=1)
            self.pos += 1
        self.pos = 0

    def CSS(self, e):
        self.tc.SetLexer(stc.STC_LEX_CSS)
        self.tc.SetKeyWords(0, " ".join(keyword.kwlist))
        self.tc.StyleSetSpec(wx.stc.STC_CSS_ATTRIBUTE, 'fore:#0000FF')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_CLASS, 'fore:#0000FF')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_COMMENT, 'fore:#008000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_DEFAULT, 'fore:#000000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_DIRECTIVE, 'fore:#0000FF')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_DOUBLESTRING, 'fore:#800080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_ID, 'fore:#008080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_IDENTIFIER, 'fore:#000000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_IDENTIFIER2, 'fore:#000000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_IMPORTANT, 'fore:#000000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_OPERATOR, 'fore:#800000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_PSEUDOCLASS, 'fore:#008080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_SINGLESTRING, 'fore:#800080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_TAG, 'fore:#008080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_UNKNOWN_IDENTIFIER, 'fore:#000000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_UNKNOWN_PSEUDOCLASS, 'fore:#008080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_VALUE, 'fore:#668B8B')

if __name__ == "__main__":

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

预先感谢

0 个答案:

没有答案