使用PyQt4或PySide捕获屏幕,特定的小部件除外?

时间:2019-05-06 04:18:03

标签: python pyqt4 pyside

我试图捕获半透明的pyqt4应用程序区域中的屏幕区域,但我想在捕获中排除小部件“ betwn = QPushButton('test')”,我试图使用以下方法隐藏小部件hide()这样:betwn.hide(),用于在捕获之前被排除,但不起作用,即使被隐藏的“ betwn”也将被捕获。

这是我的PyQt4代码:

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from datetime import datetime

app = QApplication(sys.argv)
widget = QWidget()
widget.setLayout(QVBoxLayout())
widget.setAttribute(Qt.WA_TranslucentBackground)

label = QLabel()
widget.layout().addWidget(label)

def capture():
    p = QPixmap.grabWindow(QApplication.desktop().winId(),widget.geometry().x(),widget.geometry().y(),widget.geometry().width(),widget.geometry().height())
    label.setPixmap(p)
def shoot():
    betwn.hide()
    capture()
    betwn.show()
button = QPushButton('take screenshot', clicked=shoot)
betwn = QPushButton('test')
widget.layout().addWidget(button)
widget.layout().addWidget(betwn)

widget.show()
app.exec_()

这是我的PySide代码:

import sys
from PySide.QtGui import *
from PySide.QtCore import *
from datetime import datetime

app = QApplication(sys.argv)
widget = QWidget()
widget.setLayout(QVBoxLayout())
widget.setAttribute(Qt.WA_TranslucentBackground)

label = QLabel()
widget.layout().addWidget(label)

def capture():
    p = QPixmap.grabWindow(QApplication.desktop().winId(),widget.geometry().x(),widget.geometry().y(),widget.geometry().width(),widget.geometry().height())
    label.setPixmap(p)
def shoot():
    betwn.hide()
    capture()
    betwn.show()
button = QPushButton('take screenshot', clicked=shoot)
betwn = QPushButton('test')
widget.layout().addWidget(button)
widget.layout().addWidget(betwn)

widget.show()
app.exec_()

我如何在捕获中排除小部件“ betwn = QPushButton('test')”?还是如果您知道使用窗口的其他方式捕获桌面区域而不捕获窗口的其他方法,请使用窗口的坐标以及宽度和高度?

赞:

QPixmap.grabWindow(QApplication.desktop().winId(),widget.geometry().x(),widget.geometry().y(),widget.geometry().width(),widget.geometry().height());

1 个答案:

答案 0 :(得分:0)

当调用hide方法时,这并不意味着该按钮将在此时关闭,因为它涉及到重新绘制过程,该过程在同步任务结束后结束,因此在这种情况下,您必须给Qt足够的时间来执行这项工作并且可以完成。制作一个QTimer :: singleShot()。

app = QApplication(sys.argv)
widget = QWidget()
widget.setLayout(QVBoxLayout())
widget.setAttribute(Qt.WA_TranslucentBackground)

label = QLabel()
widget.layout().addWidget(label)


def capture():
    p = QPixmap.grabWindow(
        QApplication.desktop().winId(), 
        *widget.geometry().getRect()
    )
    label.setPixmap(p)
    betwn.show()


def shoot():
    betwn.hide()
    QTimer.singleShot(1, capture)


button = QPushButton("take screenshot", clicked=shoot)
betwn = QPushButton("test")
widget.layout().addWidget(button)
widget.layout().addWidget(betwn)

widget.show()
sys.exit(app.exec_())