سؤال

a simple example:

t.py

import wx
class MyFrame(wx.Frame):
    def __init__(self,parent):
        wx.Frame.__init__(self,parent,-1,title="my Frame",pos=(100,100),size=(300,400))
        panel=wx.Panel(self,-1)
        Text=wx.TextCtrl(panel,-1,"abc",size=(50,40),style=wx.TE_RICH2|wx.TE_MULTILINE)
        Text.SetDefaultStyle(wx.TextAttr("red"))

if __name__=='__main__':
    app=wx.App()
    frame=MyFrame(None)
    frame.Show()
    app.MainLoop()

system:ubuntu 12.04. I've added wx.TE_RICH2,why wx.TextCtrl.SetDefaultStyle not working? thanks

هل كانت مفيدة؟

المحلول

The problem is that when using the method SetDefaultStyle, it requires that the wx.TextCtrl object have no text inserted before you call it. For example remove the "abc" in the wx.TextCtrl declaration, then run your app and it should work. Ex:

import wx
class MyFrame(wx.Frame):
    def __init__(self,parent):
        wx.Frame.__init__(self,parent,-1,title="my Frame",pos=(100,100),size=(300,400))
        panel=wx.Panel(self,-1)
        # Notice no "abc" parameter
        Text=wx.TextCtrl(panel,-1,size=(50,40),style=wx.TE_RICH2|wx.TE_MULTILINE)
        Text.SetDefaultStyle(wx.TextAttr("red"))

if __name__=='__main__':
    app=wx.App()
    frame=MyFrame(None)
    frame.Show()
    app.MainLoop()

Code should work now. There is also a SetStyle() method that is used if the TextCtrl object already has text in it but this method is less efficient as said by the docs (http://docs.wxwidgets.org/trunk/classwx_text_ctrl.html)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top