无意中创建一个小部件

时间:2018-02-21 19:37:13

标签: python pyqt pyqt5

我的系统主窗口有一个GUI类。我已将名为“viewEvidenceButton”的按钮链接到名为“openReportViewer”的函数,以便在单击该按钮时打开报告查看器窗口。我注意到在主窗口上,它创建了一个类似方形的小部件,就像我在运行程序时窗口左上角没有文本的按钮一样。为什么这样做,我怎么能阻止这种情况发生。这是我的主要GUI窗口代码。

# import Statements
from PyQt5 import QtCore, QtGui, QtWidgets
from reportViewerWindow import Ui_reportViewerWindow


# Main Class that holds User Interface Objects
class Ui_MainWindow(object):

    # Function for Opening Report Viewer Window From Main Window by clicking View Reports
    def openReportViewer(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_reportViewerWindow()
        self.ui.setupUi(self.window)
        MainWindow.hide()
        self.window.show()


    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")

        # Sim Card Button
        self.simCardButton = QtWidgets.QPushButton(self.centralwidget)
        self.simCardButton.setGeometry(QtCore.QRect(30, 200, 211, 30))
        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))
        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))
        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))
        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(170, 20, 423, 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")

        # Combo Box settings
        self.ViewDataCB = QtWidgets.QComboBox(self.centralwidget)
        self.ViewDataCB.setGeometry(QtCore.QRect(560, 150, 231, 22))
        self.ViewDataCB.setObjectName("ViewDataCB")
        self.ViewDataCB.addItem("")
        self.ViewDataCB.addItem("")
        self.ViewDataCB.addItem("")
        self.ViewDataCB.addItem("")
        self.ViewDataCB.addItem("")
        self.ViewDataCB.addItem("")

        # # View Evidence Button
        self.viewEvidenceButton = QtWidgets.QPushButton(self.centralwidget)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.viewEvidenceButton.setFont(font)
        self.viewEvidenceButton = QtWidgets.QPushButton(self.centralwidget)
        self.viewEvidenceButton.setObjectName("viewEvidenceButton")



        # Label for evidence viewer
        self.evidenceViewingLabel = QtWidgets.QLabel(self.centralwidget)
        self.evidenceViewingLabel.setGeometry(QtCore.QRect(660, 120, 141, 16))
        self.evidenceViewingLabel.setObjectName("evidenceViewingLabel")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(650, 320, 141, 31))
        self.pushButton.setObjectName("pushButton")
        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", "SQLite DB Android Parser"))
        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", "SQLite Android Database Parser"))
        self.label.setText(_translate("MainWindow", "Please Choose Data Parsing Option"))
        self.ViewDataCB.setItemText(0, _translate("MainWindow", "Choose An Option"))
        self.ViewDataCB.setItemText(1, _translate("MainWindow", "SMS Evidence"))
        self.ViewDataCB.setItemText(2, _translate("MainWindow", "Sim Card Evidence"))
        self.ViewDataCB.setItemText(3, _translate("MainWindow", "Call Log Evidence"))
        self.ViewDataCB.setItemText(4, _translate("MainWindow", "Canonical Address Evidence"))
        self.ViewDataCB.setItemText(5, _translate("MainWindow", "Full Report"))
        self.viewEvidenceButton.setText(_translate("MainWindow", "View Reports"))
        self.evidenceViewingLabel.setText(_translate("MainWindow", "Report Generator"))
        self.pushButton.setText(_translate("MainWindow", "Generate 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

        # Event Handling to open Report Viewer Window
        self.viewEvidenceButton.setGeometry(QtCore.QRect(650, 360, 141, 31))
        self.viewEvidenceButton.clicked.connect(self.openReportViewer)


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_())

报告查看器窗口

# import Statements
from PyQt5 import QtCore, QtGui, QtWidgets

# Main Class that holds User Interface Objects
class Ui_reportViewerWindow(object):
    def setupUi(self, reportViewerWindow):
        reportViewerWindow.setObjectName("reportViewerWindow")
        reportViewerWindow.setFixedSize(426, 343)
        self.centralwidget = QtWidgets.QWidget(reportViewerWindow)
        self.centralwidget.setObjectName("centralwidget")

        # sms Report View button
        self.smsReportView = QtWidgets.QPushButton(self.centralwidget)
        self.smsReportView.setGeometry(QtCore.QRect(200, 120, 191, 31))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.smsReportView.setFont(font)
        self.smsReportView.setObjectName("smsReportView")

        # Canonical Address Report View button
        self.canonicalReportView = QtWidgets.QPushButton(self.centralwidget)
        self.canonicalReportView.setGeometry(QtCore.QRect(200, 160, 191, 31))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.canonicalReportView.setFont(font)
        self.canonicalReportView.setObjectName("canonicalReportView")

        # Sim card Report View button
        self.simCardReportView = QtWidgets.QPushButton(self.centralwidget)
        self.simCardReportView.setGeometry(QtCore.QRect(200, 240, 191, 31))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.simCardReportView.setFont(font)
        self.simCardReportView.setObjectName("simCardReportView")

        # Call Log Report View button
        self.callLogReportView = QtWidgets.QPushButton(self.centralwidget)
        self.callLogReportView.setGeometry(QtCore.QRect(200, 200, 191, 31))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.callLogReportView.setFont(font)
        self.callLogReportView.setObjectName("callLogReportView")

        # Geometry and layout settings
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(70, 30, 351, 61))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName("label")

        # Full Report View button
        self.fullReportButton = QtWidgets.QPushButton(self.centralwidget)
        self.fullReportButton.setGeometry(QtCore.QRect(200, 280, 191, 31))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.fullReportButton.setFont(font)
        self.fullReportButton.setObjectName("fullReportButton")

        # Button that links to the view reports window
        reportViewerWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(reportViewerWindow)
        self.statusbar.setObjectName("statusbar")
        reportViewerWindow.setStatusBar(self.statusbar)
        self.retranslateUi(reportViewerWindow)
        QtCore.QMetaObject.connectSlotsByName(reportViewerWindow)

    # setting text on buttons
    def retranslateUi(self, reportViewerWindow):
        _translate = QtCore.QCoreApplication.translate
        reportViewerWindow.setWindowTitle(_translate("reportViewerWindow", "Report Viewer"))
        self.smsReportView.setText(_translate("reportViewerWindow", "View Sim Report"))
        self.canonicalReportView.setText(_translate("reportViewerWindow", "View SMS Report"))
        self.simCardReportView.setText(_translate("reportViewerWindow", "View Call Log Report"))
        self.callLogReportView.setText(_translate("reportViewerWindow", "View Canonical Report"))
        self.label.setText(_translate("reportViewerWindow", "Android Mobile Device Report Viewer"))
        self.fullReportButton.setText(_translate("reportViewerWindow", "View Full Report"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    reportViewerWindow = QtWidgets.QMainWindow()
    ui = Ui_reportViewerWindow()
    ui.setupUi(reportViewerWindow)
    reportViewerWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

问题是因为您正在创建2个按钮:

# # View Evidence Button
self.viewEvidenceButton = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.viewEvidenceButton.setFont(font)
self.viewEvidenceButton = QtWidgets.QPushButton(self.centralwidget) # <--- remove this line
self.viewEvidenceButton.setObjectName("viewEvidenceButton")

删除按钮的第二次创建,因此该按钮不会出现。