wx Python - 颜色编码 - StyledTextctrl

问题描述 投票:0回答:1

My Problem:

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

我目前正在导入此模块:

import wx
import wx.stc
import os, sys

I have tried:

我从这里尝试过代码:Here但是它没有做“some”关键字但旁边有“all”字样

Screenshot:

任何帮助都会得到很好的赞赏

python windows python-2.7 wxpython wxwidgets
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.