我想在TraitsUI视图中的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()

关于python - 在Enthought TraitsUI TextEditor中更改字体,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19111858/

10-12 00:07