更改Canvas ScaledText

时间:2016-12-10 00:31:58

标签: python canvas wxpython wxwidgets

我正在使用wxPython Canvas'AddScaledTextBox(...)来创建缩放的文本框。

例如见here

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

如何在定义文本框后更改背景颜色?(例如用户输入=>背景颜色更改)

2 个答案:

答案 0 :(得分:1)

ScaledTextBox类派生自TextObjectMixin,其SetColorSetBackgroundColor方法。

代码:

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

编辑: 颜色变化后强制重绘画布。此外,由于某些原因,更改背景颜色并不坚持,因此我切换到演示更改文本颜色。

删除了辅助编辑,因为它只是创建一个新文本框而不是更新原始文本。

答案 1 :(得分:1)

很确定这是一个错误。请参阅lines 1841 - 1845 in the wxPython source - 有self.BackgroundColor的引用,但它从未使用过。

因此,对于您的示例,您需要设置与文本框关联的画笔的颜色。我的基础是Robin Dunn的例子。函数SetBoxBackground是重要的部分。

正如评论中所述,只是调用box.Brush.SetColour(color)box.Pen.SetColour(color)会导致问题:如果您有两个具有相同颜色的文本框,则更改一个的笔刷/笔颜色也会影响另一个。 (我不是百分百肯定,但我认为这是因为缓存,请参见this comment in the source。)

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