wx Python - 颜色编码 - StyledTextctrl

wx Python - colour coding - StyledTextctrl

我的问题:

嗨!我现在已经用谷歌搜索了 2 天,但我还没有找到答案 然而 :( 我想做的是当我输入关键字时(例如 "True" 和 "False")那个词会变成红色

我目前正在导入这个模块:

import wx
import wx.stc
import os, sys

我试过了:

我试过这里的代码:Here 但这不会做 "some" 关键字,而是 "all" 旁边的词

Screenshot:

非常感谢任何帮助

首先导出一个class,它是wxStyledTextCtrl

的子class
class Script:public wxStyledTextCtrl

下面的代码是针对Lua的,但是同样的逻辑适用于任何语言。因此,您需要为 Python 修改它并将代码添加到您的构造函数中。

SetLexer(wxSTC_LEX_LUA); //Dont forget to set the lexer to Python
m_LuaKeywords =_T("and break do else elseif end false for function if in local nil not or repeat return then true until while pairs ipairs");     
SetWordChars(wxT("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMONPQRSTUVWXYZ._") );
SetKeyWords(0,m_LuaKeywords);
SetFont(font);

StyleSetForeground(0,  wxColour(128, 128, 128)); // White space
StyleSetForeground(wxSTC_LUA_COMMENT,  wxColour(0,   127, 0));   // Block Comment
font.SetPointSize(10); font.Italic();
StyleSetFont(wxSTC_LUA_COMMENT, font);
StyleSetUnderline(wxSTC_LUA_COMMENT, false);

StyleSetForeground(wxSTC_LUA_COMMENTLINE, wxColour(0,   127, 0));   //Line Comment
StyleSetFont(wxSTC_LUA_COMMENTLINE,font);    //doc. Comment
StyleSetUnderline(wxSTC_LUA_COMMENTLINE, false);

font.SetPointSize(11);font.SetStyle(wxFONTSTYLE_NORMAL);
StyleSetForeground(wxSTC_LUA_NUMBER, wxColour(127, 127, 127)); // Number
StyleSetFont(wxSTC_LUA_NUMBER, font);

StyleSetForeground(wxSTC_LUA_STRING, wxColour(0,   0,   127)); // Double quoted string
StyleSetBold(wxSTC_LUA_STRING,  true);
StyleSetUnderline(wxSTC_LUA_STRING, false);

如果您查看 wx/stc/stc.h,您会看到 Python 的状态如下:

/// Lexical states for SCLEX_PYTHON
#define wxSTC_P_DEFAULT 0
#define wxSTC_P_COMMENTLINE 1
#define wxSTC_P_NUMBER 2
#define wxSTC_P_STRING 3
#define wxSTC_P_CHARACTER 4
#define wxSTC_P_WORD 5
#define wxSTC_P_TRIPLE 6
#define wxSTC_P_TRIPLEDOUBLE 7
#define wxSTC_P_CLASSNAME 8
#define wxSTC_P_DEFNAME 9
#define wxSTC_P_OPERATOR 10
#define wxSTC_P_IDENTIFIER 11
#define wxSTC_P_COMMENTBLOCK 12
#define wxSTC_P_STRINGEOL 13
#define wxSTC_P_WORD2 14
#define wxSTC_P_DECORATOR 15