0

I'm making a program to specify a specific area using pyqt5. If I click on the capture button, I will hide the MainWindow and display a screen to specify the area. I want to clear the screen and return to the mainwindow by pressing the esc key. But MainWindow does not show again. How should I work it?

form_class = uic.loadUiType("prototype.ui")[0]
class MainWindow(QtWidgets.QMainWindow, form_class) :
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.Capture_Button.clicked.connect(self.Capture_Btn_clicked)
        self.CaptureWindow = CaptureWidget()
        self.CaptureWindow.hide()

    def Capture_Btn_clicked(self) :
        self.hide()
        self.CaptureWindow.close()
        self.CaptureWindow.__init__()
        self.CaptureWindow.show()
        QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CrossCursor)) 

    def enterEvent(self, QEvent):
         QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor)) 
         self.setWindowOpacity(1)

This is the class that specifies the area (some of the code is omitted).

class CaptureWidget(QtWidgets.QDialog):

    def __init__(self):
        super().__init__()
        root = tk.Tk()
        self.setupUi(self)

    def setupUi(self) :
        self.screen_width = root.winfo_screenwidth()
        self.screen_height = root.winfo_screenheight()
        self.setGeometry(0, 0, self.screen_width, self.screen_height)
        self.setWindowTitle(' ')
        self.begin = QtCore.QPoint()  
        self.end = QtCore.QPoint()
        self.setWindowOpacity(0.3) 
        QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CrossCursor)) 
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint|QtCore.Qt.WindowStaysOnTopHint) 
        print('Capture the screen...')

        self.is_set_region = False 
        self.is_mouse_click = False
        self.show()    

    def keyPressEvent(self, event):
        key = event.key()
        if key == Qt.Key_Escape:

            print('esc')
            self.close()

        elif key == Qt.Key_F1:
            self.close()
            self.__init__()
InApple
  • 1
  • 4

1 Answers1

0

First of all, instead of overwriting the keyPressEvent method, it is easier to use QShortcut. On the other hand for this case it is better to create a signal that indicates when the escape key is pressed connecting it to the show method.

from PyQt5 import QtCore, QtGui, QtWidgets, uic

class CaptureWidget(QtWidgets.QDialog):
    escape_pressed = QtCore.pyqtSignal()

    def __init__(self):
        super().__init__()
        self.setupUi()

    def setupUi(self):
        self.setWindowOpacity(0.3) 
        QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CrossCursor)) 
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
        shorcut_scaped = QtWidgets.QShortcut(QtCore.Qt.Key_Escape, self)
        shorcut_scaped.activated.connect(self.escape_pressed)
        shorcut_scaped.activated.connect(self.close)

        shorcut = QtWidgets.QShortcut(QtCore.Qt.Key_F1, self)
        shorcut.activated.connect(self.close)

form_class, _ = uic.loadUiType("prototype.ui")

class MainWindow(QtWidgets.QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.Capture_Button.clicked.connect(self.Capture_Btn_clicked)
        self.CaptureWindow = CaptureWidget()
        self.CaptureWindow.escape_pressed.connect(self.show)

    @QtCore.pyqtSlot()
    def Capture_Btn_clicked(self):
        self.hide()
        self.CaptureWindow.showFullScreen()
        QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CrossCursor)) 

    def enterEvent(self, QEvent):
         QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor)) 
         self.setWindowOpacity(1)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241