我需要帮助使我的查找对话框不干扰我的词法分析器。我在此question中使用了查找对话框,但它使用的SetStyles会干扰我的词法分析器。除了弄乱语法突出显示之外,当我再次尝试执行“查找”时,它只会突出显示随机的内容。任何帮助表示赞赏!
编辑器:
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 :(得分:1)
首先,您应该使用lexer未使用的样式编号,请参见_stc.py
中的样式代码。
您还可以使用Colourise(0, -1)
方法清除自定义样式,并将默认词法分析器的颜色重新应用于整个文档(请参见StyledTextCtrl documentation)。
import wx
import wx.stc as stc
import keyword
SCE_FIND = 24 # style for highlighting of matching text
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_FIND_CLOSE, self.on_find_close)
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()
self.tc.StyleSetSpec(SCE_FIND, "fore:#FF0000,back:#000000")
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', wx.FR_NOUPDOWN | wx.FR_NOMATCHCASE)
dlg.Show()
def on_find(self, event):
fstring = event.GetFindString()
self.size = len(fstring)
while True:
self.pos = self.txt.find(fstring, self.pos)
if self.pos < 0:
break
self.tc.StartStyling(pos=self.pos, mask=0xFF)
self.tc.SetStyling(length=self.size, style=SCE_FIND)
self.pos += 1
self.pos = 0
def on_find_close(self, event):
self.tc.Colourise(0, -1)
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()