2012-06-11 21 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

相關問題