【问题标题】:wx Python - colour coding - StyledTextctrlwx Python - 颜色编码 - StyledTextctrl
【发布时间】:2017-06-23 05:10:48
【问题描述】:

我的问题:

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

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

import wx
import wx.stc
import os, sys

我试过了:

我已经尝试过这里的代码:Here 但这不是“一些”关键字,而是它旁边的“全部”字

Screenshot:

任何帮助将不胜感激

【问题讨论】:

  • 你能显示更多代码吗? IE。你是如何创建 Scintilla 对象并设置词法分析器的?
  • 您必须在关键字列表中包含“True”和“False”(您可以有多个),然后为该特定列表分配颜色。不用说,如果您将词法分析器设置为 Python,它也会为您节省大量工作。
  • 我需要安装 scintilla 吗?
  • 不,你不需要。 wxStyledTextCtrl 应该可以完成这项工作。 docs.wxwidgets.org/3.1/classwx_styled_text_ctrl.html

标签: python windows python-2.7 wxpython wxwidgets


【解决方案1】:

首先派生一个类,它是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

【讨论】:

  • 好的 谢谢大家的帮助!我会试试我得到的
猜你喜欢
  • 1970-01-01
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
  • 2020-07-02
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 2011-06-05
相关资源
最近更新 更多