-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_())
試試我的答案,如果我幫你不要忘了請將其標記爲正確。 – eyllanesc