单击按钮运行脚本

时间:2018-02-22 09:30:42

标签: python pyqt5

我试图在按钮点击上运行外部Python脚本。我认为我几乎有它工作,虽然它一旦我运行GUI类本身就运行外部脚本。单击按钮时我需要它来运行脚本。我想我几乎拥有它,有人可以指导我需要改变的地方。

我编写了一个名为parseCanonical()的函数:这​​是我尝试设置按钮单击的位置,以便我可以运行外部脚本。

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

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



    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(200, 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.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)

        # Canonical Addresses Button Event Handling function
        def parseCanonical():
            self.canonicalAddressesButton.clicked().connect(self.select_data())
        os.system('CanonicalAddressesParser.py')


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

2 个答案:

答案 0 :(得分:0)

我认为os.system('CanonicalAddressesParser.py')不是您搜索的内容 如果您的脚本CanonicalAddressesParser.py与主脚本位于同一文件夹中,则可以将其导入import CanonicalAddressesParser
然后调用CanonicalAddressesParser内的函数,你需要写:CanonicalAddressesParser.func()

所以如果你替换

self.canonicalAddressesButton.clicked().connect(self.select_data())

通过

self.canonicalAddressesButton.clicked.connect(CanonicalAddressesParser.select_data)

有帮助吗?

答案 1 :(得分:0)

一旦运行GUI就打开脚本的原因是你在retranslateUi函数下有这个调用os.system('CanonicalAddressesParser.py'),所以它和其他一切一起被调用。此外,您在未调用的函数内部设置按钮。按钮似乎连接到一个不存在的功能(或者至少不包含在您发布的代码中)。您还错误地设置了该按钮。您在其上方设置的按钮单击事件是正确的。

我建议创建一个名为select_data的函数,并将脚本运行行移动到该函数,然后从按钮调用它。

def retranslateUi(self, MainWindow):
    # Code here
    self.canonicalAddressesButton.clicked.connect(self.select_data)

def select_data(self):
    os.system('CanonicalAddressesParser.py')