PyQt全屏显示图像

4 投票
2 回答
4644 浏览
提问于 2025-04-15 15:59

我正在使用PyQt来捕捉我的屏幕,方法是用QPixmap.grabWindow(QApplication.desktop().winId())。我想知道有没有办法可以全屏显示我的屏幕截图(没有窗口边框之类的)。我正在寻找一种方法来用PyQt让我的显示变得不那么鲜艳。

2 个回答

0

这是一个关于PyQt6的工作示例,展示了directedition所描述的解决方案。

  • Qt.WindowType.FramelessWindowHint可以创建一个没有边框的窗口,文档注意,这个选项可以在创建窗口时作为flags参数传入,也可以在之后用setWindowFlags来设置。
  • showFullScreen()可以让窗口全屏显示,文档
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QImage
from PyQt6.QtWidgets import QApplication, QMainWindow


class Img(QMainWindow):
    def __init__(self, img_path, parent=None):
        super().__init__(parent)
        self.qimg = QImage(img_path)

        self.setWindowFlags(Qt.WindowType.FramelessWindowHint) 

    def paintEvent(self, qpaint_event):
        # event handler of QWidget triggered when, for ex, painting on the widget’s background.
        painter = QPainter(self)
        rect = qpaint_event.rect() # Returns the rectangle that needs to be updated
        painter.drawImage(rect, self.qimg)
        self.showFullScreen()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    
    img_path = # add path of the image

    window = Img(img_path)
    window.show()
    
    sys.exit(app.exec())
9

在图像小部件的代码中,将 QtCore.Qt.FramelessWindowHint 传递给 QWidget 的构造函数,并配合使用 self.showFullScreen(),就可以实现这个效果。

撰写回答