wxPython wxTextCtrl 中每行的不同前景色

发布于 2024-12-09 05:40:36 字数 191 浏览 0 评论 0原文

我有一个多行

wx.TextCtrl()

对象,我设置它的前景和背景颜色来编写字符串。我需要用不同的颜色编写不同的行,

wx.TextCtrl.setForgroundcolor()

也更改所有以前的行颜色。有没有办法解决这个问题?

I have a multiline

wx.TextCtrl()

object which I set it's forground and Background colors for writing strings.I need to write different lines with different colors ,

wx.TextCtrl.setForgroundcolor()

changes all previous lines colors as well.Is there a way around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美羊羊 2024-12-16 05:40:36

wx.Python中有几种方法可以获取彩色文本。

  • wx.TextCtrlwx.TE_RICHwx.TE_RICH2 样式
  • wx.stc.StyledTextCtrl
  • wx. richtext.RichTextCtrl
  • wx.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"))

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()

如其他答案所示,如果您使用多行文本(而不是多行文本),则使用 wx.ListCrtl 可能是一种非常直接的方法。

There are several methods in wx.Python to get colored text.

  • wx.TextCtrl with wx.TE_RICH, wx.TE_RICH2 styles
  • wx.stc.StyledTextCtrl
  • wx.richtext.RichTextCtrl
  • wx.HtmlWindow (inserting color tags in your text)
  • wx.ListCrtl

You can get examples of all of them in the wxPython demo

For example, you can change fore and background colors in any part of a 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 is also easy to use to write lines with different colors:

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()

As indicated in other answer the use of a wx.ListCrtl can be a very straighforward method if you work with lines of text (instead of multiline text).

淡忘如思 2024-12-16 05:40:36

如果您需要在插入之前设置线条颜色,您可以更改基本属性:

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

这是示例:

#!/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()

结果:

在此处输入图像描述

If you need set color for line before inserting you can change basic properties:

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

Here is example:

#!/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()

Result:

enter image description here

怼怹恏 2024-12-16 05:40:36

wx.TextCtrl 的替代方案是 wx.ListCtrl 它有一个每行界面。

您可以使用以下命令设置 wx.ListCtrl 的颜色
设置ItemBackgroundColour

SetItemTextColour

An alternative to wx.TextCtrl is wx.ListCtrl which has a per-line interface.

You can set colors of the wx.ListCtrl with
SetItemBackgroundColour
and
SetItemTextColour

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文