简体   繁体   English

更改Canvas ScaledText的背景颜色

[英]Change background color of a Canvas ScaledText

I'm using wxPython Canvas ' AddScaledTextBox(...) to create a scaled Text Box. 我正在使用wxPython AddScaledTextBox(...)创建缩放的文本框。

See here for example: 参见这里例如:

Box = Canvas.AddScaledTextBox("A Two Line\nString",
                                  Point,
                                  2,
                                  BackgroundColor = "Yellow",
                                  LineColor = "Red",
                                  LineStyle = "Solid",
                                  PadSize = 5,
                                  Family = wx.TELETYPE,
                                  Position = 'bl')

How to change the Background color, later, after the textbox has been defined? 在定义文本框之后,如何更改背景颜色? (for example user input => background color change) (例如,用户输入=>背景颜色更改)

The ScaledTextBox class derives from TextObjectMixin which has SetColor and SetBackgroundColor methods. ScaledTextBox类从具有SetColorSetBackgroundColor方法的TextObjectMixin派生。

Code: 码:

import wx
from wx.lib.floatcanvas import FloatCanvas

class TestFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        self.canvas = FloatCanvas.FloatCanvas(self, BackgroundColor = "black")
        MainSizer = wx.BoxSizer(wx.VERTICAL)
        MainSizer.Add(self.canvas, 4, wx.EXPAND)
        self.SetSizer(MainSizer)
        self.A = self.canvas.AddScaledTextBox('yoda', (0,0), Position = 'tl', Alignment = 'center', PadSize = 10, Size = 15, Width = 150, BackgroundColor = 'white')
        self.A.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.OnRectDown)
        wx.CallAfter(self.canvas.ZoomToBB)

    def OnRectDown(self, event):
        print 'Rectangle: Left Button down clicked at:', event.HitCoords
        if self.A.Color == 'red':
            self.A.SetColor('black')
        else:
            self.A.SetColor('red')
        self.canvas.Draw(True)

app = wx.App(0)
frame = TestFrame(None, title="Test")
frame.Show(True)
app.MainLoop()

EDIT: Forced a redraw of the canvas after the color change. 编辑:更改颜色后强制重新绘制画布。 Also, changing the background color isn't sticking for some reason, so I switched to demoing changing the text color. 另外,由于某些原因,更改背景颜色也不会停留,因此我切换到演示更改文本颜色的演示。

Removed secondary edit because it was just creating a new text box instead of updating the original. 删除了辅助编辑,因为它只是创建一个新的文本框而不是更新原始文本框。

Pretty sure this is a bug. 可以肯定这是一个错误。 See lines 1841 - 1845 in the wxPython source - there is a reference to self.BackgroundColor but it's never used. 参见wxPython源代码中的1841至1845行-引用了self.BackgroundColor但从未使用过。

So for your example, you need to set the color of the brush associated with the text box. 因此,对于您的示例,您需要设置与文本框关联的画笔的颜色。 I've based the following off of Robin Dunn's example. 我基于Robin Dunn的示例进行了以下介绍。 The function SetBoxBackground is the important part. 函数SetBoxBackground是重要的部分。

As noted in the comments, just calling box.Brush.SetColour(color) or box.Pen.SetColour(color) can cause issues: if you have two text boxes with the same color, changing the brush/pen color for one will also affect the other. 如评论中所述,仅调用box.Brush.SetColour(color)box.Pen.SetColour(color)可能会引起问题:如果您有两个具有相同颜色的文本框,则更改其中一个的画笔/笔颜色也会影响对方。 (I'm not 100% sure, but I think this is because of caching, see for example this comment in the source .) (我不确定100%,但是我认为这是由于缓存造成的,例如,请参阅源代码中的此注释 。)

import wx
from wx.lib.floatcanvas import FloatCanvas

class TestFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        self.canvas = FloatCanvas.FloatCanvas(self, BackgroundColor = "black")
        MainSizer = wx.BoxSizer(wx.VERTICAL)
        MainSizer.Add(self.canvas, 4, wx.EXPAND)
        self.SetSizer(MainSizer)
        self.box = self.canvas.AddScaledTextBox("A Two Line\nString",
                                  (0,10),
                                  2,
                                  BackgroundColor = "Yellow",
                                  LineColor = "Red",
                                  LineStyle = "Solid",
                                  PadSize = 5,
                                  Family = wx.TELETYPE,
                                  Position = 'bl')
        self.box2 = self.canvas.AddScaledTextBox("Second Box",
                                  (0,0),
                                  2,
                                  BackgroundColor = "Yellow",
                                  LineColor = "Red",
                                  LineStyle = "Solid",
                                  PadSize = 5,
                                  Family = wx.TELETYPE,
                                  Position = 'bl')
        self.box.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.OnRectDown)
        wx.CallAfter(self.canvas.ZoomToBB)

    def OnRectDown(self, event):
        print ('Rectangle: Left Button down clicked at:', event.HitCoords)
        if self.box.Color == 'red':
            self.SetBoxBackground('green', "Solid")
            self.box.SetColor('black')
        else:
            self.SetBoxBackground('white', "Solid")
            self.box.SetColor('red')
        self.canvas.ClearBackground()
        self.canvas.Refresh()
        self.canvas.Draw(True)

    def SetBoxBackground(self, color, style, linewidth=1):
        # See https://github.com/wxWidgets/wxPython/blob/master/wx/lib/floatcanvas/FloatCanvas.py#L1841

        # Create a new brush (fill) with specified color
        self.box.SetBrush(color, style)
        # Create a new pen (line) with specified color
        self.box.SetPen(color, style, linewidth)
        self.box.SetBackgroundColor(color) # Now this works correctly

app = wx.App(0)
frame = TestFrame(None, title="Test")
frame.Show(True)
app.MainLoop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM