2011-10-12 7 views

Respuesta

11

Hay varios métodos en wx.Python para conseguir texto en color.

  • wx.TextCtrl con wx.TE_RICH, wx.TE_RICH2 estilos
  • wx.stc.StyledTextCtrl
  • wx.richtext.RichTextCtrl
  • wx.HtmlWindow (insertar etiquetas de color en el texto)
  • wx.ListCrtl

, usted puede obtener ejemplos de todos ellos en el th e wxPython demostración

Por ejemplo, puede cambiar de proa y los colores de fondo en cualquier parte de un wx.TextCrtl:

rt = wx.TextCtrl(self, -1,"My Text....",size=(200, 100),style=wx.TE_MULTILINE|wx.TE_RICH2) 
rt.SetInsertionPoint(0) 
rt.SetStyle(2, 5, wx.TextAttr("red", "blue")) 

wx.richtext también es fácil de usar para escribir líneas con diferentes colores:

rtc = wx.richtext.RichTextCtrl(self, style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER) 
rtc.BeginTextColour((255, 0, 0)) 
rtc.WriteText("this color is red") 
rtc.EndTextColour() 
rtc.Newline() 

Como se indica en otra respuesta, el uso de un wx.ListCrtl puede ser un método muy directo si trabaja con líneas de texto (en lugar de texto de líneas múltiples).

5

Si usted necesita color establecidas para la línea antes de insertar puede cambiar las propiedades básicas:

ctrlText.SetDefaultStyle(wx.TextAttr(wx.GREEN,wx.BLACK)) 
#where wx.GREEN - foreground, wx.BLACK - background for text 
ctrlText.SetBackgroundColour(wx.BLACK) 

Aquí es ejemplo:

#!/usr/bin/python 
# -*- coding: utf-8 -*- 
import wx 

class ColorTextForm(wx.Frame): 

    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, "Colored text") 

     panel = wx.Panel(self, wx.ID_ANY) 
     self.log = wx.TextCtrl(panel, wx.ID_ANY, size=(100,100), 
          style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL|wx.TE_RICH) 

     self.log.SetDefaultStyle(wx.TextAttr(wx.GREEN, wx.BLACK)) 
     #self.log.SetBackgroundColour(wx.BLACK) 
     self.bgColor = wx.WHITE 
     self.log.SetBackgroundColour(self.bgColor) 

     btnRed = wx.Button(panel, wx.ID_ANY, 'Red') 
     btnGreen = wx.Button(panel, wx.ID_ANY, 'Green') 
     self.cbBG = wx.CheckBox(panel, label='White') 
     self.Bind(wx.EVT_BUTTON, self.onButtonRed, btnRed) 
     self.Bind(wx.EVT_BUTTON, self.onButtonGreen, btnGreen) 
     self.Bind(wx.EVT_CHECKBOX, self.onCheckChangeBG, self.cbBG) 

     sizer = wx.BoxSizer(wx.VERTICAL) 
     sizer.Add(self.log, 1, wx.ALL|wx.EXPAND, 5) 
     sizer.Add(btnRed, 0, wx.ALL|wx.CENTER, 5) 
     sizer.Add(btnGreen, 0, wx.ALL|wx.CENTER, 5) 
     sizer.Add(self.cbBG, 0, wx.ALL|wx.CENTER, 5) 
     panel.SetSizer(sizer) 

    def onButtonGreen(self, event): 
     self.log.SetDefaultStyle(wx.TextAttr(wx.GREEN, self.bgColor)) 
     self.log.AppendText("Geen\n") 

    def onButtonRed(self, event): 
     self.log.SetDefaultStyle(wx.TextAttr(wx.RED,self.bgColor)) 
     self.log.AppendText("Red\n") 

    def onCheckChangeBG(self, e): 
     sender = e.GetEventObject() 
     isChecked = sender.GetValue() 
     if isChecked: 
      self.bgColor = wx.BLACK 
      self.cbBG.SetLabel('Black') 
     else: 
      self.bgColor = wx.WHITE 
      self.cbBG.SetLabel('White') 

if __name__ == "__main__": 
    app = wx.PySimpleApp() 
    frame = ColorTextForm().Show() 
    app.MainLoop() 

Resultado:

enter image description here

Cuestiones relacionadas