如何使“查找”对话框使用选择而不是设置样式?

2024-04-26 10:17:04 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使文本编辑器的Find函数使用selections而不是SetStyles?它干扰了Lexer和自我控制设置器(stc.stc_LEX_空)在这个question中。我不想使用SetStyles,因为我觉得它将来会干扰其他SetStyles,所以我想改用selections,我该怎么做?你知道吗

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()

提前谢谢


Tags: posselfstyleondeffindcssstc