我创建了一个带有两个按钮的gui:一个说"打开输入文件:"还有一个说" Run"。
当有人点击"打开输入文件:"时,他/她可以选择一个文件作为输入,当该人点击"运行"时,脚本runScrapy是应该开始。
设置按钮的部分如下所示:
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Open Input File:"))
self.pushButton.clicked.connect(self.showDialog)
self.pushButton_2.setText(_translate("MainWindow", "Run"))
self.pushButton_2.clicked.connect(self.runScrapy)
self.label.setText(_translate("MainWindow", "Happy Scraping"))
def showDialog(self):
fileName = QFileDialog.getOpenFileName()
if fileName:
global file
file = fileName[0]
print(file)
def runScrapy(self):
process = CrawlerProcess()
process.crawl(BasicSpider)
process.start() # the script will block here until the crawling is finished
然而,不是运行" runScrapy"当点击pushButton_2时," runScrapy"我启动此脚本后立即开始运行。我不明白为什么,因为showDialog只会在你点击之后弹出"打开输入文件:"。
问:如何更改代码,以便runScrapy仅在点击pushButton_2时运行?
提前致谢!
答案 0 :(得分:2)
您应该从Qt事件处理开始。
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
# set your window/mainwindow here
sys.exit(app.exec_())
也许本教程也很有用:Qt GUI app with python