当状态栏隐藏时,PyQt5主窗口关闭

2024-05-18 06:53:28 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我刚刚添加了一些代码,它应该: 添加一个菜单项,允许您选中或取消选中是否要查看状态栏。在

但是,当我点击这个菜单项时,它会关闭整个窗口,而不仅仅是隐藏状态栏。有人能帮忙吗?下面是我的代码(抱歉,我只是把它都放在那里了-没那么多)。在

提前谢谢。在

皮耶卡

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

Tags: theimportselfdefcreatethispyqt5btn
1条回答
网友
1楼 · 发布于 2024-05-18 06:53:28

要获得statusbar,必须通过statusBar()方法来实现,因此请将代码更改为:

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

相关问题 更多 >