一口快乐水 2017-05-17 13:51 采纳率: 0%
浏览 4439

PyQt5如何获取鼠标处图片像素

如题,我搜到了PyQt4的写法,想改成5的,但是5的函数变了,不太会,不知道5里鼠标处的Wid怎么获取?
这个是4的实现方法
PS:5不支持QPixmap.grabWindow,改成了Qscreen.grabWindow,
然后第一个参数Wid用QApplication.desktop().winId()也无法获取了,暂时还没有找到获取方式

if event.buttons() & Qt.LeftButton:  
   pointX = event.globalX()
   pointY = event.globalY()
   # img is QImage type
   img = QPixmap.grabWindow(
           QApplication.desktop().winId()).toImage()
   rgb = img.pixel(pointX, pointY)
   #十进制
   red10 = QtGui.qRed(rgb)
   green10 =QtGui.qGreen(rgb)
   blue10 = QtGui.qBlue(rgb)
   color10="("+str(red10)+","+str(green10)+","+str(blue10)+")"
   #十六进制
   #print str(hex(red10))
   red16=str(hex(red10))[2:]
   green16=str(hex(green10))[2]
   blue16=str(hex(blue10))[2:]
   color16=red16+green16+blue16
   #print color16
   print "(%s,%s) = %s (%s,%s,%s)" % (pointX, pointY, color16,red10, green10, blue10)
   self.label.setText("(%s,%s) = %s (%s,%s,%s)" % (pointX, pointY, color16,red10, green10, blue10))
#该片段来自于http://outofmemory.cn


代码来源
http://outofmemory.cn/code-snippet/12666/PyQt4-get-screen-position-color

  • 写回答

1条回答

  • 一去丶二三里 C++ 领域优质创作者 2017-12-18 06:06
    关注

    您需要创建 QScreen 对象,并进行初始化(例如:QtGuiApplication.primaryScreen()),然后抓取屏幕:

    screen.grabWindow(QApplication.desktop().winId())
    
    评论

报告相同问题?

悬赏问题

  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区