Skip to main content
 首页 » 编程设计

python之在 Enthought TraitsUI TextEditor 中更改字体

2024年08月28日32jiqing9006

我想在 TraitsUI View 中更改 TextEditor 中的字体。我怎样才能做到这一点?

我阅读了(优秀的)文档、API 引用文档并向 Google 寻求答案,但找不到答案。

我的应用程序不需要平台和工具包独立性。我在 Windows 上工作并使用 wx 工具包。

请您参考如下方法:

在深入研究源代码并进行一些试验后,我提出了以下解决方案。对我来说,这似乎太复杂了(我必须对两个类进行子类化!)不是最简单的或预期的方法。

如果有更好的解决方案,我很乐意学习。

import wx 
from traitsui.wx.text_editor import CustomEditor 
from traitsui.editors.text_editor import ToolkitEditorFactory 
 
class _MyTextEditor(CustomEditor): 
    def init(self, parent): 
        CustomEditor.init(self, parent) 
        font = wx.Font(10, wx.FONTFAMILY_MODERN, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL) 
        self.control.SetFont(font) 
 
class MyTextEditor(ToolkitEditorFactory): 
    klass = _MyTextEditor 
    def _get_custom_editor_class(self): 
        return _MyTextEditor 
    def _get_simple_editor_class(self): 
        return _MyTextEditor 
 
if __name__ == "__main__": 
    from traitsui.api import View, Item 
    from traits.api import Str, HasTraits 
 
    class MyTestcase(HasTraits): 
        a_string = Str() 
        traits_view = View(Item('a_string', editor=MyTextEditor())) 
 
    w = MyTestcase() 
    w.configure_traits()