我有一条多线
wx.TextCtrl()
Run Code Online (Sandbox Code Playgroud)
我设置它的forground和用于写字符串的背景颜色的对象.我需要用不同的颜色写出不同的线条,
wx.TextCtrl.setForgroundcolor()
Run Code Online (Sandbox Code Playgroud)
改变所有以前的线条颜色.这有什么方法吗?
joa*_*uin 13
wx.Python中有几种方法可以获得彩色文本.
wx.TextCtrl用wx.TE_RICH,wx.TE_RICH2款式wx.stc.StyledTextCtrlwx.richtext.RichTextCtrlwx.HtmlWindow (在文本中插入颜色标签)wx.ListCrtl您可以在wxPython演示中获得所有这些示例
例如,您可以在以下任何部分更改前景色和背景色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"))
Run Code Online (Sandbox Code Playgroud)
wx.richtext 也很容易用来写不同颜色的线条:
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()
Run Code Online (Sandbox Code Playgroud)
如其他答案所示,wx.ListCrtl如果使用文本行(而不是多行文本),则使用a 可能是一种非常直接的方法.
如果在插入之前需要为行设置颜色,可以更改基本属性:
ctrlText.SetDefaultStyle(wx.TextAttr(wx.GREEN,wx.BLACK))
#where wx.GREEN - foreground, wx.BLACK - background for text
ctrlText.SetBackgroundColour(wx.BLACK)
Run Code Online (Sandbox Code Playgroud)
这是一个例子:
#!/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()
Run Code Online (Sandbox Code Playgroud)
结果:
| 归档时间: |
|
| 查看次数: |
5327 次 |
| 最近记录: |