1
私は現在、pyqt5でアプリケーションを構築する方法を学んでおり、closeEventメソッドで問題が発生したため、ユーザーはQMessageBoxオブジェクトによる確認を求められます。 Xボタンでうまくいっているようです - イベントはアクションが確認されたときに「受け入れられ」、キャンセルボタンがクリックされたときに「キャンセル」されます。ただし、ドロップダウンの[ファイル]メニューの[終了]ボタンを使用すると、クリックしたボタンに関係なく、プログラムは終了コード1で閉じられます。どちらの場合も同じcloseEventメソッドを使用するので、奇妙に見えます。PyQt5 closeEventメソッド
import sys
from PyQt5.QtWidgets import QApplication, QMessageBox, QMainWindow, QAction
class window(QMainWindow):
def __init__(self):
super().__init__()
def createUI(self):
self.setGeometry(500, 300, 700, 700)
self.setWindowTitle("window")
quit = QAction("Quit", self)
quit.triggered.connect(self.closeEvent)
menubar = self.menuBar()
fmenu = menubar.addMenu("File")
fmenu.addAction(quit)
def closeEvent(self, event):
close = QMessageBox()
close.setText("You sure?")
close.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)
close = close.exec()
if close == QMessageBox.Yes:
event.accept()
else:
event.ignore()
main = QApplication(sys.argv)
window = window()
window.createUI()
window.show()
sys.exit(main.exec_())
ありがとうございました!
コンソール/端末で実行しましたか?エラーメッセージが表示されます。 'AttributeError: 'bool'オブジェクトには属性 'accept'がありません。 – furas
ボタンをクリックして関数を呼び出すと、' accept'と 'ignore()'を持たない 'event'オブジェクトがあります。エラーメッセージとプログラムは終了コード1で終了します。 – furas