How to hide elements on a window in PyQt for a screenshot and then show them again?

Viewed 15

I have a window in PyQt5, where I have a lot of elements and two specific buttons among them. One of the buttons, called "Save", when clicked, takes a screenshot of the window with this code:

w = self.centralwidget
screen = app.primaryScreen()
screenshot = screen.grabWindow(w.winId())
screenshot.save('screenshot.jpg', 'jpg')

The other button, called "Browse", opens a Windows explorer with the folder of the taken screenshot.

The buttons themselves work as intended.

What I want to achieve is, when the "Save" button is clicked, the "Save" and "Browse" buttons to hide, so that they are not visible on the screenshot of the window, then the screenshot to be taken, and then show these buttons on a window again.

I tried the following method (called, when "Save" button is clicked):

def screenshot(self, MainWindow):
        self.screenshotButton.hide() #hiding the "Browse" button
        self.saveButton.hide()  #hiding the "Save" button
        w = self.centralwidget
        screen = app.primaryScreen()
        screenshot = screen.grabWindow(w.winId()) #taking the screenshot
        screenshot.save('screenshot.jpg', 'jpg') #saving the screenshot
        self.screenshotButton.show() #showing "Browse" button again
        self.saveButton.show() #showing "Save" button again

But this does not hide the buttons on the screenshot, they are still visible. I also tried putting time.sleep(5) after using the .(hide) and before taking the screenshot, but it doesn't help.

What can I do about this?

0 Answers
Related