可绘制可滚动 window 未呈现

Drawable scrollable window not rendering

我正在做一个项目,我想在 ScrolledWindow 内的位图上绘制东西。

它工作正常,我可以画线,但是当我滚动 window 并尝试画一些东西时,它没有渲染。新绘制的线条只会在我“移开”(如滚动回 0、0)然后返回时渲染。

我在 SO 上遇到了一些(很老的)问题,但似乎没有人遇到过这个问题。

这是一个代码示例:

import wx


class MainFrame(wx.Frame):

    def __init__(self, *args, **kw):
        wx.Frame.__init__(self, *args, **kw)
        s = wx.BoxSizer(wx.VERTICAL)
        panel = DrawablePanel(self)
        s.Add(panel, 1, wx.EXPAND)
        self.SetSizer(s)
        self.SetSize(400, 400)


class DrawablePanel(wx.ScrolledWindow):

    def __init__(self, parent):
        super().__init__(parent, id=wx.ID_ANY)
        self.SetScrollbars(10, 10, 100, 100)

        self.buffer = wx.Bitmap((1000, 1000))
        cdc = wx.ClientDC(self)
        self.DoPrepareDC(cdc)
        dc = wx.BufferedDC(cdc, self.buffer)
        dc.Clear()

        self.mouse_pos = None

        self.Bind(wx.EVT_PAINT, self.on_paint)
        self.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down)
        self.Bind(wx.EVT_MOTION, self.on_motion)

    def on_paint(self, evt):
        wx.BufferedPaintDC(self, self.buffer, wx.BUFFER_VIRTUAL_AREA)

    def on_mouse_down(self, evt):
        self.mouse_pos = self.CalcUnscrolledPosition(evt.GetPosition()).Get()

    def on_motion(self, evt):
        if evt.Dragging() and evt.LeftIsDown():
            new_pos = self.CalcUnscrolledPosition(evt.GetPosition()).Get()
            coord = self.mouse_pos + new_pos
            client_device = wx.ClientDC(self)
            self.DoPrepareDC(client_device)
            dc = wx.BufferedDC(client_device, self.buffer)
            dc.DrawLine(*coord)
            self.mouse_pos = new_pos


if __name__ == "__main__":
    app = wx.App()
    wx.InitAllImageHandlers()
    MainFrame(None).Show()
    app.MainLoop()

任何帮助将不胜感激。 提前致谢,

杰里米

只需为面板添加一个'Refresh'命令window:

Refresh(self, eraseBackground=True, rect=None) Causes this window, and all of its children recursively, to be repainted.

def on_motion(self, evt):
    if evt.Dragging() and evt.LeftIsDown():
        new_pos = self.CalcUnscrolledPosition(evt.GetPosition()).Get()
        coord = self.mouse_pos + new_pos
        client_device = wx.ClientDC(self)
        self.DoPrepareDC(client_device)
        dc = wx.BufferedDC(client_device, self.buffer)
        dc.DrawLine(*coord)
        self.mouse_pos = new_pos
    self.Refresh()