2017-08-28 111 views
-1

我正在学习如何通过教程使用PyQt5。当隐藏状态栏时PyQt5主窗口关闭

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

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

在此先感谢。

Pieka

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

试试我的答案,如果我帮你不要忘了请将其标记为正确。 – eyllanesc

回答

0

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

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