开发者

sending colored text to a TextCtrl in wxpython

I'm trying to send colored text to a TextCtrl widget, but开发者_如何学运维 don't know how

style = wx.TE_MULTILINE|wx.BORDER_SUNKEN|wx.TE_READONLY|wx.TE_RICH2
self.status_area = wx.TextCtrl(self.panel, -1,
                               pos=(10, 270),style=style,
                               size=(380,150))

basically that snippet defines a status box in my window, and I want to write colored log messages to it. If I just do self.status_area.AppendText("blah") it will append text like I want, but it will always be black. I can't find the documentation on how to do this.


You need to call SetStyle to change the text behavior.

import wx

class F(wx.Frame):
    def __init__(self, *args, **kw):
        wx.Frame.__init__(self, None)
        style = wx.TE_MULTILINE|wx.BORDER_SUNKEN|wx.TE_READONLY|wx.TE_RICH2
        self.status_area = wx.TextCtrl(self, -1,
                                       pos=(10, 270),style=style,
                                       size=(380,150))
        self.status_area.AppendText("blahblahhblah")
        fg = wx.Colour(200,80,100)
        at = wx.TextAttr(fg)
        self.status_area.SetStyle(3, 5, at)

app = wx.PySimpleApp()
f = F()
f.Show()
app.MainLoop()


documentation of wxwidgets has this to say (you can also look up wxPython docs, but it points to wxwidgets anyway): either use SetDefaultStyle before you append text to your textctrl, or after inserting text use SetStyle. According to docs, the first solution is more efficient (and sounds easier to me.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜