无法让wx.BufferedDC绘制任何内容

1 投票
2 回答
1401 浏览
提问于 2025-04-15 18:43

我遇到了关于设备上下文(DCs)的问题。我正在尝试制作一个应用程序,这个程序需要在屏幕上画很多线条,并且需要更新得非常快。为了避免闪烁,我决定试试缓冲设备上下文。但是当我运行这段代码时,它什么都没有画出来。我到底哪里出错了呢?

import wx

class MainFrame(wx.Frame):
    def __init__(self):
        screensize = wx.GetDisplaySize()
        self.framesize = (screensize[0]/4*3, screensize[1]/4*3)
        wx.Frame.__init__(self, None, -1, "CursorTracker", size=self.framesize,
                          style=wx.SYSTEM_MENU|
                          wx.CAPTION|
                          wx.CLOSE_BOX|
                          wx.MINIMIZE_BOX)
        self.dc = wx.ClientDC(self)
        self.bdc = wx.BufferedDC(self.dc)
        self.SetBackgroundColour(wx.WHITE)
        self.Timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer)
        self.Timer.Start(100)

    def OnTimer(self, event):

        self.bdc.DrawLine(1,1,100,100)


class App(wx.App):
    def OnInit(self):
        frame = MainFrame()
        frame.Show()
        return True

app = App(redirect=False)
app.MainLoop()

2 个回答

2

BufferedDC 这个东西只有在被销毁的时候,才会把里面的内容复制到屏幕上。因为你从来没有销毁 self.bdc,所以它的内容自然就不会显示在屏幕上。所以,你需要想办法让 self.bdc 不再存在(比如可以用一个新的 BufferedDc 实例来替换它:也就是说,在你想让绘图显示出来的“关键时刻”,重新执行赋值 self.bdc = wc.BufferedDc(self.dc))。

3

我之前用过AutoBufferedPaintDC,但发现自己用MemoryDC来做双缓冲更灵活。这里有个模板给你参考。

import wx

class Frame(wx.Frame):
    def __init__(self):
        super(Frame, self).__init__(None, -1, 'CursorTracker')
        self.mdc = None # memory dc to draw off-screen
        self.Bind(wx.EVT_SIZE, self.on_size)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.on_erase)
        self.Bind(wx.EVT_PAINT, self.on_paint)
        w, h = wx.GetDisplaySize()
        w, h = w * 3 / 4, h * 3 / 4
        self.SetSize((w, h))
        self.Center()
        self.on_timer()
    def on_size(self, event):
        # re-create memory dc to fill window
        w, h = self.GetClientSize()
        self.mdc = wx.MemoryDC(wx.EmptyBitmap(w, h))
        self.redraw()
    def on_erase(self, event):
        pass # don't do any erasing to avoid flicker
    def on_paint(self, event):
        # just blit the memory dc
        dc = wx.PaintDC(self)
        if not self.mdc:
            return
        w, h = self.mdc.GetSize()
        dc.Blit(0, 0, w, h, self.mdc, 0, 0)
    def on_timer(self):
        # refresh every N milliseconds
        self.redraw()
        wx.CallLater(100, self.on_timer)
    def redraw(self):
        # do the actual drawing on the memory dc here
        dc = self.mdc
        w, h = dc.GetSize()
        dc.Clear()
        dc.DrawLine(0, 0, w, h)
        self.Refresh()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = Frame()
    frame.Show()
    app.MainLoop()

基本的做法是:

  • 创建一个内存设备上下文(memory dc)用于在屏幕外绘图
  • 如果窗口大小改变了,就调整内存设备上下文的大小并重新绘制
  • 当发生绘制事件时,只需把内存设备上下文的内容复制到绘制设备上下文上
  • 在擦除背景的事件中什么都不做,以避免闪烁
  • 只有在确实需要改变屏幕上的内容时才调用重新绘制

如果你在on_size中保存了那个创建的EmptyBitmap的引用,甚至可以用wxBitmap.SaveFile()把窗口内容保存为图像文件。

撰写回答