需要在 wxPython 中使用 self.MemoryDC 的帮助

发布于 2024-11-07 08:12:18 字数 1832 浏览 1 评论 0原文

我正在尝试制作一个双缓冲的自定义文本小部件(为了避免闪烁)。 不过,我希望能够做一些事情。然而,我不确定我应该使用的确切方法。

前两个很简单,我只想更改背景和前景色。

所以或多或少我希望能够在 self.Draw() 中更改 self.Text 的文本颜色。

片段:

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

self.MemoryDC 的背景(填充)颜色相同。

接下来,有谁知道我如何居中 self.Text ?最后,创建self.Text后如何配置它?

到目前为止的小部件:

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)

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)

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

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

发布评论

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

评论(1

帅气尐潴 2024-11-14 08:12:18

我不明白你在创建 self.Text 后配置它是什么意思。如果您想在绘制文本后更改文本,则不能。一旦你把它绘制到 DC 上,它就在那里,改变它的唯一方法是清除 DC 并重新绘制它。在您的情况下,更新文本时您所需要做的就是再次调用 Resize() ,强制重绘。请注意,DrawText() 不会返回任何内容,因此 self.Text 的值将为 None。您绝对不能使用 that 来引用绘制的文本。 :D

至于其余部分,下面是一个 Draw() 方法的示例,该方法将文本居中并将其绘制为蓝色:

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)

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