Reputation: 809
I have created a gui that has two buttons: one that says "Open Input File:" and one that says "Run".
When someone clicks on "Open Input File:", he/she can select a file as input, and when that person then clicks on "Run", the script runScrapy is supposed to start.
The part where the buttons are set looks like this:
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
However, instead of running "runScrapy" when pushButton_2 is clicked, "runScrapy" starts running immediately after I start this script. I don't understand why, since showDialog does only pop up after you've clicked "Open Input File:".
Q: How do I change my code so that runScrapy only runs when pushButton_2 is clicked?
Thanks in advance!
Upvotes: 2
Views: 208
Reputation: 1313
You should start with Qt event handling.
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
# set your window/mainwindow here
sys.exit(app.exec_())
Maybe this tutorial is also helpful: Qt GUI app with python
Upvotes: 2