Python Forum

Full Version: How to clear the screen of glitched rectangles?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm coding a program that captures a section of the screen as an image by drawing a rectangle with cursor, yet the process creates glitched rectangles. Is there a way to refresh the screen or maybe a completely different code that works? The dc.Clear() doesn't seem to clear the screen.

import wx 
import win32gui

app = wx.PySimpleApp() 
dc = wx.ScreenDC() 

dc.StartDrawingOnTop(None) 
dc.SetPen(wx.Pen('red', 2)) 
dc.SetBrush(wx.TRANSPARENT_BRUSH) 

int_x = win32gui.GetCursorPos()[0]
int_y = win32gui.GetCursorPos()[1]


while 1:
    dc.DrawRectangle(int_x,int_y,win32gui.GetCursorPos()[0]-int_x,win32gui.GetCursorPos()[1]-int_y)
    dc.Clear()
The dc.clear() which should work but it's part of an endless while loop.

The 'jaggard' rectangles is is because the rectangle is constantly being drawn, then erased and the cycle repeated.
  • Remove the while
  • place the clear in a function
  • only call the clear function when you are actually ready to clear.