2012-06-11 24 views
3

我在Linux上,想要尝试重新创建用于我的Web开发的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()函数中崩溃。有什么我可以使用的吗?

+0

您正在显示,还是您想查看鼠标周围的内容? – octopusgrabbus

+0

要查看鼠标周围的内容,请在[例如] 150x150区域中查看。 –

回答

0

我知道了。而不是使用while循环,我切换到QTimer。这释放了大量的内存使用,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_()) 
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. 
+0

我如何在窗口上显示'im'?通过QTWidget/Label? –

+0

@Talasan Nicholson - 我还没有用过这么多,但我认为你创建了一个wx框架,你可以通过它显示它。该方法还允许您获取屏幕的一部分。如果你谷歌的'图像蟒蛇''很多东西出现,希望有一些帮助。如果你想出来,请在这里发布“如何”...... – fraxel

相关问题