如何检测(查看)鼠标指针周围的屏幕区域

时间:2012-06-11 22:30:30

标签: python linux

我在Linux上,并且想尝试为我的网站开发重新创建Nattyware的Pixie工具。 gPick还可以,但是Pixie就更好了。

我希望能够检测并显示鼠标指针周围的区域。我一直试图找到一种方法来显示鼠标指针周围的区域,用Python放大。

我不知道从哪里开始这样的事情。我不想保存任何图像,只显示鼠标在窗口中的放大区域。

修改: 我得到的东西可能有用。 不要跑这个,它会崩溃!

import sys, evdev
from Xlib import display, X
from PyQt4 import QtGui
from PyQt4.QtGui import QPixmap, QApplication, QColor

class printImage():
def __init__(self):
    self.window = QtGui.QMainWindow()
    self.window.setGeometry(0,0,400,200)

    self.winId = QApplication.desktop().winId()
    self.width = 150
    self.height = 150

    self.label = QtGui.QLabel('Hi')
    self.label.setGeometry(10, 10, 400, 100)
    self.label.show()

def drawView(self, x, y):
    self.label.setText('abc')
    pix = self.getScreenArea(x, y)
    self.pic.setPixmap(pix)

def render(self):
    self.window.show()

def getScreenArea(self, areaX, areaY):
    image = QPixmap.grabWindow(
        self.winId,
        x = areaX,
        y = areaY,
        width = self.width,
        height = self.height
    )

    return image

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = printImage()

    view.render()

    display = display.Display(':0')
    root = display.screen().root

    root.grab_pointer(
       True, 
       X.PointerMotionMask | X.ButtonReleaseMask, 
       X.GrabModeAsync, 
       X.GrabModeAsync, 
       0, 0, 
       X.CurrentTime
   )

    while True:
        ev = display.next_event()
        view.drawView(ev.event_x, ev.event_y)

    app.exec_()

知道它为什么会毁灭自己吗?它在grabWindow()函数崩溃了。还有其他我可以使用的东西吗?

2 个答案:

答案 0 :(得分:2)

这适用于linux,可能是跨平台的:

import wx
ff=wx.App()
screen = wx.ScreenDC()
size = screen.GetSize()
bmp = wx.EmptyBitmap(size[0], size[1])
mem = wx.MemoryDC(bmp)
mem.Blit(0, 0, size[0], size[1], screen, 0, 0)
del mem
#bmp.SaveFile('screenshot.png', wx.BITMAP_TYPE_PNG)
im = bmp.ConvertToImage()

来自help

ConvertToImage

Creates a platform-independent image from a platform-dependent
bitmap. This preserves mask information so that bitmaps and images can
be converted back and forth without loss in that respect.

答案 1 :(得分:0)

我明白了。我转而使用QTimer,而不是使用while循环。这释放了大量的内存使用量,而grabWindow()表现得非常好。

import sys, pymouse
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class printImage(QWidget):
    def __init__(self, *args):
        apply(QWidget.__init__,(self, ) + args)
        QWidget.__init__(self)

        self.winId = QApplication.desktop().winId()
        self.mouse = pymouse.PyMouse()
        self.timer = QTimer()

        self.x = self.y = self.cX = self.cY = 0

        self.createLabel()
        self.show()
        self.startListening()

    def createLabel(self):
        self.label = QLabel(self)
        self.label.move(10, 15)
        self.label.resize(150, 130)

    def startListening(self):
        self.timer.connect(self.timer, SIGNAL('timeout()'), self.sendData)
        self.timer.start(0)

    def sendData(self):
        pos = self.mouse.position()
        x = pos[0]
        y = pos[1]

        if (self.cX != x) and (self.cY != y):
            self.x = self.cX = x
            self.y = self.cY = y

            self.label.setPixmap(
                self.cropScreenArea()
            )

    def cropScreenArea(self):
        return QPixmap.grabWindow(
            self.winId,
            x = self.x - 80,
            y = self.y - 60,
            width = 150,
            height = 130
        )

    def keyPressEvent(self, e):
        if e.key() == Qt.Key_Escape:
            self.close()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = printImage()

    sys.exit(app.exec_())