我遇到了DC的问题。我正在尝试制作一个应用程序,它将在屏幕上绘制很多行,并且需要更快地更新,因为我不想闪烁,所以我决定给缓冲的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()
答案 0 :(得分: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()
基本方法是:
如果存储对on_size中创建的EmptyBitmap的引用,您甚至可以使用wxBitmap.SaveFile()
将窗口内容保存到图像文件中。答案 1 :(得分:1)
仅当对象被销毁时,才会将BufferedDC复制到屏幕上。由于您永远不会销毁self.bdc
,因此其内容永远不会被复制到屏幕上。所以,你需要安排放开self.bdc
(也许只是用一个新的BufferedDc实例替换它:即,当你想要绘图时,在“战略”时刻重做作业self.bdc = wc.BufferedDc(self.dc)
可视化。