Python draw on the screen

Asked

Viewed 742 times

2

Does anyone know if there’s any method I can draw on a pure canvas? Without the use of a graphical interface window, I will give an example to better understand

(Ignore where you’re pointing) the question is, is there any way to draw on the canvas without windows? Like draw the same in the image .

  • You have to make specific calls in the graphical Apis of each operating system.

2 answers

3

You have to use a library that has the design features - and that intermediates with the operating system.

To "black the screen" and you can draw everything from scratch, one of the easiest to use in Python is the pygame. (To install pygame, in general you only need to type pip install pygame command line (cmd in windows)

To draw in the "root" window of the operating system, where the background and icons are located, it is possible - but not usual - using libraries like Qt, or the direct API of the operating system (Xlib on Linux) - and even if the system authorizes you, what you draw is subject to being suddenly erased in any redesign of the screen.

With pygame, to draw on the full screen, just call the window setup by passing the indicator pygame.FULLSCREEN as second parameter. It is important to remember to call pygame.quit() in a building of the type try ...finally , otherwise, if your program gives some error while it is full screen, you may get "stuck" on the graphical screen.

Minimum example to erase the screen, fill it with white color, draw a red rectangle, wait a few seconds and finish:


import time
import pygame


def main():
    try:
        screen = pygame.display.set_mode((1024, 768), pygame.FULLSCREEN)
        screen.fill((255,255,255))
        pygame.draw.rect(screen, (255,0,0), (50,50,300,200)) 
        pygame.display.flip()
        time.sleep(5)
    finally:
        pygame.quit()


main()

Note that I use the resolution "1024,768" - which is kind of "universal" - you can make the pygame print (even print) out of pygame.display.list_modes() to know the native resolutions available on your machine.

In addition, pygame’s own documentation on https://www.pygame.org/docs/ref/draw.html and on the "Events" page can help you create more elaborate drawings and interact with the keyboard and mouse.

2


Example of how to draw directly on the desktop using the Win32 API calls Movetoex() and Linet()

import win32api
import win32gui

#Pega o contexto gráfico para o Desktop
dc = win32gui.GetDC(0)

#Desenha uma linha do ponto (0,0) até (1366,768)
win32gui.MoveToEx(dc,0,0)
win32gui.LineTo(dc,1366,768)

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.