0

File: untitled.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        #extra code
        self.MainWindow = MainWindow
        QtCore.QTimer.singleShot(2000, self.saveScreenshot)

    def saveScreenshot(self):
        screen = QtWidgets.QApplication.primaryScreen()
        screenshot = screen.grabWindow( self.MainWindow.winId() )
        screenshot.save('shot.jpg', 'jpg')


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "Simple MainWindow"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Output print screen image:

enter image description here

Expected output:

enter image description here

What's wrong? OS: Ubuntu 22.04

Edit: After switching to XORG from Wayland the problem solved.

Now there is another problem: I want to print screen a QMainWindow that has QMenu, QToolBar, QScrollArea. How can i make a print screen of the scrolling widget?

Chris P
  • 2,059
  • 4
  • 34
  • 68

1 Answers1

0

Input: a PyQt5 QMainWindow application that has: 1) QMenu, 2) QToolBar, 3) QScrollArea, 4)QStatus

I create 4 images one for QMenu, one for QToolBar, one for QScrollArea and one for QStatus. After that i merge the 4 images one after another.

Code:

        img = QtGui.QImage(self.menubar.size(),QtGui.QImage.Format.Format_ARGB32)
        painter = QtGui.QPainter(img)
        self.menubar.render(painter)
        istrue = img.save("file1.jpg")
        painter.end()

        img = QtGui.QImage(self.toolBar.size(),QtGui.QImage.Format.Format_ARGB32)
        painter = QtGui.QPainter(img)
        self.toolBar.render(painter)
        istrue = img.save("file2.jpg")
        painter.end()


        img = QtGui.QImage(self.scrollAreaWidgetContents.size(),QtGui.QImage.Format.Format_ARGB32)
        painter = QtGui.QPainter(img)
        self.scrollAreaWidgetContents.render(painter)
        istrue = img.save("file3.jpg")
        painter.end()


        img = QtGui.QImage(self.statusbar.size(),QtGui.QImage.Format.Format_ARGB32)
        painter = QtGui.QPainter(img)
        self.statusbar.render(painter)
        istrue = img.save("file4.jpg")
        painter.end()

        from PIL import Image

        images = [Image.open(x) for x in ['file1.jpg', 'file2.jpg', 'file3.jpg', 'file4.jpg']]
        widths, heights = zip(*(i.size for i in images))

        total_width = max(widths)
        max_height = sum(heights)

        new_im = Image.new('RGB', (total_width, max_height))

        y_offset = 0
        for im in images:
          new_im.paste(im, (0,y_offset))
          y_offset += im.size[1]

        new_im.save('final.jpg')        

Output:

enter image description here

There is a little bug with the above image: in QScrollArea the area that scrollbar supposed to be is empty (black pixels).

Chris P
  • 2,059
  • 4
  • 34
  • 68