如何(正确地)将 self.Refresh() 与屏幕刷新同步以避免闪烁绘图?

How to (properly) synchronise self.Refresh() with the screen refresh to avoid blinking drawing?

我正在尝试为 2d 对象(绘制为路径)制作动画,因此需要重新绘制。在没有闪烁对象的情况下重绘它的最佳方法是什么?

在调用 onIdle-Event 时用 self.Refresh() 重绘后,我使用一个固定时间的定时器来调用 self.Refresh(),效果更好。但我仍然遇到对象闪烁的问题。

import wx
import math
import time

class ObjectDrawer(wx.Frame):

    def __init__(self, *args, **kw):
        # Initialize vars
        self.dc = None
        self.gc = None
        self.lastTime = time.time()
        super(ObjectDrawer, self).__init__(*args, **kw)
        self.InitUI()

    def InitUI(self):
        self.timer = wx.Timer(self)
        # Initialize the GUI
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_TIMER, self.evt_timer)
        self.ShowFullScreen(True)
        self.SetBackgroundColour('white')

    def evt_timer(self, event):
        self.Refresh()

    def drawObjects(self):
        path = self.gc.CreatePath()
        #Add Something to the path e.g. a circle
        path.AddCircle(100,100,50)
        self.gc.StrokePath(path)
        path = None

    def OnPaint(self, e):
        dc = wx.PaintDC(self)
        self.gc = wx.GraphicsContext.Create(dc)
        self.gc.SetPen(wx.Pen('#e8b100', 5, wx.LONG_DASH))
        self.drawObjects()
        self.timer.Start(1000/60)


app = wx.App()
window = ObjectDrawer(None)
window.Show()
app.MainLoop()

如果将 self.Refresh() 设置为 self.Refresh(False),闪烁就会消失。您也可以使用 wx.AutoBufferedPaintDC 而不是 wx.PaintDC。查看 wxpython wiki 中的 example 以获得更复杂的示例。