如何在 wxPython 库的 Scintilla 控件中启用 C++ 突出显示?
How do I enable C++ highlighting in the Scintilla control in the wxPython library?
我正在尝试启用 C++ 突出显示,但没有突出显示任何内容。
我的代码:
self.m_pawnedit = wx.stc.StyledTextCtrl( self.m_panel1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.BORDER_NONE)
self.m_pawnedit.SetUseTabs ( True )
self.m_pawnedit.SetTabWidth ( 4 )
self.m_pawnedit.SetIndent ( 4 )
self.m_pawnedit.SetTabIndents( True )
self.m_pawnedit.SetBackSpaceUnIndents( True )
self.m_pawnedit.SetViewEOL( False )
self.m_pawnedit.SetViewWhiteSpace( False )
self.m_pawnedit.SetMarginWidth( 2, 0 )
self.m_pawnedit.SetIndentationGuides( True )
self.m_pawnedit.SetReadOnly( False );
self.m_pawnedit.SetMarginType ( 1, wx.stc.STC_MARGIN_SYMBOL )
self.m_pawnedit.SetMarginMask ( 1, wx.stc.STC_MASK_FOLDERS )
self.m_pawnedit.SetMarginWidth ( 1, 16)
self.m_pawnedit.SetMarginSensitive( 1, True )
self.m_pawnedit.SetProperty ( "fold", "1" )
self.m_pawnedit.SetFoldFlags ( wx.stc.STC_FOLDFLAG_LINEBEFORE_CONTRACTED | wx.stc.STC_FOLDFLAG_LINEAFTER_CONTRACTED );
self.m_pawnedit.SetMarginType( 0, wx.stc.STC_MARGIN_NUMBER );
self.m_pawnedit.SetMarginWidth( 0, self.m_pawnedit.TextWidth( wx.stc.STC_STYLE_LINENUMBER, "_99999" ) )
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDER, wx.stc.STC_MARK_BOXPLUS )
self.m_pawnedit.MarkerSetBackground( wx.stc.STC_MARKNUM_FOLDER, wx.BLACK)
self.m_pawnedit.MarkerSetForeground( wx.stc.STC_MARKNUM_FOLDER, wx.WHITE)
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDEROPEN, wx.stc.STC_MARK_BOXMINUS )
self.m_pawnedit.MarkerSetBackground( wx.stc.STC_MARKNUM_FOLDEROPEN, wx.BLACK )
self.m_pawnedit.MarkerSetForeground( wx.stc.STC_MARKNUM_FOLDEROPEN, wx.WHITE )
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDERSUB, wx.stc.STC_MARK_EMPTY )
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDEREND, wx.stc.STC_MARK_BOXPLUS )
self.m_pawnedit.MarkerSetBackground( wx.stc.STC_MARKNUM_FOLDEREND, wx.BLACK )
self.m_pawnedit.MarkerSetForeground( wx.stc.STC_MARKNUM_FOLDEREND, wx.WHITE )
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDEROPENMID, wx.stc.STC_MARK_BOXMINUS )
self.m_pawnedit.MarkerSetBackground( wx.stc.STC_MARKNUM_FOLDEROPENMID, wx.BLACK)
self.m_pawnedit.MarkerSetForeground( wx.stc.STC_MARKNUM_FOLDEROPENMID, wx.WHITE)
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDERMIDTAIL, wx.stc.STC_MARK_EMPTY )
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDERTAIL, wx.stc.STC_MARK_EMPTY )
self.m_pawnedit.SetSelBackground( True, wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT ) )
self.m_pawnedit.SetSelForeground( True, wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT ) )
self.m_pawnedit.SetLexer(wx.stc.STC_LEX_CPP)
self.m_pawnedit.SetHighlightGuide(1)
我尝试启用 C++ 突出显示。但它不起作用。执行代码时没有错误。
解决方案。
- 设置 C++ 词法分析器
m_pawnedit.SetLexer(wx.stc.STC_LEX_CPP)
- 添加关键字
kw = ['assert','break','continue','else','exit','for','if','return','try','new','while','public','const','using','int','namespace','enum','case','switch','default','goto','do','true','false','static','operator','char','bool']
m_pawnedit.SetKeyWords(0, " ".join(kw))
- 设置字符串、字符、数字等的样式
faces = { 'times': 'Times New Roman',
'mono' : 'Consolas',
'helv' : 'Consolas',
'other': 'Consolas',
'size' : 9,
'size2': 9,
}
#Set the default style
m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_DEFAULT, "face:%(helv)s,size:%(size)d" % faces)
m_pawnedit.StyleClearAll()
m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_DEFAULT, "face:%(helv)s,size:%(size)d" % faces)
m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_LINENUMBER, "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces)
m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_CONTROLCHAR, "face:%(other)s" % faces)
#Specify the style for brackets
m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_BRACELIGHT, "fore:#FFFFFF,back:#0000FF,bold")
m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_BRACEBAD, "fore:#000000,back:#FF0000,bold")
#----
m_pawnedit.StyleSetSpec(wx.stc.STC_C_DEFAULT, "fore:#000000,face:%(helv)s,size:%(size)d" % faces) #For default words
m_pawnedit.StyleSetSpec(wx.stc.STC_C_COMMENTLINE, "fore:#007F00,face:%(other)s,size:%(size)d" % faces) #For // comments
m_pawnedit.StyleSetSpec(wx.stc.STC_C_COMMENT, "fore:#007F00,face:%(other)s,size:%(size)d" % faces) #For /*Text*/ comments
m_pawnedit.StyleSetSpec(wx.stc.STC_C_NUMBER, "fore:#007F7F,size:%(size)d" % faces) #For numbers
m_pawnedit.StyleSetSpec(wx.stc.STC_C_STRING, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces) #For strings
m_pawnedit.StyleSetSpec(wx.stc.STC_C_CHARACTER, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces) #For symbols
m_pawnedit.StyleSetSpec(wx.stc.STC_C_WORD, "fore:#0000FF,bold,size:%(size)d" % faces) #For keywords
m_pawnedit.StyleSetSpec(wx.stc.STC_C_PREPROCESSOR, "fore:#0000FF,bold,size:%(size)d" % faces) #For preprocessor
m_pawnedit.StyleSetSpec(wx.stc.STC_C_OPERATOR, "bold,size:%(size)d" % faces) #For operators
m_pawnedit.StyleSetSpec(wx.stc.STC_C_IDENTIFIER, "fore:#000000,face:%(helv)s,size:%(size)d" % faces) #For identifiers
m_pawnedit.StyleSetSpec(wx.stc.STC_C_STRINGEOL, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % faces) #For EOL
- 填色
m_pawnedit.Colourise(0,-1)
所有STC常量都可以在“Python\Python3x\Lib\site-packages\wx”下的文件“stc.pyi”中找到
我正在尝试启用 C++ 突出显示,但没有突出显示任何内容。 我的代码:
self.m_pawnedit = wx.stc.StyledTextCtrl( self.m_panel1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.BORDER_NONE)
self.m_pawnedit.SetUseTabs ( True )
self.m_pawnedit.SetTabWidth ( 4 )
self.m_pawnedit.SetIndent ( 4 )
self.m_pawnedit.SetTabIndents( True )
self.m_pawnedit.SetBackSpaceUnIndents( True )
self.m_pawnedit.SetViewEOL( False )
self.m_pawnedit.SetViewWhiteSpace( False )
self.m_pawnedit.SetMarginWidth( 2, 0 )
self.m_pawnedit.SetIndentationGuides( True )
self.m_pawnedit.SetReadOnly( False );
self.m_pawnedit.SetMarginType ( 1, wx.stc.STC_MARGIN_SYMBOL )
self.m_pawnedit.SetMarginMask ( 1, wx.stc.STC_MASK_FOLDERS )
self.m_pawnedit.SetMarginWidth ( 1, 16)
self.m_pawnedit.SetMarginSensitive( 1, True )
self.m_pawnedit.SetProperty ( "fold", "1" )
self.m_pawnedit.SetFoldFlags ( wx.stc.STC_FOLDFLAG_LINEBEFORE_CONTRACTED | wx.stc.STC_FOLDFLAG_LINEAFTER_CONTRACTED );
self.m_pawnedit.SetMarginType( 0, wx.stc.STC_MARGIN_NUMBER );
self.m_pawnedit.SetMarginWidth( 0, self.m_pawnedit.TextWidth( wx.stc.STC_STYLE_LINENUMBER, "_99999" ) )
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDER, wx.stc.STC_MARK_BOXPLUS )
self.m_pawnedit.MarkerSetBackground( wx.stc.STC_MARKNUM_FOLDER, wx.BLACK)
self.m_pawnedit.MarkerSetForeground( wx.stc.STC_MARKNUM_FOLDER, wx.WHITE)
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDEROPEN, wx.stc.STC_MARK_BOXMINUS )
self.m_pawnedit.MarkerSetBackground( wx.stc.STC_MARKNUM_FOLDEROPEN, wx.BLACK )
self.m_pawnedit.MarkerSetForeground( wx.stc.STC_MARKNUM_FOLDEROPEN, wx.WHITE )
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDERSUB, wx.stc.STC_MARK_EMPTY )
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDEREND, wx.stc.STC_MARK_BOXPLUS )
self.m_pawnedit.MarkerSetBackground( wx.stc.STC_MARKNUM_FOLDEREND, wx.BLACK )
self.m_pawnedit.MarkerSetForeground( wx.stc.STC_MARKNUM_FOLDEREND, wx.WHITE )
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDEROPENMID, wx.stc.STC_MARK_BOXMINUS )
self.m_pawnedit.MarkerSetBackground( wx.stc.STC_MARKNUM_FOLDEROPENMID, wx.BLACK)
self.m_pawnedit.MarkerSetForeground( wx.stc.STC_MARKNUM_FOLDEROPENMID, wx.WHITE)
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDERMIDTAIL, wx.stc.STC_MARK_EMPTY )
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDERTAIL, wx.stc.STC_MARK_EMPTY )
self.m_pawnedit.SetSelBackground( True, wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT ) )
self.m_pawnedit.SetSelForeground( True, wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT ) )
self.m_pawnedit.SetLexer(wx.stc.STC_LEX_CPP)
self.m_pawnedit.SetHighlightGuide(1)
我尝试启用 C++ 突出显示。但它不起作用。执行代码时没有错误。
解决方案。
- 设置 C++ 词法分析器
m_pawnedit.SetLexer(wx.stc.STC_LEX_CPP)
- 添加关键字
kw = ['assert','break','continue','else','exit','for','if','return','try','new','while','public','const','using','int','namespace','enum','case','switch','default','goto','do','true','false','static','operator','char','bool']
m_pawnedit.SetKeyWords(0, " ".join(kw))
- 设置字符串、字符、数字等的样式
faces = { 'times': 'Times New Roman',
'mono' : 'Consolas',
'helv' : 'Consolas',
'other': 'Consolas',
'size' : 9,
'size2': 9,
}
#Set the default style
m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_DEFAULT, "face:%(helv)s,size:%(size)d" % faces)
m_pawnedit.StyleClearAll()
m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_DEFAULT, "face:%(helv)s,size:%(size)d" % faces)
m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_LINENUMBER, "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces)
m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_CONTROLCHAR, "face:%(other)s" % faces)
#Specify the style for brackets
m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_BRACELIGHT, "fore:#FFFFFF,back:#0000FF,bold")
m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_BRACEBAD, "fore:#000000,back:#FF0000,bold")
#----
m_pawnedit.StyleSetSpec(wx.stc.STC_C_DEFAULT, "fore:#000000,face:%(helv)s,size:%(size)d" % faces) #For default words
m_pawnedit.StyleSetSpec(wx.stc.STC_C_COMMENTLINE, "fore:#007F00,face:%(other)s,size:%(size)d" % faces) #For // comments
m_pawnedit.StyleSetSpec(wx.stc.STC_C_COMMENT, "fore:#007F00,face:%(other)s,size:%(size)d" % faces) #For /*Text*/ comments
m_pawnedit.StyleSetSpec(wx.stc.STC_C_NUMBER, "fore:#007F7F,size:%(size)d" % faces) #For numbers
m_pawnedit.StyleSetSpec(wx.stc.STC_C_STRING, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces) #For strings
m_pawnedit.StyleSetSpec(wx.stc.STC_C_CHARACTER, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces) #For symbols
m_pawnedit.StyleSetSpec(wx.stc.STC_C_WORD, "fore:#0000FF,bold,size:%(size)d" % faces) #For keywords
m_pawnedit.StyleSetSpec(wx.stc.STC_C_PREPROCESSOR, "fore:#0000FF,bold,size:%(size)d" % faces) #For preprocessor
m_pawnedit.StyleSetSpec(wx.stc.STC_C_OPERATOR, "bold,size:%(size)d" % faces) #For operators
m_pawnedit.StyleSetSpec(wx.stc.STC_C_IDENTIFIER, "fore:#000000,face:%(helv)s,size:%(size)d" % faces) #For identifiers
m_pawnedit.StyleSetSpec(wx.stc.STC_C_STRINGEOL, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % faces) #For EOL
- 填色
m_pawnedit.Colourise(0,-1)
所有STC常量都可以在“Python\Python3x\Lib\site-packages\wx”下的文件“stc.pyi”中找到