wxpython,如何在textctrl中设置背景颜色和前景色?

时间:2015-03-14 08:25:25

标签: macos wxpython

我想为文字,背景和前景色都设置颜色。我首先学习了tkinter,但似乎wxpython没有类似的方法。

以下代码易于测试,复制" hello world,Hello World,heLLo WORLD"到area_example,告诉我如何突出显示"你好",ignorecase


#!/usr/bin/env python
# coding=utf8

import wx

rows = 5
cols = 2
vgap = 20
hgap = 10


class Search(wx.Frame):
    #not_resizable = wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX)  # ok
    not_resizable = wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX)

    def __init__(self, parent, title, size):
        super(Search, self).__init__(parent, title=title, size=size, style=self.not_resizable)
        self.init_elements()
        self.lay_out()
        self.Centre()
        self.Show()

    def init_elements(self):
        self.panel = wx.Panel(self)
        self.entry_name = wx.TextCtrl(self.panel)
        self.entry_name.SetFocus()

        self.btn_add = wx.Button(self.panel, label="Add")
        self.btn_add.Disable()

        self.btn_recite = wx.Button(self.panel, label="Recite")
        self.btn_recite.Disable()

        self.btn_flash = wx.Button(self.panel, label="Flash")
        self.btn_flash.Disable()

        self.label_phonetic = wx.StaticText(self.panel, label='')
        self.area_meaning = wx.TextCtrl(self.panel, style=wx.TE_MULTILINE)
        self.area_example = wx.TextCtrl(self.panel, style=wx.TE_MULTILINE)

        self.btn_save = wx.Button(self.panel, label="Save")
        self.btn_sort = wx.Button(self.panel, label="Sort")

    def lay_out(self):
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        grid = wx.FlexGridSizer(rows, cols, vgap, hgap)
        grid.AddMany([
            (self.entry_name), (self.btn_add),
            (self.label_phonetic), (self.btn_recite),
            (self.area_meaning, 1, wx.EXPAND), (self.btn_flash),
            (self.area_example, 1, wx.EXPAND), (self.btn_sort),
            (self.btn_save),
        ])
        grid.AddGrowableCol(0, 1)
        grid.AddGrowableRow(2, 1)
        grid.AddGrowableRow(3, 1)
        hbox.Add(grid, proportion=1, flag=wx.ALL | wx.EXPAND, border=15)
        self.panel.SetSizer(hbox)
        #self.panel.SetSizerAndFit(hbox)

    def OnKeyUp(self, e):
        code = e.GetKeyCode()
        if code == wx.WXK_RETURN:
            self.enter_handler(e)

    def enter_handler(self, e):
        word = self.entry_name.GetValue()
        if word:
            self.highlight(word)

    def highlight(self, name):
        # todo
        # add background color and foreground color, ignore case
        print 'highlight'


def search_test():
    app = wx.App()
    title = 'Search Test'
    size = (800, 500)
    s = Search(None, title, size)
    s.entry_name.Bind(wx.EVT_KEY_UP, s.OnKeyUp)
    app.MainLoop()


if __name__ == '__main__':
    search_test()

当我问这个问题时的doc版本是wxPython 3.0.3,最后更新于2015年3月13日,修订版1725 + 2c3b7a8。

但osx上的wxPython版本brew安装版本为3.0.2,某些类和方法不可用。

1 个答案:

答案 0 :(得分:1)

在文档中,它解释了如何执行此操作:http://wxpython.org/Phoenix/docs/html/TextCtrl.html#phoenix-title-textctrl-styles

以下是一个示例代码段(应该低于您对self.area_example的定义)

self.area_example.SetDefaultStyle(wx.TextAttr(wx.RED))
self.area_example.AppendText("Red text\n")
self.area_example.SetDefaultStyle(wx.TextAttr(wx.NullColour,
                                                  wx.LIGHT_GREY))
self.area_example.AppendText("Red on grey text\n")
self.area_example.SetDefaultStyle(wx.TextAttr(wx.BLUE))
self.area_example.AppendText("Blue on grey text\n")

至于检查单词是否为“hello”,我现在只能想到绑定它并检查它。

self.area_example.Bind(wx.EVT_CHAR, self.OnKeyDown)

“OnKeyDown”功能只是一个例子。它运行但你很可能想要一个更好的方法。

def OnKeyDown(self, e):
    last_word = self.area_example.GetValue().split()[-1]
    if last_word.lower() == "hello":
        print("Change color")
    e.Skip()

从那里你应该能够完成你所需要的。