无法显示消息对话框
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法显示消息对话框相关的知识,希望对你有一定的参考价值。
我有一个包含一些QPushButtons的类,每当我按下一个按钮时,我希望程序显示一条消息,说明已执行该操作。到目前为止,我已经尝试创建一个QMessageBox并在它下面的按钮函数中调用它,但它不起作用。我一直收到编译器错误。这是我的代码和我的堆栈跟踪。我无法弄清楚我做错了什么。
# import Statements
from PyQt5 import QtCore, QtWidgets
from reportViewerWindow import Ui_reportViewerWindow
from PyQt5 import QtGui
from PyQt5.QtWidgets import QMessageBox
import os
# Main Class that holds User Interface Objects
class Ui_MainWindow(object):
# Function for Opening Report Viewer Window From Main Window by clicking View Reports button
def openReportViewer(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_reportViewerWindow()
self.ui.setupUi(self.window)
self.window.show()
# Main window setup
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setFixedSize(834, 428)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.MinimumExpanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
MainWindow.setFont(font)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setStyleSheet("background-color: Cornflowerblue")
MainWindow.setAutoFillBackground(True)
# Sim Card Button
self.simCardButton = QtWidgets.QPushButton(self.centralwidget)
self.simCardButton.setGeometry(QtCore.QRect(30, 200, 211, 30))
self.simCardButton.setToolTip("This option extracts all sim card data that is stored on the suspect devices sim card")
self.simCardButton.setToolTip("This data relates to all stored sim data on the suspect device")
self.simCardButton.setStyleSheet("background-color: Silver")
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
self.simCardButton.setFont(font)
self.simCardButton.setObjectName("simCardButton")
# Call Logs Button
self.callLogButton = QtWidgets.QPushButton(self.centralwidget)
self.callLogButton.setGeometry(QtCore.QRect(30, 300, 211, 30))
self.callLogButton.setToolTip("This option extracts all call log data that is stored on the suspect device")
self.callLogButton.setToolTip("This data relates to all incoming or outgoing phone calls stored on the suspect device")
self.callLogButton.setStyleSheet("background-color: Silver")
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
self.callLogButton.setFont(font)
self.callLogButton.setObjectName("callLogButton")
# SMS Button
self.smsButton = QtWidgets.QPushButton(self.centralwidget)
self.smsButton.setGeometry(QtCore.QRect(30, 250, 211, 30))
self.smsButton.setToolTip("This option extracts all text message data that is stored on the device")
self.smsButton.setToolTip("This data relates to all text messages that have been sent and received on the suspect device")
self.smsButton.setStyleSheet("background-color: Silver")
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
self.smsButton.setFont(font)
self.smsButton.setObjectName("smsButton")
# Canonical Address Button
self.canonicalAddressesButton = QtWidgets.QPushButton(self.centralwidget)
self.canonicalAddressesButton.setGeometry(QtCore.QRect(30, 150, 211, 30))
self.canonicalAddressesButton.setToolTip("This option extracts all text message data that is stored on the device")
self.canonicalAddressesButton.setToolTip("This data relates to all devices that have been in contact with the suspect device")
self.canonicalAddressesButton.setStyleSheet("background-color: Silver")
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
self.canonicalAddressesButton.setFont(font)
self.canonicalAddressesButton.setObjectName("canonicalAddressesButton")
# Main Window Label
self.windowMainLabel = QtWidgets.QLabel(self.centralwidget)
self.windowMainLabel.setGeometry(QtCore.QRect(180, 20, 500, 32))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(20)
font.setBold(True)
font.setWeight(75)
self.windowMainLabel.setFont(font)
self.windowMainLabel.setAlignment(QtCore.Qt.AlignCenter)
self.windowMainLabel.setObjectName("windowMainLabel")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 110, 291, 20))
self.label.setObjectName("label")
# # View Reports Button
self.viewReportsButton = QtWidgets.QPushButton(self.centralwidget)
self.viewReportsButton.setStyleSheet("background-color: Silver")
self.viewReportsButton.setGeometry(QtCore.QRect(650, 370, 175, 31))
self.viewReportsButton.clicked.connect(self.openReportViewer)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.viewReportsButton.setFont(font)
self.viewReportsButton.setObjectName("viewReportsButton")
self.viewReportsButton.setEnabled(False)
# Label for evidence viewer
self.evidenceViewingLabel = QtWidgets.QLabel(self.centralwidget)
self.evidenceViewingLabel.setGeometry(QtCore.QRect(650, 120, 141, 16))
self.evidenceViewingLabel.setObjectName("evidenceViewingLabel")
# Buttons for report generation
self.generate_canonical_report = QtWidgets.QPushButton(self.centralwidget)
self.generate_canonical_report.setGeometry(QtCore.QRect(650, 150, 175, 31))
self.generate_canonical_report.setObjectName("generate_canonical_report")
self.generate_canonical_report.setStyleSheet("background-color: Silver")
self.generate_canonical_report.setEnabled(False)
self.generate_Sim_Report = QtWidgets.QPushButton(self.centralwidget)
self.generate_Sim_Report.setGeometry(QtCore.QRect(650, 190, 175, 31))
self.generate_Sim_Report.setObjectName("generate_sim_report")
self.generate_Sim_Report.setStyleSheet("background-color: Silver")
self.generate_Sim_Report.setEnabled(False)
self.generateSMS_Report = QtWidgets.QPushButton(self.centralwidget)
self.generateSMS_Report.setGeometry(QtCore.QRect(650, 230, 175, 31))
self.generateSMS_Report.setObjectName("generate_sms_report")
self.generateSMS_Report.setStyleSheet("background-color: Silver")
self.generateSMS_Report.setEnabled(False)
self.generate_callLog_report = QtWidgets.QPushButton(self.centralwidget)
self.generate_callLog_report.setGeometry(QtCore.QRect(650, 270, 175, 31))
self.generate_callLog_report.setObjectName("generate_callLog_report")
self.generate_callLog_report.setStyleSheet("background-color: Silver")
self.generate_callLog_report.setEnabled(False)
self.generateFullReportButton = QtWidgets.QPushButton(self.centralwidget)
self.generateFullReportButton.setGeometry(QtCore.QRect(650, 310, 175, 31))
self.generateFullReportButton.setObjectName("generateFullReportButton")
self.generateFullReportButton.setStyleSheet("background-color: Silver")
self.generateFullReportButton.setEnabled(False)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.actionOpen_2 = QtWidgets.QAction(MainWindow)
self.actionOpen_2.setObjectName("actionOpen_2")
self.actionExit_2 = QtWidgets.QAction(MainWindow)
self.actionExit_2.setObjectName("actionExit_2")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
# Function that sets the text on all the UI Buttons
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Data Parser"))
self.label.setText(_translate("MainWindow", "Data Parsing Options"))
self.simCardButton.setText(_translate("MainWindow", "Sim Card"))
self.callLogButton.setText(_translate("MainWindow", "Call Logs"))
self.smsButton.setText(_translate("MainWindow", "SMS "))
self.canonicalAddressesButton.setText(_translate("MainWindow", "Canonical Addresses"))
self.windowMainLabel.setText(_translate("MainWindow", "AMF Parser"))
# Report Button set text and tool_tips
self.evidenceViewingLabel.setText(_translate("MainWindow", "Report Generator"))
self.generate_canonical_report.setText(_translate("MainWindow", "Canonical Report"))
self.generate_canonical_report.setToolTip("Select to generate canonical address report")
self.generate_Sim_Report.setText(_translate("MainWindow", "Sim Report"))
self.generate_Sim_Report.setToolTip("Select to generate sim card report")
self.generateSMS_Report.setText(_translate("MainWindow", "SMS Report"))
self.generateSMS_Report.setToolTip("Select to generate sms report")
self.generate_callLog_report.setText(_translate("MainWindow", "Call Log Report"))
self.generate_callLog_report.setToolTip("Select to generate call log report")
self.viewReportsButton.setText(_translate("MainWindow", "View Reports"))
self.viewReportsButton.setToolTip("Select to open report viewing window")
self.generateFullReportButton.setText(_translate("MainWindow", "Full Report"))
self.generateFullReportButton.setToolTip("Select to generate full report")
self.actionOpen.setText(_translate("MainWindow", "Open"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
self.actionOpen_2.setText(_translate("MainWindow", "Open"))
self.actionExit_2.setText(_translate("MainWindow", "Exit"))
# Event Handling Code Section
# Parsing Area!!!
# Canonical Addresses Button click functionality to parse canonical address data
self.canonicalAddressesButton.clicked.connect(self.select_canonical_data)
# Sim Card Button click functionality to parse sim card data
self.simCardButton.clicked.connect(self.select_sim_data)
# Call Logs Button click functionality to parse call log data
self.callLogButton.clicked.connect(self.select_call_data)
# SMS Button click functionality to parse SMS data
self.smsButton.clicked.connect(self.select_sms_data)
# Reporting Area!!!
# Report Generation Functions!!!
self.one_pass = False
self.two_pass = False
self.three_pass = False
self.four_pass = False
self.five_pass = False
# canonical address report generating functionality
self.generate_canonical_report.clicked.connect(self.generate_canonicalR)
# sim card report generating functionality
self.generate_Sim_Report.clicked.connect(self.generate_simR)
# sms report generating functionality
self.generateSMS_Report.clicked.connect(self.generate_smsR)
# call log report generating functionality
self.generate_callLog_report.clicked.connect(self.generate_call_LogR)
# All data report
self.generateFullReportButton.clicked.connect(self.generate_full_reportR)
# Data Parsed Dialog Box (This is where my problem is)
self.canonicalAddressesButton.clicked.connect(self.data_parsed_notification)
def data_parsed_notification(self):
msgBox = QtWidgets.QMessageBox()
msgBox.information(self, 'Message', 'Data Has Been Parsed!', QMessageBox.StandardButtons)
# Canonical Addresses button function
def select_canonical_data(self):
os.system('CanonicalAddressesParser.py')
self.canonicalAddressesButton.clicked.connect(self.data_parsed_notification)
self.generate_canonical_report.setEnabled(True)
# call log button function
def select_call_data(self):
os.system('CallLogParser.py')
self.generate_callLog_report.setEnabled(True)
# sms button function
def select_sms_data(self):
os.system('SmsParser.py')
self.generateSMS_Report.setEnabled(True)
# sim card button function
def select_sim_data(self):
os.system('SimCardParser.py')
self.generate_Sim_Report.setEnabled(True)
# report generation functions
def generate_canonicalR(self):
os.system('CanonicalAddressReporter.py')
self.generate_Sim_Report.setEnabled(True)
self.check_run_button()
def generate_simR(self):
os.system('SimCardReporter.py')
self.generateSMS_Report.setEnabled(True)
self.check_run_button()
def generate_smsR(self):
os.system('SmsDataReporter.py')
self.generate_callLog_report.setEnabled(True)
self.check_run_button()
def generate_call_LogR(self):
os.system('CallLogReporter.py')
self.check_run_button()
self.generateFullReportButton.setEnabled(True)
def generate_full_reportR(self):
os.system('AllDataReport.py')
self.check_run_button()
self.viewReportsButton.setEnabled(True)
def check_run_button(self):
if self.one_pass == self.two_pass == self.three_pass == self.four_pass is True:
self.generateFullReportButton.setEnabled(True)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
堆栈跟踪
line 270, in data_parsed_notification
msgBox.information(self, 'Message', 'Data Has Been Parsed!', QMessageBox.StandardButtons)
TypeError: QMessageBox.information(QWidget, str, str, QMessageBox.StandardButtons buttons=QMessageBox.Ok, QMessageBox.StandardButton defaultButton=QMessageBox.NoButton): argument 1 has unexpected type 'Ui_MainWindow'
改为self.window
self.canonicalAddressesButton.clicked.connect(self.data_parsed_notification)
window = QtWidgets
def data_parsed_notification(self):
msgBox = QtWidgets.QMessageBox()
msgBox.information(self.window, 'Message', 'Data Has Been Parsed!', QMessageBox.StandardButtons)
答案
简短解决方案
def setupUi(self, MainWindow):
self.mainwindow = MainWindow
[...]
def data_parsed_notification(self):
QtWidgets.QMessageBox.information(self.mainwindow, 'Message', 'Data Has Been Parsed!', QtWidgets.QMessageBox.Ok)
简短的解决方案可以解决您当前的问题,但我认为从长远来看,您将遇到更多相同类型的问题,因此即使它易于实现,我建议使用长解决方案,因为它将避免其他问题。
长解决方案:
生成Qt Designer的类不是小部件,它是用于填充小部件的类,并且因为information()
的QMessageBox
方法需要QWidget
正在生成该类型的错误。
建议不要修改设计,也就是说,你应该保留设计,并创建另一个负责管理逻辑的.py文件,这样你就可以再次修改你的设计,而不必重新编写任何东西。
假设您已经恢复了设计和该文件,我们可以将其称为ui_mainwindow.py:
UI_main window.朋友
class Ui_MainWindow(object):
from PyQt5 import QtCore, QtWidgets
# Main window setup
def setupUi(self, MainWindow):
[...]
# Function that sets the text on all the UI Buttons
def retranslateUi(self, MainWindow):
[...]
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
然后创建另一个名为main.py的文件,我们将在其中创建窗口小部件并使用设计来填充它:
from PyQt5 import QtCore, QtWidgets
from reportViewerWindow import Ui_reportViewerWindow
from ui_mainwindow import Ui_MainWindow
class ReportViewerWindow(QtWidgets.QMainWindow, Ui_reportViewerWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
并且在这个新类中实现了你的逻辑,上面是一个简短的总结,但接下来我将放置所有代码:
完整代码:
UI_main window.朋友
# Main GUI Window for parser
# Daniel Mc Henry
# Project Development
# CSDF 2017/2018
# L00113325
# import Statements
from PyQt5 import QtCore, QtGui, QtWidgets
# Main Class that holds User Interface Objects
class Ui_MainWindow(object):
# Main window setup
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setFixedSize(834, 428)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.MinimumExpanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
MainWindow.setFont(font)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setStyleSheet("background-color: Cornflowerblue")
MainWindow.setAutoFillBackground(True)
# Sim Card Button
self.simCardButton = QtWidgets.QPushButton(self.centralwidget)
self.simCardButton.setGeometry(QtCore.QRect(30, 200, 211, 30))
self.simCardButton.setToolTip("This option extracts all sim card data that is stored on the suspect devices sim card")
self.simCardButton.setToolTip("This data relates to all stored sim data on the suspect device")
self.simCardButton.setStyleSheet("background-color: Silver")
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
self.simCardButton.setFont(font)
self.simCardButton.setObjectName("simCardButton")
# Call Logs Button
self.callLogButton = QtWidgets.QPushButton(self.centralwidget)
self.callLogButton.setGeometry(QtCore.QRect(30, 300, 211, 30))
self.callLogButton.setToolTip("This option extracts all call log data that is stored on the suspect device")
self.callLogButton.setToolTip("This data relates to all incoming or outgoing phone calls stored on the suspect device")
self.callLogButton.setStyleSheet("background-color: Silver")
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
self.callLogButton.setFont(font)
self.callLogButton.setObjectName("callLogButton")
# SMS Button
self.smsButton = QtWidgets.QPushButton(self.centralwidget)
self.smsButton.setGeometry(QtCore.QRect(30, 250, 211, 30))
self.smsButton.setToolTip("This option extracts all text message data that is stored on the device")
self.smsButton.setToolTip("This data relates to all text messages that have been sent and received on the suspect device")
self.smsButton.setStyleSheet("background-color: Silver")
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
self.smsButton.setFont(font)
self.smsButton.setObjectName("smsButton")
# Canonical Address Button
self.canonicalAddressesButton = QtWidgets.QPushButton(self.centralwidget)
self.canonicalAddressesButton.setGeometry(QtCore.QRect(30, 150, 211, 30))
self.canonicalAddressesButton.setToolTip("This option extracts all text message data that is stored on the device")
self.canonicalAddressesButton.setToolTip("This data relates to all devices that have been in contact with the suspect device")
self.canonicalAddressesButton.setStyleSheet("background-color: Silver")
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
self.canonicalAddressesButton.setFont(font)
self.canonicalAddressesButton.setObjectName("canonicalAddressesButton")
# Main Window Label
self.windowMainLabel = QtWidgets.QLabel(self.centralwidget)
self.windowMainLabel.setGeometry(QtCore.QRect(180, 20, 500, 32))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(20)
font.setBold(True)
font.setWeight(75)
self.windowMainLabel.setFont(font)
self.windowMainLabel.setAlignment(QtCore.Qt.AlignCenter)
self.windowMainLabel.setObjectName("windowMainLabel")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 110, 291, 20))
self.label.setObjectName("label")
# # View Reports Button
self.viewReportsButton = QtWidgets.QPushButton(self.centralwidget)
self.viewReportsButton.setStyleSheet("background-color: Silver")
self.viewReportsButton.setGeometry(QtCore.QRect(650, 370, 175, 31))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.viewReportsButton.setFont(font)
self.viewReportsButton.setObjectName("viewReportsButton")
self.viewReportsButton.setEnabled(False)
# Label for evidence viewer
self.evidenceViewingLabel = QtWidgets.QLabel(self.centralwidget)
self.evidenceViewingLabel.setGeometry(QtCore.QRect(650, 120, 141, 16))
self.evidenceViewingLabel.setObjectName("evidenceViewingLabel")
# Buttons for report generation
self.generate_canonical_report = QtWidgets.QPushButton(self.centralwidget)
self.generate_canonical_report.setGeometry(QtCore.QRect(650, 150, 175, 31))
self.generate_canonical_report.setObjectName("generate_canonical_report")
self.generate_canonical_report.setStyleSheet("background-color: Silver")
self.generate_canonical_report.setEnabled(False)
self.generate_Sim_Report = QtWidgets.QPushButton(self.centralwidget)
self.generate_Sim_Report.setGeometry(QtCore.QRect(650, 190, 175, 31))
self.generate_Sim_Report.setObjectName("generate_sim_report")
se以上是关于无法显示消息对话框的主要内容,如果未能解决你的问题,请参考以下文章