You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何为基于wxPython开发的文本编辑器设置深色主题?

为wxPython文本编辑器配置深色主题

嘿,我来帮你搞定wxPython文本编辑器的深色主题配置!结合你提供的代码基础,我给你两种实用的深色主题实现方案,一种是精细定制STC控件样式,另一种是适配系统深色主题,你可以按需选择。

方案1:自定义STC控件深色样式(推荐,定制性强)

wx.stc.StyledTextCtrl支持非常细致的样式调整,我们可以给普通文本、关键字、注释、行号等元素分别设置深色背景和对应前景色,下面是基于你的Editor类扩展的完整代码:

import wx
import wx.lib.dialogs
import wx.stc as stc
import os

class Editor(wx.Frame):
    def __init__(self, parent, title):
        super().__init__(parent, title=title, size=(800,600))
        self.dirname = ''
        self.filename = ''
        self.leftMarginWidth = 25

        # 创建核心编辑控件
        self.stc = stc.StyledTextCtrl(self)
        self.setup_dark_theme()  # 初始化深色主题

        # 初始化菜单/工具栏(保留你原有逻辑)
        self.create_basic_ui()
        self.Show()

    def setup_dark_theme(self):
        # 整体基础样式
        self.stc.SetBackgroundColour('#282c34')  # VS Code深色背景
        self.stc.SetForegroundColour('#abb2bf')  # 普通文本颜色
        self.stc.SetCaretForegroundColour('#ffffff')  # 光标颜色

        # 行号栏样式
        self.stc.SetMargins(5, 5)
        self.stc.SetMarginType(0, stc.STC_MARGIN_NUMBER)
        self.stc.SetMarginWidth(0, self.leftMarginWidth)
        self.stc.SetMarginBackground(0, '#21252b')  # 行号栏背景
        self.stc.SetMarginForeground(0, '#5c6370')  # 行号文字颜色

        # 选中文本样式
        self.stc.SetSelBackground(True, '#3e4451')
        self.stc.SetSelForeground(True, '#ffffff')

        # Python语法高亮配置(可根据你的编辑语言调整)
        self.stc.SetLexer(stc.STC_LEX_PYTHON)
        # 逐个配置语法元素颜色
        style_configs = [
            (stc.STC_P_DEFAULT, '#abb2bf'),          # 普通文本
            (stc.STC_P_COMMENTLINE, '#5c6370'),      # 单行注释
            (stc.STC_P_NUMBER, '#d19a66'),           # 数字
            (stc.STC_P_STRING, '#98c379'),           # 字符串
            (stc.STC_P_WORD, '#61afef'),             # 关键字
            (stc.STC_P_CLASSNAME, '#e5c07b'),        # 类名
            (stc.STC_P_DEFNAME, '#e5c07b'),          # 函数/方法名
        ]
        for style_id, color in style_configs:
            self.stc.StyleSetForeground(style_id, color)
            self.stc.StyleSetBackground(style_id, '#282c34')
            self.stc.StyleSetFont(style_id, wx.Font(10, wx.FONTFAMILY_MODERN, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))

    def create_basic_ui(self):
        # 示例菜单逻辑(替换成你自己的菜单/工具栏代码)
        menubar = wx.MenuBar()
        file_menu = wx.Menu()
        open_item = file_menu.Append(wx.ID_OPEN, '&打开', '打开文件')
        self.Bind(wx.EVT_MENU, self.on_open_file, open_item)
        menubar.Append(file_menu, '&文件')
        self.SetMenuBar(menubar)

    def on_open_file(self, event):
        # 示例打开文件逻辑
        dlg = wx.FileDialog(self, "选择文件", self.dirname, "", "*.*", wx.FD_OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
            with open(os.path.join(self.dirname, self.filename), 'r', encoding='utf-8') as f:
                self.stc.SetText(f.read())
        dlg.Destroy()

if __name__ == '__main__':
    app = wx.App()
    Editor(None, "深色主题文本编辑器")
    app.MainLoop()

方案2:适配系统深色主题(简单,跟随系统)

如果你的wxPython版本是4.0及以上,可以让编辑器跟随系统的深色外观,不过这种方式定制性较弱,STC的语法高亮仍需单独配置:

# 在App初始化前添加系统主题判断
app = wx.App(False)
# 检测系统是否处于深色模式
if wx.SystemSettings.GetAppearance().IsDark():
    # 覆盖系统默认窗口颜色
    wx.SystemSettings.SetColour(wx.SYS_COLOUR_WINDOW, '#282c34')
    wx.SystemSettings.SetColour(wx.SYS_COLOUR_WINDOWTEXT, '#abb2bf')
    # 可根据需要调整更多系统颜色(如按钮、菜单等)

小提示

  • 如果你之前有设置过STC的浅色样式,记得替换掉,避免样式冲突;
  • 颜色值可以自由调整,上面用的是VS Code风格的配色,你也可以换成Monokai、Dracula等流行深色主题的配色;
  • 如果你编辑的是其他编程语言,需要修改对应的Lexer(比如C++用stc.STC_LEX_CPP)和对应的样式编号,可参考wxPython官方文档。

内容的提问来源于stack exchange,提问作者Daniel M

火山引擎 最新活动