clearing lines drawn on screen

I am working to make a program that will draw a line to the mouse position so that one could track it.

I have used wx.ScreenDC to draw the line and put it in a loop so that a new line is made every time the mouse pointer changes location.

The problem now is that as soon as I change the mouse position a new line comes but not all of the old lines are not removed. I have tried using the .Clear() method but this does not work. this link suggests using .refresh() method but I cannot find that for ScreenDC.

The screenshot is attached and code is also attached

I do not want this excess of lines. How can I remove them?
while True:

class Frame(wx.Frame):

    """docstring for Frame.wx.Frame."""

    pass

class App(wx.App):

    """docstring for App"""

    def OnInit(self):

        (a,b) =win32api.GetCursorPos()

        app = wx.App(False)

        s = wx.ScreenDC()

        

        p1=wx.CYAN_PEN

        

        a1=round(1.25*a)

        b1=round(1.25*b)

    

        s.StartDrawingOnTop()           

        s.Pen = p1

        s.DrawLine(0,0,a1,b1)

        s.EndDrawingOnTop()

        s.Clear()

        

        return True

    

if __name__ == '__main__':

    app=App()

    app.MainLoop()

del app

``

spare.png

Jai Javeria wrote:

I am working to make a program that will draw a line to the mouse position so that one could track it.

I have used wx.ScreenDC to draw the line and put it in a loop so that a new line is made every time the mouse pointer changes location.

The problem now is that as soon as I change the mouse position a new line comes but not all of the old lines are not removed. I have tried using the .Clear() method but this does not work. this link <python - How to clear drawing on screen in wxpython? - Stack Overflow; suggests using .refresh() method but I cannot find that for ScreenDC.

Who did you think was going to erase the lines? Remember, when you draw using wx.ScreenDC, you are drawing directly on the desktop. It's not a window you control, you are drawing on the master copy of all those other application windows, without their knowledge. There is no "backup copy" that can be used to fix things up. It is your responsibility to clean up what you have done.

When an application does this kind of thing to its own window, they do it in one of two ways: they either keep a copy of their original window as a bitmap so they can fix it up later, or they draw using an "XOR" operation, so it can be erased by drawing the exact same line again. That second option could work for you, except for two problems. First, you'd have to remember the exact location of the last line you drew, so you can undo it. Second, if any window or any part of the desktop gets redrawn, then it won't have your line any more, and your "undo" will just make it worse.

Overall, what you are doing here is just not practical. The operating system has several interesting mechanisms built-in for drawing attention to the cursor, like temporarily making it very large, or drawing concentric rings, like when you drop a rock in a pond. You COULD do that kind of thing by creating your own transparent window, and drawing on that window. When your window closes, the background windows will repaint themselves.

ยทยทยท

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.