PyQt5 Main Window 在隐藏状态栏时关闭

PyQt5 Main Window closes when statusbar is hidden

我正在通过教程学习如何使用 PyQt5。

我刚刚添加了一些应该用于: 添加一个菜单条目,允许您选中或取消选中是否要查看状态栏。

但是,当我单击此菜单项时,它会关闭整个 window,而不仅仅是隐藏状态栏。有人可以帮忙吗?下面是我的代码(抱歉,我只是把它全部放在那里 - 没那么多)。

提前致谢。

皮耶卡

import sys
from PyQt5.QtWidgets import (QWidget, QToolTip, QPushButton, QApplication, QMessageBox, QDesktopWidget, QMainWindow, QApplication, QAction, qApp, QMenu)
from PyQt5.QtGui import (QIcon, QFont)
from PyQt5.QtCore import QCoreApplication


class Example(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(200, 200, 800, 500)
        self.setWindowTitle('Warhammer Simulater 0.01')
        self.setWindowIcon(QIcon(r"C:\Users\Ordinateur\Pictures\#Download\BattleAxeIcon.png"))

        QToolTip.setFont(QFont('SansSerif', 10))

        self.setToolTip('This is a <i>where</i> we will eventually show the <u>battlefield</u>')

        # Create a Button
        self.btn = QPushButton('Roll', self)
        self.btn.setToolTip('This <b>rolls</b> the dice!')
        self.btn.resize(self.btn.sizeHint())
        self.btn.move(50, 50)

        # Create a Quit Button
        self.qbtn = QPushButton('Quit', self)
        self.qbtn.clicked.connect(self.close)
        self.qbtn.resize(self.qbtn.sizeHint())
        self.btn.setToolTip('This quits the application!')
        self.qbtn.move(int(self.width()-(60+self.qbtn.width()/2)), int(self.height()-(50-self.qbtn.height()/2)))

        # Create a Status Bar
        self.statusBar().showMessage('Ready')

        # Create a Menu Bar
        newAct = QAction('New', self)
        impAct = QAction('Import Mail', self)

        exitAct = QAction(QIcon(r"C:\Users\Ordinateur\Pictures\#Download\SimpleSkull.png"), '&Exit', self)
        exitAct.setShortcut('Ctrl+Q')
        exitAct.setStatusTip('Exit application')
        exitAct.triggered.connect(self.close)

        viewStatAct = QAction('View statusbar', self, checkable=True)
        viewStatAct.setStatusTip('View statusbar')
        viewStatAct.setChecked(True)
        viewStatAct.triggered.connect(self.toggleMenu)

        self.menubar = self.menuBar()
        self.fileMenu = self.menubar.addMenu('&File')

        self.fileMenu.addAction(newAct)

        impMenu = QMenu('Import', self)
        impMenu.addAction(impAct)

        self.fileMenu.addAction(viewStatAct)
        self.fileMenu.addMenu(impMenu)
        self.fileMenu.addAction(exitAct)

        self.center()
        self.show()

    def closeEvent(self, event):
        # This is called when user attempts tp Quit
        reply = QMessageBox.question(self, 'Already?',
                                 "Are you sure you want to quit?", QMessageBox.Yes |
                                 QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

    def center(self):
        # Centers the Main Window on the Screen

        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def toggleMenu(self, state):
        # Turns the Status Menu On and Off
        if state:
            self.statusbar.show()
        else:
            self.statusbar.hide()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    print("Done")
    sys.exit(app.exec_())

要获得 statusbar 你必须通过 statusBar() 方法来完成,所以将你的代码更改为:

def toggleMenu(self, state):
    # Turns the Status Menu On and Off
    if state:
        self.statusBar().show()
    else:
        self.statusBar().hide()