Pyqt5 - 回到隐藏的主窗口

Pyqt5 - Back to hidden MainWindows

我正在编写一个程序来使用 pyqt5 指定特定区域。 如果我点击捕获按钮,我将隐藏主窗口并显示一个屏幕来指定区域。我想通过按 esc 键清除屏幕和 return 到主窗口。但是 MainWindow 不再显示。我应该如何操作它?

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)

这是指定区域的class(部分代码省略)

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__()

首先,与其覆盖keyPressEvent方法,不如使用QShortcut更简单。另一方面,对于这种情况,最好创建一个信号,指示何时按下转义键,将其连接到 show 方法。

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_())