Mientras un hilo se encarga de mostrar la interfaz gráfica, el otro obtiene la posición del cursor cada 0.1 segundos y captura un rectángulo de la pantalla alrededor del mismo. Por defecto este último tiene un tamaño de 125×100. Este simple ampliador de 75 líneas corre en Windows, Linux y Mac OS X, gracias a la portabilidad del framework PyQt y el toolkit AutoPy.
Descargas
- Código de fuente (requiere Python 2.7, AutoPy, PyQt 4)
- Archivo ejecutable (Windows)
Fuente
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# ampliador.py
#
# Copyright 2013 Recursos Python - www.recursospython.com
#
from threading import Thread
from time import sleep
from PyQt4.QtCore import QObject, pyqtSignal, QSize, Qt
from PyQt4.QtGui import QApplication, QMainWindow, QLabel, QPixmap
import autopy
WIDTH, HEIGHT = 125, 100 # Ancho y Alto
class GetPosSignal(QObject):
trigger = pyqtSignal()
def __init__(self, capture_screen):
QObject.__init__(self)
self.capture_screen = capture_screen
def connect(self):
self.trigger.connect(self.get_pos)
def get_pos(self):
# Posición del cursor
x, y = autopy.mouse.get_pos()
# Crear un rectángulo alrededor de la posición obtenida
self.capture_screen(x - (WIDTH / 2.), y - (HEIGHT / 2.))
class Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.resize(WIDTH, HEIGHT)
self.setWindowTitle("Ampliador PyQt y AutoPy")
self.image_label = QLabel(self)
self.setCentralWidget(self.image_label)
self.setWindowFlags(Qt.WindowStaysOnTopHint) # Mantener la ventana al frente
self.signal = GetPosSignal(self.capture_screen)
self.signal.connect()
self.run = True
Thread(target=self.worker).start()
def closeEvent(self, event):
# Terminar el thread al cerrar la ventana
self.run = False
def worker(self):
while self.run:
self.signal.trigger.emit()
sleep(0.1)
def capture_screen(self, x, y):
# Capturar pantalla
pixmap = QPixmap.grabWindow(
QApplication.desktop().winId(), x, y, WIDTH, HEIGHT
)
# Aumentar 100%
self.image_label.setPixmap(
pixmap.scaled(QSize(WIDTH * 2, HEIGHT * 2))
)
if __name__ == "__main__":
app = QApplication([])
window = Window()
window.show()
app.exec_()
Si se desea cambiar el tamaño de la región ampliada, las constantes WIDTH y HEIGHT pueden ser modificadas. Por ejemplo:
WIDTH, HEIGHT = 200, 175
Versión
Python 2.7
Curso online 👨💻
¡Ya lanzamos el curso oficial de Recursos Python en Udemy!
Un curso moderno para aprender Python desde cero con programación orientada a objetos, SQL y tkinter en 2024.
Consultoría 💡
Ofrecemos servicios profesionales de desarrollo y capacitación en Python a personas y empresas. Consultanos por tu proyecto.
