Python 如何检测对话框';什么是最后一件事?
Python 如何检测对话框';什么是最后一件事?,python,pyqt,pyside,Python,Pyqt,Pyside,大家好 我正在使用python3.4和Windows7中的PyQt5制作一个GUI应用程序 应用程序非常简单。用户单击主窗口的按钮,弹出信息对话框。当用户单击信息对话框的关闭按钮(窗口的X按钮)时,系统显示确认消息。就这些 这是我的密码 # coding: utf-8 import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QDialog, QLabel class mainClass(QM
大家好 我正在使用python3.4和Windows7中的PyQt5制作一个GUI应用程序 应用程序非常简单。用户单击主窗口的按钮,弹出信息对话框。当用户单击信息对话框的关闭按钮(窗口的X按钮)时,系统显示确认消息。就这些 这是我的密码
# coding: utf-8
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QDialog, QLabel
class mainClass(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
openDlgBtn = QPushButton("openDlg", self)
openDlgBtn.clicked.connect(self.openChildDialog)
openDlgBtn.move(50, 50)
self.setGeometry(100, 100, 200, 200)
self.show()
def openChildDialog(self):
childDlg = QDialog(self)
childDlgLabel = QLabel("Child dialog", childDlg)
childDlg.resize(100, 100)
childDlg.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
mc = mainClass()
sys.exit(app.exec_())
结果屏幕截图是
在这种情况下,我在mainClass中添加了这些代码
def closeEvent(self, event):
print("X is clicked")
此代码仅在主窗口关闭时有效。但我想要的是当childDlg关闭时closeEvent函数工作。不是主窗口
我该怎么办?您已经在类
mainClass
中添加了方法closeEvent
。
因此,您重新实现了QMainwindow
的closeEvent
方法,而不是ChildLg的closeEvent
方法。为此,您必须将chilDlg
子类化如下:
# coding: utf-8
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QDialog, QLabel
class ChildDlg(QDialog):
def closeEvent(self, event):
print("X is clicked")
class mainClass(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
openDlgBtn = QPushButton("openDlg", self)
openDlgBtn.clicked.connect(self.openChildDialog)
openDlgBtn.move(50, 50)
self.setGeometry(100, 100, 200, 200)
self.show()
def openChildDialog(self):
childDlg = ChildDlg(self)
childDlgLabel = QLabel("Child dialog", childDlg)
childDlg.resize(100, 100)
childDlg.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
mc = mainClass()
sys.exit(app.exec_())
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QDialog, QLabel
class mainClass(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
openDlgBtn = QPushButton("openDlg", self)
openDlgBtn.clicked.connect(self.openChildDialog)
openDlgBtn.move(50, 50)
self.setGeometry(100, 100, 200, 200)
self.show()
def openChildDialog(self):
childDlg = QDialog(self)
childDlgLabel = QLabel("Child dialog", childDlg)
childDlg.closeEvent = self.CloseEvent
childDlg.resize(100, 100)
childDlg.show()
def CloseEvent(self, event):
print("X is clicked")
if __name__ == "__main__":
app = QApplication(sys.argv)
mc = mainClass()
sys.exit(app.exec_())