PYQT4:关键消息框
PYQT4: Critical Message Box
好的,不确定我在这里遗漏了什么。我为工作创建了一个 GUI 应用程序,其中包含一个关闭按钮。如果用户使用自定义 window 关闭按钮,我能够执行 closeEvent 函数。但是,当他们单击 GUI 上的关闭按钮时,我无法产生相同的效果。下面是我的代码、错误和展示我的问题的 GUI 图片。在此先感谢您的帮助。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'icMainWin.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import icMainWin_rc
import sys
import os
import time
import os.path as osp
import ctypes
from ctypes import wintypes
import os
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
#setting the path variable for icon
path = osp.join(osp.dirname(sys.modules[__name__].__file__), 'I:\Infection Control System Database\images\Malware-icon.png')
myappid = 'I:\Infection Control System Database\images\Malware-icon.png' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
lpBuffer = wintypes.LPWSTR()
AppUserModelID = ctypes.windll.shell32.GetCurrentProcessExplicitAppUserModelID
AppUserModelID(ctypes.cast(ctypes.byref(lpBuffer), wintypes.LPWSTR))
appid = lpBuffer.value
ctypes.windll.kernel32.LocalFree(lpBuffer)
if appid is not None:
print(appid)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(905, 675)
MainWindow.setMinimumSize(QtCore.QSize(905, 675))
MainWindow.setMaximumSize(QtCore.QSize(905, 680))
font = QtGui.QFont()
font.setBold(False)
font.setWeight(50)
MainWindow.setFont(font)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("I:\Infection Control System Database\Program Files\images\ICDatabaseImage.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
发布代码被截断
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.actionExit, QtCore.SIGNAL(_fromUtf8("triggered()")), self.btnExit.click)
QtCore.QObject.connect(self.actionWar_Room, QtCore.SIGNAL(_fromUtf8("triggered()")), self.cmdWarRoom.click)
QtCore.QObject.connect(self.actionFlu, QtCore.SIGNAL(_fromUtf8("triggered()")), self.btnFlu.click)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.btnExit, self.btnHelp)
MainWindow.setTabOrder(self.btnHelp, self.btnRequest)
MainWindow.setTabOrder(self.btnRequest, self.btnSOR_IFMC)
MainWindow.setTabOrder(self.btnSOR_IFMC, self.btnSOR_IAH)
MainWindow.setTabOrder(self.btnSOR_IAH, self.btnSOR_ILH)
MainWindow.setTabOrder(self.btnSOR_ILH, self.btnSOR_IMVH)
MainWindow.setTabOrder(self.btnSOR_IMVH, self.btnSOR_IFOH)
MainWindow.setTabOrder(self.btnSOR_IFOH, self.cmdEmail)
MainWindow.setTabOrder(self.cmdEmail, self.cmdHCSBrpt)
MainWindow.setTabOrder(self.cmdHCSBrpt, self.cmdWarRoom)
MainWindow.setTabOrder(self.cmdWarRoom, self.cmdReview)
MainWindow.setTabOrder(self.cmdReview, self.cmdIC_AccessDB)
MainWindow.setTabOrder(self.cmdIC_AccessDB, self.btnCaseCAUTI)
MainWindow.setTabOrder(self.btnCaseCAUTI, self.btnCaseCLABSI)
MainWindow.setTabOrder(self.btnCaseCLABSI, self.btnCaseSSI)
MainWindow.setTabOrder(self.btnCaseSSI, self.btnCaseVAP)
MainWindow.setTabOrder(self.btnCaseVAP, self.btnCaseLabID)
MainWindow.setTabOrder(self.btnCaseLabID, self.btnCaseMDRO)
MainWindow.setTabOrder(self.btnCaseMDRO, self.btnHH)
MainWindow.setTabOrder(self.btnHH, self.btnNHSN)
MainWindow.setTabOrder(self.btnNHSN, self.btnHAI)
MainWindow.setTabOrder(self.btnHAI, self.btnPremier)
MainWindow.setTabOrder(self.btnPremier, self.btnFlu)
MainWindow.setTabOrder(self.btnFlu, self.btnMicrosoft)
MainWindow.setTabOrder(self.btnMicrosoft, self.dateEdit)
MainWindow.setTabOrder(self.dateEdit, self.timeEdit)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Infection Control Database - Landing Page", None))
self.lblWelcome.setText(_translate("MainWindow", "Welcome!", None))
self.dateEdit.setDisplayFormat(_translate("MainWindow", "MMMM dd yyyy", None))
self.btnExit.setStatusTip(_translate("MainWindow", "Exit Database", None))
self.btnExit.setText(_translate("MainWindow", "Exit", None))
self.btnExit.setShortcut(_translate("MainWindow", "Ctrl+Shift+Q", None))
self.btnHelp.setText(_translate("MainWindow", "Help", None))
self.btnRequest.setText(_translate("MainWindow", "Request ", None))
self.grpReportsSOR.setStatusTip(_translate("MainWindow", "SOR Reports", None))
self.grpReportsSOR.setTitle(_translate("MainWindow", "SOR Files", None))
self.btnSOR_IFMC.setText(_translate("MainWindow", "IFMC", None))
self.btnSOR_IAH.setText(_translate("MainWindow", "IAH", None))
self.btnSOR_ILH.setText(_translate("MainWindow", "ILH", None))
self.btnSOR_IMVH.setText(_translate("MainWindow", "IMVH", None))
self.btnSOR_IFOH.setText(_translate("MainWindow", "IFOH", None))
self.grp_EnterCaseData.setTitle(_translate("MainWindow", "Case Data Entry", None))
self.btnCaseCAUTI.setStatusTip(_translate("MainWindow", "Enter CAUTI Case", None))
self.btnCaseCAUTI.setText(_translate("MainWindow", "CAUTI Case", None))
self.btnCaseCLABSI.setStatusTip(_translate("MainWindow", "Enter CLABSI Case ", None))
self.btnCaseCLABSI.setText(_translate("MainWindow", "CLABSI Case", None))
self.btnCaseSSI.setStatusTip(_translate("MainWindow", "Enter SSI Case", None))
self.btnCaseSSI.setText(_translate("MainWindow", "SSI Case", None))
self.btnCaseVAP.setStatusTip(_translate("MainWindow", "Enter VAP Case", None))
self.btnCaseVAP.setText(_translate("MainWindow", "VAP Case", None))
self.btnCaseLabID.setStatusTip(_translate("MainWindow", "Enter LabID Case", None))
self.btnCaseLabID.setText(_translate("MainWindow", "LabID Case", None))
self.btnCaseMDRO.setStatusTip(_translate("MainWindow", "Enter MDRO Cases", None))
self.btnCaseMDRO.setText(_translate("MainWindow", "MDRO", None))
self.cmdEmail.setText(_translate("MainWindow", "Email", None))
self.cmdHCSBrpt.setText(_translate("MainWindow", "HCSB Report", None))
self.cmdWarRoom.setText(_translate("MainWindow", "War Room Report", None))
self.cmdReview.setText(_translate("MainWindow", "Review HAI Data", None))
self.cmdIC_AccessDB.setText(_translate("MainWindow", "2011 IC Access Database", None))
self.label.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-weight:600; color:#aa0000;\">Infection Control Database</span></p></body></html>", None))
self.btnHH.setStatusTip(_translate("MainWindow", "Hand Hygiene Application", None))
self.btnNHSN.setStatusTip(_translate("MainWindow", "NHSN Website", None))
self.btnHAI.setStatusTip(_translate("MainWindow", "HAI Folder", None))
self.btnPremier.setStatusTip(_translate("MainWindow", "Navigate to SafetySurveillor: https://ss.premierinc.com/Setnet/user/Login.po ", None))
self.btnFlu.setStatusTip(_translate("MainWindow", "Flu Reports", None))
self.groupBox.setStatusTip(_translate("MainWindow", "Navigate to Location", None))
self.groupBox.setTitle(_translate("MainWindow", "Navigation ", None))
self.menuFile.setTitle(_translate("MainWindow", "File", None))
self.menuEnd_Processes.setTitle(_translate("MainWindow", "End Processes", None))
self.menuView.setTitle(_translate("MainWindow", "View", None))
self.menuReports.setTitle(_translate("MainWindow", "Reports", None))
self.actionExit.setText(_translate("MainWindow", "Exit", None))
self.actionExit.setShortcut(_translate("MainWindow", "Ctrl+Shift+Q", None))
self.actionTerminate_Excel.setText(_translate("MainWindow", "Terminate Excel", None))
self.actionTerminate_Access.setText(_translate("MainWindow", "Terminate Access", None))
self.actionTerminate_Powerpoint.setText(_translate("MainWindow", "Terminate PowerP", None))
self.actionTerminate_Word.setText(_translate("MainWindow", "Terminate Word", None))
self.actionReset_Citrix.setText(_translate("MainWindow", "Reset Citrix", None))
self.actionReset_Citrix.setStatusTip(_translate("MainWindow", "Reset Citrix Server", None))
self.actionTerminate_Outlook.setText(_translate("MainWindow", "Terminate Outlook", None))
self.actionHCSB.setText(_translate("MainWindow", "HCSB", None))
self.actionWar_Room.setText(_translate("MainWindow", "War Room", None))
self.actionFlu.setText(_translate("MainWindow", "Flu", None))
信号事件(电子邮件)
self.cmdEmail.clicked.connect(self.EmailClicked)
信号事件(退出)
self.btnExit.clicked.connect(self.ExitClicked)
操作事件(电子邮件)
def EmailClicked(self):os.startfile('C:\Program Files\Microsoft Office\Office14\OUTLOOK.EXE')
动作事件(退出)
def ExitClicked(self):
ret = QMessageBox.critical(self, "Critical",
'Are you sure you want to exit?', QMessageBox.Ok, QMessageBox.Cancel)
ret = msgBox.exec_()
if ret == msgBox.Yes:
QtCore.QCoreApplication.instance().quit()
class ControlMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(ControlMainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.dateEdit.setDisplayFormat("MMM dd yyyy")
self.ui.timeEdit.setDisplayFormat("hh:mm:ss AP")
self.updateTime()
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.updateTime)
self.timer.start(1000)
def updateTime(self):
current = QtCore.QDateTime.currentDateTime()
self.ui.dateEdit.setDate(current.date())
self.ui.timeEdit.setTime(current.time())
def closeEvent(self, event):
result = QtGui.QMessageBox.critical(self,
"Confirm Exit...",
"Are you sure you want to exit?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
event.ignore()
if result == QtGui.QMessageBox.Yes:
event.accept()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
#app.setStyle( "Classic" )
mySW = ControlMainWindow()
mySW.show()
sys.exit(app.exec_())
您的代码生成错误,因为 QMessageBox.critical
需要传递 QObject
对象而 Ui_MainWindow
不需要。
class Ui_MainWindow(object):
....
def ExitClicked(self):
ret = QMessageBox.critical(self, "Critical",
'Are you sure you want to exit?', QMessageBox.Ok, QMessageBox.Cancel)
ret = msgBox.exec_()
if ret == msgBox.Yes:
QtCore.QCoreApplication.instance().quit()
此外,您希望按下按钮默认关闭和您的新关闭按钮具有相同的行为,您必须将发出新按钮的信号与关闭功能连接起来,因为关闭功能调用 closeEvent
.
class Ui_MainWindow(object):
....
class ControlMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(ControlMainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.btnExit.clicked.connect(self.close)
....
def closeEvent(self, event):
result = QtGui.QMessageBox.critical(self,
"Confirm Exit...",
"Are you sure you want to exit?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
event.ignore()
if result == QtGui.QMessageBox.Yes:
event.accept()
删除连接:self.btnExit.clicked.connect(self.ExitClicked)
和函数 def ExitClicked(self): ...
好的,不确定我在这里遗漏了什么。我为工作创建了一个 GUI 应用程序,其中包含一个关闭按钮。如果用户使用自定义 window 关闭按钮,我能够执行 closeEvent 函数。但是,当他们单击 GUI 上的关闭按钮时,我无法产生相同的效果。下面是我的代码、错误和展示我的问题的 GUI 图片。在此先感谢您的帮助。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'icMainWin.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import icMainWin_rc
import sys
import os
import time
import os.path as osp
import ctypes
from ctypes import wintypes
import os
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
#setting the path variable for icon
path = osp.join(osp.dirname(sys.modules[__name__].__file__), 'I:\Infection Control System Database\images\Malware-icon.png')
myappid = 'I:\Infection Control System Database\images\Malware-icon.png' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
lpBuffer = wintypes.LPWSTR()
AppUserModelID = ctypes.windll.shell32.GetCurrentProcessExplicitAppUserModelID
AppUserModelID(ctypes.cast(ctypes.byref(lpBuffer), wintypes.LPWSTR))
appid = lpBuffer.value
ctypes.windll.kernel32.LocalFree(lpBuffer)
if appid is not None:
print(appid)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(905, 675)
MainWindow.setMinimumSize(QtCore.QSize(905, 675))
MainWindow.setMaximumSize(QtCore.QSize(905, 680))
font = QtGui.QFont()
font.setBold(False)
font.setWeight(50)
MainWindow.setFont(font)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("I:\Infection Control System Database\Program Files\images\ICDatabaseImage.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
发布代码被截断
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.actionExit, QtCore.SIGNAL(_fromUtf8("triggered()")), self.btnExit.click)
QtCore.QObject.connect(self.actionWar_Room, QtCore.SIGNAL(_fromUtf8("triggered()")), self.cmdWarRoom.click)
QtCore.QObject.connect(self.actionFlu, QtCore.SIGNAL(_fromUtf8("triggered()")), self.btnFlu.click)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.btnExit, self.btnHelp)
MainWindow.setTabOrder(self.btnHelp, self.btnRequest)
MainWindow.setTabOrder(self.btnRequest, self.btnSOR_IFMC)
MainWindow.setTabOrder(self.btnSOR_IFMC, self.btnSOR_IAH)
MainWindow.setTabOrder(self.btnSOR_IAH, self.btnSOR_ILH)
MainWindow.setTabOrder(self.btnSOR_ILH, self.btnSOR_IMVH)
MainWindow.setTabOrder(self.btnSOR_IMVH, self.btnSOR_IFOH)
MainWindow.setTabOrder(self.btnSOR_IFOH, self.cmdEmail)
MainWindow.setTabOrder(self.cmdEmail, self.cmdHCSBrpt)
MainWindow.setTabOrder(self.cmdHCSBrpt, self.cmdWarRoom)
MainWindow.setTabOrder(self.cmdWarRoom, self.cmdReview)
MainWindow.setTabOrder(self.cmdReview, self.cmdIC_AccessDB)
MainWindow.setTabOrder(self.cmdIC_AccessDB, self.btnCaseCAUTI)
MainWindow.setTabOrder(self.btnCaseCAUTI, self.btnCaseCLABSI)
MainWindow.setTabOrder(self.btnCaseCLABSI, self.btnCaseSSI)
MainWindow.setTabOrder(self.btnCaseSSI, self.btnCaseVAP)
MainWindow.setTabOrder(self.btnCaseVAP, self.btnCaseLabID)
MainWindow.setTabOrder(self.btnCaseLabID, self.btnCaseMDRO)
MainWindow.setTabOrder(self.btnCaseMDRO, self.btnHH)
MainWindow.setTabOrder(self.btnHH, self.btnNHSN)
MainWindow.setTabOrder(self.btnNHSN, self.btnHAI)
MainWindow.setTabOrder(self.btnHAI, self.btnPremier)
MainWindow.setTabOrder(self.btnPremier, self.btnFlu)
MainWindow.setTabOrder(self.btnFlu, self.btnMicrosoft)
MainWindow.setTabOrder(self.btnMicrosoft, self.dateEdit)
MainWindow.setTabOrder(self.dateEdit, self.timeEdit)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Infection Control Database - Landing Page", None))
self.lblWelcome.setText(_translate("MainWindow", "Welcome!", None))
self.dateEdit.setDisplayFormat(_translate("MainWindow", "MMMM dd yyyy", None))
self.btnExit.setStatusTip(_translate("MainWindow", "Exit Database", None))
self.btnExit.setText(_translate("MainWindow", "Exit", None))
self.btnExit.setShortcut(_translate("MainWindow", "Ctrl+Shift+Q", None))
self.btnHelp.setText(_translate("MainWindow", "Help", None))
self.btnRequest.setText(_translate("MainWindow", "Request ", None))
self.grpReportsSOR.setStatusTip(_translate("MainWindow", "SOR Reports", None))
self.grpReportsSOR.setTitle(_translate("MainWindow", "SOR Files", None))
self.btnSOR_IFMC.setText(_translate("MainWindow", "IFMC", None))
self.btnSOR_IAH.setText(_translate("MainWindow", "IAH", None))
self.btnSOR_ILH.setText(_translate("MainWindow", "ILH", None))
self.btnSOR_IMVH.setText(_translate("MainWindow", "IMVH", None))
self.btnSOR_IFOH.setText(_translate("MainWindow", "IFOH", None))
self.grp_EnterCaseData.setTitle(_translate("MainWindow", "Case Data Entry", None))
self.btnCaseCAUTI.setStatusTip(_translate("MainWindow", "Enter CAUTI Case", None))
self.btnCaseCAUTI.setText(_translate("MainWindow", "CAUTI Case", None))
self.btnCaseCLABSI.setStatusTip(_translate("MainWindow", "Enter CLABSI Case ", None))
self.btnCaseCLABSI.setText(_translate("MainWindow", "CLABSI Case", None))
self.btnCaseSSI.setStatusTip(_translate("MainWindow", "Enter SSI Case", None))
self.btnCaseSSI.setText(_translate("MainWindow", "SSI Case", None))
self.btnCaseVAP.setStatusTip(_translate("MainWindow", "Enter VAP Case", None))
self.btnCaseVAP.setText(_translate("MainWindow", "VAP Case", None))
self.btnCaseLabID.setStatusTip(_translate("MainWindow", "Enter LabID Case", None))
self.btnCaseLabID.setText(_translate("MainWindow", "LabID Case", None))
self.btnCaseMDRO.setStatusTip(_translate("MainWindow", "Enter MDRO Cases", None))
self.btnCaseMDRO.setText(_translate("MainWindow", "MDRO", None))
self.cmdEmail.setText(_translate("MainWindow", "Email", None))
self.cmdHCSBrpt.setText(_translate("MainWindow", "HCSB Report", None))
self.cmdWarRoom.setText(_translate("MainWindow", "War Room Report", None))
self.cmdReview.setText(_translate("MainWindow", "Review HAI Data", None))
self.cmdIC_AccessDB.setText(_translate("MainWindow", "2011 IC Access Database", None))
self.label.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-weight:600; color:#aa0000;\">Infection Control Database</span></p></body></html>", None))
self.btnHH.setStatusTip(_translate("MainWindow", "Hand Hygiene Application", None))
self.btnNHSN.setStatusTip(_translate("MainWindow", "NHSN Website", None))
self.btnHAI.setStatusTip(_translate("MainWindow", "HAI Folder", None))
self.btnPremier.setStatusTip(_translate("MainWindow", "Navigate to SafetySurveillor: https://ss.premierinc.com/Setnet/user/Login.po ", None))
self.btnFlu.setStatusTip(_translate("MainWindow", "Flu Reports", None))
self.groupBox.setStatusTip(_translate("MainWindow", "Navigate to Location", None))
self.groupBox.setTitle(_translate("MainWindow", "Navigation ", None))
self.menuFile.setTitle(_translate("MainWindow", "File", None))
self.menuEnd_Processes.setTitle(_translate("MainWindow", "End Processes", None))
self.menuView.setTitle(_translate("MainWindow", "View", None))
self.menuReports.setTitle(_translate("MainWindow", "Reports", None))
self.actionExit.setText(_translate("MainWindow", "Exit", None))
self.actionExit.setShortcut(_translate("MainWindow", "Ctrl+Shift+Q", None))
self.actionTerminate_Excel.setText(_translate("MainWindow", "Terminate Excel", None))
self.actionTerminate_Access.setText(_translate("MainWindow", "Terminate Access", None))
self.actionTerminate_Powerpoint.setText(_translate("MainWindow", "Terminate PowerP", None))
self.actionTerminate_Word.setText(_translate("MainWindow", "Terminate Word", None))
self.actionReset_Citrix.setText(_translate("MainWindow", "Reset Citrix", None))
self.actionReset_Citrix.setStatusTip(_translate("MainWindow", "Reset Citrix Server", None))
self.actionTerminate_Outlook.setText(_translate("MainWindow", "Terminate Outlook", None))
self.actionHCSB.setText(_translate("MainWindow", "HCSB", None))
self.actionWar_Room.setText(_translate("MainWindow", "War Room", None))
self.actionFlu.setText(_translate("MainWindow", "Flu", None))
信号事件(电子邮件)
self.cmdEmail.clicked.connect(self.EmailClicked)
信号事件(退出)
self.btnExit.clicked.connect(self.ExitClicked)
操作事件(电子邮件)
def EmailClicked(self):os.startfile('C:\Program Files\Microsoft Office\Office14\OUTLOOK.EXE')
动作事件(退出)
def ExitClicked(self):
ret = QMessageBox.critical(self, "Critical",
'Are you sure you want to exit?', QMessageBox.Ok, QMessageBox.Cancel)
ret = msgBox.exec_()
if ret == msgBox.Yes:
QtCore.QCoreApplication.instance().quit()
class ControlMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(ControlMainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.dateEdit.setDisplayFormat("MMM dd yyyy")
self.ui.timeEdit.setDisplayFormat("hh:mm:ss AP")
self.updateTime()
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.updateTime)
self.timer.start(1000)
def updateTime(self):
current = QtCore.QDateTime.currentDateTime()
self.ui.dateEdit.setDate(current.date())
self.ui.timeEdit.setTime(current.time())
def closeEvent(self, event):
result = QtGui.QMessageBox.critical(self,
"Confirm Exit...",
"Are you sure you want to exit?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
event.ignore()
if result == QtGui.QMessageBox.Yes:
event.accept()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
#app.setStyle( "Classic" )
mySW = ControlMainWindow()
mySW.show()
sys.exit(app.exec_())
您的代码生成错误,因为 QMessageBox.critical
需要传递 QObject
对象而 Ui_MainWindow
不需要。
class Ui_MainWindow(object):
....
def ExitClicked(self):
ret = QMessageBox.critical(self, "Critical",
'Are you sure you want to exit?', QMessageBox.Ok, QMessageBox.Cancel)
ret = msgBox.exec_()
if ret == msgBox.Yes:
QtCore.QCoreApplication.instance().quit()
此外,您希望按下按钮默认关闭和您的新关闭按钮具有相同的行为,您必须将发出新按钮的信号与关闭功能连接起来,因为关闭功能调用 closeEvent
.
class Ui_MainWindow(object):
....
class ControlMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(ControlMainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.btnExit.clicked.connect(self.close)
....
def closeEvent(self, event):
result = QtGui.QMessageBox.critical(self,
"Confirm Exit...",
"Are you sure you want to exit?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
event.ignore()
if result == QtGui.QMessageBox.Yes:
event.accept()
删除连接:self.btnExit.clicked.connect(self.ExitClicked)
和函数 def ExitClicked(self): ...