[Python] How to Draw (directly) on Screen in Windows with Python?

Discussão em 'Python' iniciado por Stack, Outubro 2, 2024.

  1. Stack

    Stack Membro Participativo

    I want to create visual hints for users on their screens, but I got struck finding a simple solution how to do basic drawings straight on the screen without limiting the user actions (under Windows with Python 3.x).

    After a search the only - not properly working - solution I found was using wxPython. Here's the code:

    import wx

    # init
    app=wx.App()
    dc=wx.ScreenDC()

    # set line and fill style
    dc.SetBrush(wx.TRANSPARENT_BRUSH)
    dc.SetPen(wx.Pen((255, 0, 0), width=3, style=wx.PENSTYLE_SOLID))

    # draw (x, y, width, height)
    dc.DrawRectangle(100, 100, 200, 100)


    The code draws on the screen, but the result barely becomes visible as the screen is redrawn (by Windows) very quick. I tried a work-around in repeating the drawing command with a for-loop, but also the flickering rectangle is barely visible (and this is nothing that I like to show to my clients).

    A bit better (close to sufficient) is using a transparent TKinter window (without header) and display it for a - shorter - period of time. Here's the WORKING code of that (with one downside that is explained below the code):

    from tkinter import *

    def HighlightSection(Rect=(100,100,300,200), Color = 'red', Duration = 3):
    win= Tk()
    GeometryString = str(Rect[0])+'x'+str(Rect[1])+'+' \
    +str(Rect[2])+'+'+str(Rect[3])
    win.geometry(GeometryString) # "200x100+300+250" # breite, höhe, x, y #
    win.configure(background=Color)
    win.overrideredirect(1)
    win.attributes('-alpha', 0.3)
    win.wm_attributes('-topmost', 1)
    win.after(Duration * 1000, lambda: win.destroy())
    win.mainloop()


    One thing here I could not make working: Any chance to make this TKinter "window" click-trough? Then this would be sufficient (close to quite good). As long as it is not click-trough the user cannot act in/under the highlighted area!

    Is there a simple, solid solution to make draws (line, rectangles, text) on the screen and take it off again after a defined period of time? Any help is appreciated! Thanks in advance, Ulrich!

    Continue reading...

Compartilhe esta Página