wx Python - 颜色编码 - StyledTextctrl

时间:2017-02-06 09:51:03

标签: python windows python-2.7 wxpython wxwidgets

我的问题:

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

我目前正在导入此模块:

import wx
import wx.stc
import os, sys

我试过了:

我从这里尝试过代码:Here 但这并不是“一些”关键词,而是“全部”字样旁边的

Screenshot:

任何帮助都会得到非常赞赏

1 个答案:

答案 0 :(得分:0)

首先派生一个类,它是wxStyledTextCtrl

的子类
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
相关问题