我在马拉雅拉姆文本编辑器工作。我需要在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'
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()