0

I am working on Malayalam Text Editor. I need to Bold selected text using text control in WxPython on button click. The working environment is Ubuntu 10.04 and WxPython 2.8.

Here is my code:

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()

Error

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

1 Answers1

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()
Yoriz
  • 3,595
  • 2
  • 13
  • 20
  • Thanks @Yoriz But getting error Traceback (most recent call last): File "C:/Python27/gdg", line 29, in on_bold font.MakeBold() AttributeError: 'Font' object has no attribute 'MakeBold' –  Apr 02 '13 at 03:26
  • I'm using wxpython version 2.9.4.0 so you will either have to upgrade your version or find out what 2.8 uses rather then MakeBold ps looks like this could be the cure - http://docs.wxwidgets.org/stable/wx_wxfont.html#wxfontsetweight – Yoriz Apr 02 '13 at 05:43