Python 在单击按钮时多次运行的脚本
我有一个python GUI脚本,它有4个按钮,可以调用4个相关的外部脚本。单击每个按钮,将运行每个相关的外部脚本,并为每个脚本创建一个单独的csv文件,其中包含数据。问题是,当我按下GUI上的按钮时,它似乎不止一次地运行相应的脚本,即在每个csv中生成两倍的数据。我不希望这样,我只希望脚本在单击时运行一次以生成一次数据。我应该如何阻止脚本在单击按钮时多次调用Python 在单击按钮时多次运行的脚本,python,pyqt5,Python,Pyqt5,我有一个python GUI脚本,它有4个按钮,可以调用4个相关的外部脚本。单击每个按钮,将运行每个相关的外部脚本,并为每个脚本创建一个单独的csv文件,其中包含数据。问题是,当我按下GUI上的按钮时,它似乎不止一次地运行相应的脚本,即在每个csv中生成两倍的数据。我不希望这样,我只希望脚本在单击时运行一次以生成一次数据。我应该如何阻止脚本在单击按钮时多次调用 # Main Class that holds User Interface Objects class Ui_MainWindow(o
# 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))
self.simCardButton.setToolTip("Select to parse sim card data")
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("Select to parse call log data")
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("Select to parse sms data")
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("Select to parse canonical address data")
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.setToolTip("Choose data report type")
self.ViewDataCB.setObjectName("ViewDataCB")
self.ViewDataCB.addItem("canonical address")
self.ViewDataCB.addItem("sim card")
self.ViewDataCB.addItem("sms")
self.ViewDataCB.addItem("call logs")
self.ViewDataCB.addItem("choice prompt")
self.ViewDataCB.addItem("full report")
# # View Evidence Button
self.viewReportsButton = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.viewReportsButton.setFont(font)
self.viewReportsButton.setObjectName("viewReportsButton")
# Label for evidence viewer
self.evidenceViewingLabel = QtWidgets.QLabel(self.centralwidget)
self.evidenceViewingLabel.setGeometry(QtCore.QRect(660, 120, 141, 16))
self.evidenceViewingLabel.setObjectName("evidenceViewingLabel")
self.generateReportButton = QtWidgets.QPushButton(self.centralwidget)
self.generateReportButton.setGeometry(QtCore.QRect(650, 320, 141, 31))
self.generateReportButton.setObjectName("generateReportButton")
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", "Canonical Address Report"))
self.ViewDataCB.setItemText(2, _translate("MainWindow", "Sim Card Report"))
self.ViewDataCB.setItemText(3, _translate("MainWindow", "SMS Report"))
self.ViewDataCB.setItemText(4, _translate("MainWindow", "Call Log Report"))
self.ViewDataCB.setItemText(5, _translate("MainWindow", "Full Evidence Report"))
self.viewReportsButton.setText(_translate("MainWindow", "View Reports"))
self.viewReportsButton.setToolTip("Select to open report viewing window")
self.evidenceViewingLabel.setText(_translate("MainWindow", "Report Generator"))
self.generateReportButton.setText(_translate("MainWindow", "Generate Report"))
self.generateReportButton.setToolTip("Select to 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.viewReportsButton.setGeometry(QtCore.QRect(650, 360, 141, 31))
self.viewReportsButton.clicked.connect(self.openReportViewer)
# Canonical Addresses Button click functionality to parse canonical address data
self.canonicalAddressesButton.clicked.connect(self.select_canonical_data)
def select_canonical_data(self):
os.system('CanonicalAddressesParser.py')
# Call Logs Button click functionality to parse call log data
self.callLogButton.clicked.connect(self.select_call_data)
def select_call_data(self):
os.system('CallLogParser.py')
# SMS Button click functionality to parse SMS data
self.smsButton.clicked.connect(self.select_sms_data)
def select_sms_data(self):
os.system('SmsParser.py')
# Sim Card Button click functionality to parse sim card data
self.simCardButton.clicked.connect(self.select_sim_data)
def select_sim_data(self):
os.system('SimCardParser.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_())
我想你不明白connect是如何工作的。
您所做的是:
1.调用<代码>重传UI时,将第一个按钮连接到函数上
2.单击按钮时调用的函数将“下一步”按钮连接到新函数
3.等所有按钮 因此,如果您在第一个按钮中多次单击(例如,
CanonicalAddresssButton
),它将多次连接按钮callLogButton
。因此多次调用函数选择调用数据
因此,要正确连接所有按钮,您必须在函数<代码>重传UI
中连接所有按钮,然后在此之后声明要连接的4个函数
注意与python的识别
所以你需要改变这一点:
# Canonical Addresses Button click functionality to parse canonical address data
self.canonicalAddressesButton.clicked.connect(self.select_canonical_data)
def select_canonical_data(self):
os.system('CanonicalAddressesParser.py')
# Call Logs Button click functionality to parse call log data
self.callLogButton.clicked.connect(self.select_call_data)
def select_call_data(self):
os.system('CallLogParser.py')
# SMS Button click functionality to parse SMS data
self.smsButton.clicked.connect(self.select_sms_data)
def select_sms_data(self):
os.system('SmsParser.py')
# Sim Card Button click functionality to parse sim card data
self.simCardButton.clicked.connect(self.select_sim_data)
def select_sim_data(self):
os.system('SimCardParser.py')
作者:
你的按钮在工作吗。。。是的,但假设我运行self.smsButton.clicked.connect(self.select_sms_data)def select_sms_data(self):os.system('SmsParser.py')。它可能会在我单击它时创建数据2到3次,而不是只创建一次。我也不能按任何顺序单击这4个按钮中的任何一个。似乎CanonicalAddresses按钮必须在任何其他按钮之前先单击。是的,它必须先单击,我将解释为什么我在那里测试了它,我将一个函数移到了另一个之上。顶部的函数是唯一一个首先工作的函数。谢谢。我现在可以看出我做错了什么。它现在工作得很好。我会接受你的回答。太好了,很高兴能帮上忙
# 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)
def select_canonical_data(self):
os.system('CanonicalAddressesParser.py')
def select_call_data(self):
os.system('CallLogParser.py')
def select_sms_data(self):
os.system('SmsParser.py')
def select_sim_data(self):
os.system('SimCardParser.py')