2

I'm trying to make a custom text widget that is double buffered (In order to avoid flicker). However, I'd like to be able to do a few things. Yet, I'm unsure of the exact methods I should use.

The first two are easy I simply want to change the background and foreground color.

So more or less I want to be able to change the text color for self.Text in self.Draw().

Snippet:

self.Text = mdc.DrawText(self.TextString, 10, 0) 

As sell as the Background (fill) color for self.MemoryDC.

Next, does anyone know how I could center self.Text? Finally, how do I configure self.Text after it has been created?

The widget thus far:

class DynamicText (wx.Panel):
    def __init__(self, par):
        self.Par = par

        wx.Panel.__init__(self, self.Par)


        self.Time = Time(self, func=self.SetTime)



        self.Dim = self.Par.GetClientSize()
        self.SetSize(self.Dim)

        self.Bind(wx.EVT_SIZE, self.Resize)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.Erase)
        self.Bind(wx.EVT_PAINT, self.Paint)

    def Set (self, text) :
        self.TextString = text

    def SetTime (self, time) :
        self.Set(str(time))
        self.Resize(None)

    def Resize(self, event):

        self.Width, self.Height = self.GetSize()

        bitmap = wx.EmptyBitmap(self.Width, self.Height)

        self.MemoryDC = wx.MemoryDC(bitmap)

        ''' Redraws **self.MemoryDC** '''
        mdc = self.MemoryDC

        ''' Deletes everything from widget. '''
        mdc.Clear()


        fs = 11
        font = wx.Font( fs, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        mdc.SetFont(font)

        self.Draw()

        self.Refresh()



    def Draw (self) :
        mdc = self.MemoryDC
        self.Text = mdc.DrawText(self.TextString, 10, 0)

    def Erase(self, event):
        ''' Does nothing, as to avoid flicker. '''
        pass

    def Paint(self, event):
        pdc = wx.PaintDC(self)
        w, h = self.MemoryDC.GetSize()
        pdc.Blit(0, 0, w, h, self.MemoryDC, 0, 0)
Gareth Rees
  • 64,967
  • 9
  • 133
  • 163
rectangletangle
  • 50,393
  • 94
  • 205
  • 275

1 Answers1

1

I don't understand what you mean by configuring self.Text after it was created. If you want to change the text after you've drawn it - you can't. Once you've drawn it to the DC it's there, and the only way to change it would be to clear the DC and repaint it. In your case, it seems all you need to do when the text is updated is to call Resize() again, forcing a redraw. Note that DrawText() retruns nothing, so the value of your self.Text would be None. You definitely can't use that to refer to the drawn text. :D

As for the rest, here's an example of a Draw() method that centers the text and paints it blue:

def Draw(self) :
    mdc = self.MemoryDC
    dc_width, dc_height = mdc.GetSizeTuple()
    text_width, text_height, descent, externalLeading = mdc.GetFullTextExtent(self.TextString)
    x = (dc_width  - text_width)  / 2
    y = (dc_height - text_height) / 2
    mdc.SetTextForeground('Blue')
    mdc.DrawText(self.TextString, x, y)
Boaz Yaniv
  • 6,334
  • 21
  • 30
  • Yep, I meant the ability to change text after it was drawn as configure. I'm new to wxPython so I'm still trying to get the ins and outs of it. From what I understand **wx.MemoryDC()** can be made to function like a Tkinter Canvas widget. You can configure text on that particular widget without having to redraw it. Thanks! – rectangletangle May 14 '11 at 09:31