0

我正在使用马拉雅拉姆语文本编辑器。我需要在按钮单击时使用 WxPython 中的文本控件加粗选定的文本。工作环境是 Ubuntu 10.04 和 WxPython 2.8。

这是我的代码:

import wx

class TestFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(TestFrame, self).__init__(*args, **kwargs)

        panel = wx.Panel(self)

        text = 'Highlight some text and then click the button below'

        self.textctrl = wx.TextCtrl(panel, value=text, style=wx.TE_RICH2,
                                    size=(300, -1))

        btn = wx.Button(panel, wx.ID_BOLD)
        btn.Bind(wx.EVT_BUTTON, self.on_bold)

        pSizer = wx.BoxSizer(wx.VERTICAL)
        pSizer.Add(self.textctrl, 0, wx.ALL, 5)
        pSizer.Add(btn, 0, wx.ALL, 5)
        panel.SetSizer(pSizer)

        vSizer = wx.BoxSizer(wx.VERTICAL)
        vSizer.Add(panel, 1, wx.EXPAND)
        self.SetSizer(vSizer)
        self.Layout()

    def on_bold(self, event):
        start, end = self.textctrl.GetSelection()
        font = self.textctrl.GetFont()
        font.MakeBold()
        text_attr = wx.TextAttr(self.textctrl.ForegroundColour,
                                self.textctrl.BackgroundColour, font)
        self.textctrl.SetStyle(start, end, text_attr)


if __name__ == '__main__':
    wxapp = wx.App(False)
    testFrame = TestFrame(None)
    testFrame.Show()
    wxapp.MainLoop()

错误

Traceback (most recent call last):
  File "C:/Python27/gdg", line 29, in on_bold
    font.MakeBold()
AttributeError: 'Font' object has no attribute 'MakeBold'
4

1 回答 1

0
import wx


class TestFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(TestFrame, self).__init__(*args, **kwargs)

        panel = wx.Panel(self)

        text = 'Highlight some text and then click the button below'

        self.textctrl = wx.TextCtrl(panel, value=text, style=wx.TE_RICH2,
                                    size=(300, -1))

        btn = wx.Button(panel, wx.ID_BOLD)
        btn.Bind(wx.EVT_BUTTON, self.on_bold)

        pSizer = wx.BoxSizer(wx.VERTICAL)
        pSizer.Add(self.textctrl, 0, wx.ALL, 5)
        pSizer.Add(btn, 0, wx.ALL, 5)
        panel.SetSizer(pSizer)

        vSizer = wx.BoxSizer(wx.VERTICAL)
        vSizer.Add(panel, 1, wx.EXPAND)
        self.SetSizer(vSizer)
        self.Layout()

    def on_bold(self, event):
        start, end = self.textctrl.GetSelection()
        font = self.textctrl.GetFont()
        font.MakeBold()
        text_attr = wx.TextAttr(self.textctrl.ForegroundColour,
                                self.textctrl.BackgroundColour, font)
        self.textctrl.SetStyle(start, end, text_attr)


if __name__ == '__main__':
    wxapp = wx.App(False)
    testFrame = TestFrame(None)
    testFrame.Show()
    wxapp.MainLoop()
于 2013-04-01T12:40:06.113 回答