[PyKDE] pixmap trouble

Jürgen Urner loboto at web.de
Mon Mar 20 21:35:19 GMT 2006

Well, new to linux, new to KDE and new to pyQt. Had quite an easy port 
from micros~1 ,
not much of a hassle indeed. So, here I am and the first thing to make 
me scratch my head is

The GUI below is supposed to grab a portion from the screen, display it 
in the client area
  adding a string "color" in the color of the pixel at the cursor 
position. Quite obvious this
code is inspired by kMag and is suposed to do about the same thing for 
me with the help of
pyQT 3 on KDE 3.4.3.

The GUI works like this:

- click on it to start capturing
- click on any pos of the screen to grab the screen pixels color
- hit escape to release the capture

And here are the head scratchers:

1. The grab_pixel() method returns an arbitrary color instead of the 
color at the cursor
2. resizing the view causes the pixmap to contain arbitrary contents 
when a certain size is exceeded
3. if I set the GUI to instead of grabbing a pixel at a specified pos to 
grabbing a portion of the screen,
    to my surprise a capture of the last position is returned and not 
one of the current

Any help is greatly appreciated

import sys
import qt

class Magnifier(qt.QFrame):
    """magnifier widget"""

    def __init__(self, parent, application):
        qt.QFrame.__init__(self, parent)

        self.application = application
        self.is_dragging = False
        self.px = None
        self.x = None
        self.y = None
        self.color = None

    def grab_display(self):
        if self.x == None: return

        w, h = self.width(), self.height()
        x = self.x - (w / 2)
        y = self.y - (h / 2)

        desktop = self.application.desktop()
        self.px = qt.QPixmap(w, h)
        self.px.grabWindow(desktop.winId(), x, y, w, h)

    def grab_pixel(self, x, y):
        desktop = self.application.desktop()
        px = qt.QPixmap(1, 1)
        px.grabWindow(desktop.winId(), x, y, 1, 1)
        img = px.convertToImage()
        self.color = qt.QColor(img.pixel(0, 0))

    def paintEvent(self, event):
        if self.px and not self.px.isNull():   
            painter = qt.QPainter(self)
            painter.drawPixmap(0, 0, self.px, 0, 0, self.px.width(), 
            if self.color:
                    painter.drawText(10, 20, 'color')
                except Exception, d:
                    print Exception, d

    def mousePressEvent(self, event):
        if event.button()== event.LeftButton:
            if self.is_dragging:
                    pos = self.mapToGlobal(event.pos())
                    self.grab_pixel(pos.x(), pos.y())              ##
                except Exception, d:
                    print Exception, d
                self.is_dragging = True

    def mouseMoveEvent(self, event):
        if self.is_dragging:
            pos = self.mapToGlobal(event.pos())
            self.x, self.y = pos.x(), pos.y()
            self.grab_display()                                           ##

    def resizeEvent(self, event):

    def keyReleaseEvent(self, event):
        if event.key() == event.Key_Escape:
            self.is_dragging = False

class SampleApp(qt.QMainWindow):

    def __init__(self, parent=None, name='ColorPick', application=None):
        qt.QMainWindow.__init__(self, parent, name)
        self.mag = Magnifier(self, application)
        self.g = qt.QGridLayout(self, 1, 1)
        self.g.addWidget(self.mag, 0, 0)

def main():
    app = qt.QApplication(sys.argv)
    window = SampleApp(application=app)

if __name__ == "__main__": main()

More information about the PyQt mailing list